Блог пользователя qwexd

Автор qwexd, 2 года назад, По-английски

I've seen quite a lot of people asking about hacking but I couldn't find any guide in codeforces so I decided to make this blog.


What is Hacking?

$$$\ $$$ In a codeforces round, test cases might not be enough to judge your code. It's very normal since we can't expect setters to consider every single wrong solution to add new test cases. In other words, if your code gets Accepted during the contest, this doesn't mean your submission is necessarily correct. To avoid incorrect solutions that get Accepted due to the lack of enough test cases to judge during the round we use Hacking.

$$$\ $$$ There are two types of Hacking, one of them is after the contest and the other is during the contest; but the general idea of hack is providing a test case where you think someone's solution fails. The judge checks this specific test case you provided and determine if the solution actually fails. (Comparing outputs with actually working pre-made jury code to check if it gets Wrong Answer verdict, or simply checking if it's getting TLE/CE...) After Hacking Phase ends, all submissions run once again with new test cases that are added during the hacking phase. Note that if your solution is wrong and no one hacked you, it doesn't mean your submissions won't fail!


Hacking Types

During the Round (Div. 2, Div. 1 and mixed Rounds)

$$$\ $$$ Contests you can do hacks with this format are Div1, Div2, Global or Div1+2 contests. In this format, you can submit again to fix your mistake if you got hacked unless you locked your submission from the lock button. You can see the point for wrong/correct hacks from the right-down corner in the dashboard or any problem page. Successful hacks give 100 points while unsuccessful ones give -50 points. So if you cannot hack a solution, you will lose your points for nothing, so BE ABSOLUTELY SURE before doing something and think more.

  • Note that if you think you can solve more problems, I don't suggest you think about hacks, if you fail at doing hacks, you will only be wasting your time and decreasing your score. Hacking is the last choice if you don't feel any process at solving problems. Now let's look at how to see someone's code and submit a hack.

  • Only the latest Accepted submission counts, which means if you want to hack someone with multiple Accepted codes, you need to hack their latest Accepted code since other ones are useless as the penalty is also calculated from the latest Accepted code.

$$$\ $$$ To do hacks, you must go to the dashboard. After that, click the lock signature near your accepted solutions. Now you are ready to hack! Note that you must get Accepted before doing hacks in this problem.

  • Be careful! When you lock your solution, you won't be able to submit your locked problem again, so if you get hacked or find an edge case, you won't be able to submit again to the problem.

  • Tip: If you (or someone else) lock your solution, your score will be seen with bold font at the standings.

$$$\ $$$ After locking your solution, note that you won't be able to hack everyone. This means that you can only hack your roommates.

  • Oh wait, what does roommate mean? At the beginning of contests, the system will assign you to a random room. You can see your roommates' standings from the Room Section. If it doesn't appear, go to Dashboard and now you can see it.

$$$\ $$$ In the standings of the room section, you will see the points of your roommates and if you double-click or ctrl+click on a passed solution, you will be able to see the image of the solution code. That's because you must not just copy-paste the solution to gain time for stress-testing and brute force all solutions. Under that, you will see the Hack button and can send tests according to the constraints. If it turns out to be too long, you can send a generator code.

$$$\ $$$ If your hack is not suitable for the constraints, you will get the verdict 'Invalid input'. If your hack gets the verdict 'Unsuccessful hacking attempt' if the defenders' code works correctly against your input, and finally gets 'Successful hacking attempt' if the code gives TLE or gives a different answer from the jury answer or anything else other than Accepted.

  • Tip: Convert the image to text by typing while trying about the correctness of a solution. You can think about edge case and try it by your eyes but it can be a little dangerous so you can make a mistake (unless you are NOT ahmet23).

note: Sometimes you can't make hacks if pretests are too strong or the solution is too obvious. In addition, hacks can really be a lottery. One or more of your roommates' solutions must be wrong to hack. So I strongly suggest you look into more problems if you are able to solve them.


After the Round (Div. 3, Div. 4 and Educational Rounds)

$$$\ $$$ There is a 12-hour open hacking phase after the contest ends where all users can look at others' submissions and try to find a counter case to their solution and hack them. You don't get extra points for these hacks. After the open hacking phase ends all submissions run again with the new test cases that have been added to tests during the open hacking phase. Contests with this format are Div. 3, Div. 4 Rounds, and Educational Rounds.

$$$\ $$$ During a 12-hours hacking session, you will be able to see everyone's code, unlike during-contest hacks. You just can go standings page and click any solution you want. Now you can see the code as text and copy-paste it to your local computer and start some experiments on that lab rat as you wish :).

Note that you don't need to get AC at that problem to do hacks as these hacks won't give you any score.

