Please read the new rule regarding the restriction on the use of AI tools. ×

yud08's blog

By yud08, history, 4 hours ago, In English

So I was trying to blitz my way through ABC in the last Div 2 as per usual, when, shockingly, I received the verdict "Wrong answer on test 8" for B(actually, a quite regular occurrence). I just thought it was an issue with the sqrt() function, so I coded a binary search version for the sqrt and moved on. However, after the contest, I resubmitted in C++17(I was using C++20), and it somehow passes? Also, when prompted by my friend, I added #pragma GCC target("avx2") to that submission, and it somehow failed again. Could someone please explain what's happening here?

Failing submission(C++20)
Accepted submission(C++17)
Failing submission(with pragma)

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

»
4 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

had the same issue, will just never trust floating point math again lol

»
4 hours ago, # |
  Vote: I like it 0 Vote: I do not like it

poggers

»
4 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

Use sqrtl for long long i use it for your first falling submission and got AC

»
3 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

It's basically just a rounding error. C++20 believes for some reason that sqrt(854258780613619262) = 924261208, even though 924261208 * 924261208 = 854258780613619264 (the difference is only two). Here are the two submissions which I used to debug that. However, after checking the value of sqrt(854258780613619262) in a separate submission under C++17, it also prints out the wrong value, which makes even less sense.

»
55 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

I think the problem is with floating point. Therefore, I used $$$n=i^2+x$$$, where $$$x \le 2k+1$$$, for each $$$\lfloor\sqrt{k}\rfloor\le i\le k$$$ until I get the minimum. And it indeed worked. So, I think the problem is with floating points.

»
28 minutes ago, # |
  Vote: I like it 0 Vote: I do not like it

I too have encountered the same. And that's the reason I've permanently stuck to using C++ 17.

In the B problem of this contest, I just used floor(sqrt(num)) in C++ 17 and it worked fine for me.