Last round involved a randomized solution for problem E, and some pretests were aimed against std's rand() function. This caused a big dispute among contestants. It was questioned whether doing this type of tests was fair or necessary, since passing them depended merely on specific language knowledge, not actual problem-solving skills. Contest coordinators counter-argued that participants would've gotten hacked if it hadn't been for these pretests. They also stated that it's one's responsibility to know their favorite language when using it on a round.
The fact is, this controversy was really caused by a technical property within C++ (and codeforces, partly), which many people had no clue existed. This is just an example of the fact that languages and their functioning in CF will sometimes have non-intuitive and unexpected properties, which may screw our performance in a contest. In other words, we are constantly exposed to bumping into another of these hidden behaviors, making the mentioned round hardly the last time something like this can happen.
That's why I thought: We should have a list of the main language-specific issues that could annoy us during contests. Just a brief enumeration of not-so-fun facts about languages, such that, if not known, could break our solutions or get us hacked. Making a brief list available for the community, and exposing it to participants before a round could prevent a lot of trouble. If a problem could make you bump into one of these weird language issues, contest coordinators could safely assume we know about them and not get into a moral debate whether it's fair to include tests against it. Also, contestants would be safe from losing rating to specific language knowledge. In other words, we could prevent issues like the last round's with the least amount of effort.
This blog is an attempt to build up this list once and for all! It should include as much facts as possible, so I'm calling out for your help. If you've had trouble on a problem because of issues in the language, I invite you to help out contributing and write it as a comment. I'll update it in the list as soon as possible. You'd be helping out and preventing other people from going through the same annoying experience! I've included the very few facts I know about for now. It's not much, which is why anything you can share is useful!
Python
Python
correct, default recursion depth in python is just 1000. You can increase it by calling
sys.setrecursionlimit()
Test 108 of that Problem E is also an exploit of Codeforces system as well, as every execution of std::random_device gives the same output when compiling with MinGW w64.
Computing where n ≥ 0 and x > 0 are both integers as
ceil(n*1.0/x)
or asceil(double(n)/x)
may lead to precision issues resulting in WA which can be used as a hack case. So, its better to compute it as(n + x - 1) / x
.I use a macro for this as
#define iceil(n, x) (((n) + (x) - 1) / (x))
.I don't think precision issues are specific to any languages.
Oops!! Sorry. I thought he was collecting ideas for things which may lead to hacks. Anyways, it must not be included in the blog post but it can still be helpful to beginners. So, I'll leave the comment here as such.
This works for positives and negatives values:
#define myceil(i, j) (i/j + ( !(i<0 ^ j<0) && i%j!=0))
The negated xor expression is meant to skip the next part if the result of division is negative (no fix needed), otherwise it adds one to the result.
CustomTest gives 1600ms for line with ceil and 46ms for myceil. This is the test code:
Please note that without the dummy addition it will run in an instant due to compiler optimization. It's kind of messy, I know, but it works.
maybe this implementation will be useful too
Not really leading to hacks, more likely to fail pretests, but I feel I need to include this
In C++ multiset.erase erases all instances of object, not only one
Unfortunately I only realized it during the contest, and have a few friends who also were screwed up because of that
Well that's not entirely true. Say I want to erase an element
x
from the multisetms
. Writingms.erase(x)
would erase all instances ofx
but to erase only a single instance we can usems.erase(ms.find(x))
.Using Arrays.sort() on primitives has worst case O(n2).
You're legit retarded.
Please add pow
Should I say that size() in C++ retutns unsigned integer? There are many blogs complaining about Runtime error because of that.
eidan
Just updated the list with your suggestions. Thank you for cooperating! Any other idea, don't hesitate to share it!
The link in size() being unsigned in c++ is of the blog entry of pow function. Please update.
Acknowledged!
Undefined behaviour in C++
In python, there could be precision issues while using the built in log function. If you want to compute the floor of log2(258 - 1), you would get 58.000..01, while you should get 57.9999. When you take floor, you would get 58, instead of 57. This gave me a WA once.
Again, precision error are not language specific. You should be careful with them with every language.
This point doesn't sound bad. Do you have a blog or resource that backs it though? Some analysis on this issue would be a good complement.
What was the issue with
rand
in the "last round"? Can you give a link to a comment that says something about it?Solution required random values from (1,10^6) while c++ rand() gives only values till 32767. https://codeforces.net/blog/entry/65033?#comment-491350
There were several comments acknowledging the issue, not only one. The one provided by tusg25, is one of them, here is another.
Using any language that does not compile to native code may cause TLE :)
Auto comment: topic has been updated by eidan (previous revision, new revision, compare).