ManasXD's blog

By ManasXD, 4 months ago, In English

Problem Link : 1995C - Squaring

I have tried to implement the float method from the editorial.

Editorial solution

I am using base 2 for log here. In the first code, I am calculating the difference of b[i-1] and b[i], tmp is the number of times log(2)=1 has to be added into b[i]. If b[i-1]-b[i]>0, We are adding it's ceil value to the total operations ( ops ) and also to b[i] itself.

However this solution gives wrong answer on test-case 2, my guess is my outputs are bigger than answer expected.

Note: I have removed input templates from codes to make them short, rest is intact. You can also refer to the submission links.

Code with wrong answer

code with ceil()

submission : 272991530

While this code works

working code

submission : 272991530

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

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

    Then what is the solution, how to avoid such errors.

    I am really curious about how the second approach avoids floating point errors.

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

      I don't trust FP in this problem either. I'd rather do a full integer-based solution.

      For this one, the eps used in AC solution helped a little bit in circumventing. TL;DR there might be cases where the "equal" comparison for FP numbers aren't accurate, somehow making two identical number having slight errors (turning $$$0$$$ into $$$10^{-15}$$$ or something that similarly low, pushing ceil to $$$1$$$). eps sets a threshold for ignored error.