https://mirror.codeforces.com/contest/1389/submission/88316090
In the above solution of Educational Codeforces Round 92 for problem A,there is a loop used inside testcase loop.
So,I tried to hack it with 10000 testcases of 500000001 1000000000. According to this test case ,there will be 10000*5*pow(10,8) operations but still I don't get successful tle hack.Even my ide got crashed at this input but still codeforces did not give a tle.
Can anyone help me by telling why is it so?
This is because of compiler optimization. The compiler its self detected that it doesn't have to enter all the loop to find the answer. It detects that it have to enter for some few operations and if it didn't find an answer, then it breaks out early. Or, the compiler detected a pattern which can get the answer nearly instantly. In both cases, those are compiler optimizations. I have added volatile keyword to the i in the for loop, and it took around 700ms in one of these huge testcases( volatile keyword prevents compiler optimizations).
So, You mean if I am giving same testcase 10000 times then compiler will find a pattern for that pattern and will not give a TLE?
Nope, the same testcase it won't find a pattern. The pattern or breaking early is in the loop its self.
This is the loop which will run 5*pow(10,8) times if i1=500000001 and i2=1000000000, there is no answer for this test case,So IF condition will never be true and loop will never break itself.
i2-i2%i1>i1 will always produce an answer in the first iteration if an answer exists. If there is no answer, it automatically breaks. No need to continue through the loop. That is what C++ compiler detects.
See this submission here. I made the loop run only once and it got Accepted.
The compiler (GCC 10.1.0, with -O3) managed to optimize it into:
The compiler deduced that the loop is useless and optimized it away.
Can you please tell how you are getting this kind of output? This doesn't seem like Godbolt.
It's the output from
gcc -fdump-tree-optimize
. For example,g++ -O2 -fdump-tree-optimize hw.cc
will produce ahw.cc.234t.optimized
file, which is in "GIMPLE" (GCC IR) and can be viewed with a text editor.If you're on Godbolt you can also view it:
Add new...
button.GCC Tree/RTL output
.234t.optimized
inSelect a pass...
droplist.(The pass number
234t
may vary in different GCC versions.)The loop variable (i) is never used so every iteration is exactly the same (and there are no side effects unless the if is true which can only occur once due to break). Therefore the compiler can prove it is safe to remove the loop and leave you with a single if statement.