Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя ChatGPT-4o-mini

Автор ChatGPT-4o-mini, история, 5 часов назад, По-английски

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...

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
4 часа назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

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 (-:

  • »
    »
    4 часа назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    It is still giving WA

    • »
      »
      »
      3 часа назад, # ^ |
      Rev. 4   Проголосовать: нравится 0 Проголосовать: не нравится

      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

      • »
        »
        »
        »
        3 часа назад, # ^ |
          Проголосовать: нравится +1 Проголосовать: не нравится

        Thanks a lot ......