Now let's touch on some tips to do hacks.

  • First, notice that you can sort solutions by running time. This gives the advantage to find slow codes. If the problem is about optimizing time or something else (you can understand it by problem), you can generate a test that causes worst-case and brute-force over slow codes and submit hacks one by one.

  • Second, can find an edge case that doesn't appear at pretests and brute force over random suspicious codes. Generally, ratings below the 1500s are doing mistakes commonly (it maybe can be a little unethical, but true) so you can look to especially their codes.

  • If the solution to a problem requires double or something else that might cause a lot of trouble to people and if the pretests are weak, it can be easy to hack. You can use the technique above.


Thanks to tolbi and burak for helping me with preparing the blog.

  • Проголосовать: нравится
  • +166
  • Проголосовать: не нравится

»
2 года назад, # |
  Проголосовать: нравится +10 Проголосовать: не нравится

Coincidentally, I wanted to learn about hacks recently. Thanks for this blog!

»
2 года назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

nice blog!

helped a lot thanks :)

»
2 года назад, # |
  Проголосовать: нравится +85 Проголосовать: не нравится

My biggest tip about hacking is to have the status page open filtered to hacks. This way you see all the hacks that have been done during the contest. If you start seeing a lot of hacks on a problem, it might be worth it to check your own code and maybe go for some yourself.

The two most common hacks are

  • Hashing problems, where wrong codes are using a fixed seed / bad randomisation. This also includes unsafe use of hashmaps / unordered maps.
  • Problems where you need to compute a sqrt / exponentiation, as many weak programmers use inaccurate functions like sqrt and pow. To hack sqrt-codes in particular, try inputs where square root is computed on $$$x^2 - 1$$$ or $$$x^2$$$.
  • »
    »
    2 года назад, # ^ |
    Rev. 3   Проголосовать: нравится -26 Проголосовать: не нравится

    Does the "Hashing problems" category really include TLE hacks for hash tables? Saying this because I don't think this is an "unsafe" use, but just an innate flaw of hash tables.

    upd: I also think a big part of hacking comes from a weak pretests, what are your opinions on this?

    • »
      »
      »
      2 года назад, # ^ |
        Проголосовать: нравится +3 Проголосовать: не нравится

      Yes it does include hash tables, you have to usually include some randomization component to make sure your hash table can't be hacked. For this reason, I generally always use ordered set/map to not have to worry about this.

»
2 года назад, # |
  Проголосовать: нравится +14 Проголосовать: не нравится

ahmet23 ORZ

»
2 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

as a newbie i always wanted to know about what hacks are and how to do them on contest anddddd finally a blog for hacks is here thankssss!!!!! kudos to author

»
2 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

This is very useful i think, because i didn't have any idea about locking a solution. Thanks for such a helpful post

»
2 года назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

I don't know if using OCR to paste the code during the round is valid.

If it's valid, do anyone have a great OCR tool? Typing other's code is suffering for me >_<

  • »
    »
    12 месяцев назад, # ^ |
      Проголосовать: нравится -9 Проголосовать: не нравится

    I don't think that's allowed according to this blog post :(

    Attempting to digitally extract other contestant's code during the hacking is considered cheating. You may not use any technical/digital tools to obtain other contestant's code, including (but not limited) OCR, traffic capture, browsers plugins and so on. The only allowed method to analyze other contestant's solution is reading it in a hacking window. However it is allowed to manually retype the solution or it's parts to run it locally.

»
2 года назад, # |
  Проголосовать: нравится +4 Проголосовать: не нравится

One more thing: pay attention to the compiler version on codeforces.

Some undefined behavior could run perfectly on G++ 7.X, but fails on G++ 11.

So when you are unsure about this kind of issue, the custom test is your great cooperator(although it would be very lag during the contest)

Thanks to qwexd,tolbi and burak for your great guide ^_^ hope more coders try hacking during the contest!

»
2 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Spoiler alert, the word to hack comes from the same root of حقق يحقق meaning to investigate, investigating in Arabic. This was my solution for why detective Conan was shrunk into a child version of himself.

»
12 месяцев назад, # |
  Проголосовать: нравится -15 Проголосовать: не нравится

can i add a new test case now to a question which was in contest months before which can fail my accepted code?

  • »
    »
    12 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    No. You can only add tests during the uphacking session which you need to be purple+ and there is only a week to do this after the contest.

    • »
      »
      »
      12 месяцев назад, # ^ |
        Проголосовать: нравится -15 Проголосовать: не нравится

      is there anyone who can add that test case now?

      • »
        »
        »
        »
        12 месяцев назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        I don't believe it worths but if you really want to try, maybe you can contact with authors of the problem?

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

if we have to present output in integer format and someone submitted it using string can we hack it ????

:/

»
11 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I used an int instead of a long long so my code failed a hack after the contest, does that mean It doesn't get accepted lowering my rating?