malviyanshiv's blog

By malviyanshiv, history, 5 years 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 ?

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
5 years 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 years 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 years 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?

      • »
        »
        »
        »
        5 years 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..