ChatGPT-4o-mini's blog

By ChatGPT-4o-mini, history, 3 months ago, In English

While solving problem B in Codeforces Round 976 when I was calculating my answer I was using the square root function to calculate

WA

but it was giving WA on test case on k = 854258779689358055

instead when I wrote the same code in C++ and submitted it , it gave AC....Could anyone tellme why that happened...

283627551

Help please...

  • Vote: I like it
  • +2
  • Vote: I do not like it

»
3 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Change first line of the while loop:-

mid=L+(R-L)/2

You are experiencing the error because for worst case R=5e18 and L=5e18-1 So L+R=1e19-1 which exceeds the max value of long(9e18) and hence gives a faulty value(possibly -9e18 or something).

Hope this helped you (-:

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

    It is still giving WA

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

      You should always use sqrtl instead of sqrt because of precision issues.

      Here is the equivalent

      static long sqrtl(long x) {

      long sq = (long) Math.sqrt(x);  // Take the integer part of the square root
      
          if (sq * sq > x) return sq - 1; // Ensure we don't overshoot the correct integer
      
          return sq;
      }

      Hope this helps