malviyan_shiv's blog

By malviyan_shiv, history, 5 days ago, In English,

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 ?

 
 
 
 

»
5 days ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

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 to long long. So, you should use floor() or ceil() according to what you want.

  • »
    »
    5 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The code does work on other online compilers and also it works offline with same GCC version.

    • »
      »
      »
      5 days ago, # ^ |
      Rev. 3   Vote: I like it +5 Vote: I do not like it

      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?

      • »
        »
        »
        »
        4 days ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Yup you are right but that should not cause different results on different platform. My main problem is the cause of difference..