In Codeforces Round #538 (Div. 2), I submitted the solution for problem C and got WA on test case 4 ( solution link ). I checked it offline and it gave correct output for test case 4. I also checked it on online compiler ideone, and it also gave the correct result. I tried submitting in different versions of C++ ( an illogical attempt and so )... failed. Please help me to find the cause of ambiguity ?
I tried several changes, and come up with that the bug is in
log2(n)/log2(b)
(and also the one below it in the code) and when n = b, then the result of the division will be something like 0.99999... which will be 0 after casting tolong long
. So, you should usefloor()
orceil()
according to what you want.The code does work on other online compilers and also it works offline with same GCC version.
Yeah, but in these cases, we should take the worse behaviour. We should know that
double
may lose precision while processing it with integer types, so why to not be careful while using it?Yup you are right but that should not cause different results on different platform. My main problem is the cause of difference..