vyomgoyal098's blog

By vyomgoyal098, history, 5 weeks ago, In English

So I was solving the question https://mirror.codeforces.com/contest/1809/problem/B

While submitting I found that using #pragma GCC target("avx,avx2,fma") in giving WA 292802920 check line 4

while this code 292803134 is giving AC when I am not using #pragma GCC target("avx,avx2,fma")

So why is #pragma GCC target("avx,avx2,fma") this casuing issue? As far as I knew, using this the compiler would optimize the code for CPU utilization. But in this code the while using sqrt() function this is giving WA.

Any reason to explain the same???

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

»
5 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

skull

»
4 weeks ago, # |
  Vote: I like it +6 Vote: I do not like it

Using #pragma GCC target("avx,avx2,fma") optimizes your code for specific CPU instructions (AVX, AVX2, FMA) which can improve performance, but it may alter the behavior of certain functions like sqrt(). These optimizations can cause small precision differences in floating-point operations, leading to discrepancies in the results, especially for problems that require exact precision.

When you remove the pragma, the compiler defaults to safer optimizations that avoid these precision issues, resulting in more consistent and correct behavior for functions like sqrt(). In competitive programming, it's best to avoid such optimizations unless you're sure they won't affect correctness.

»
4 weeks ago, # |
  Vote: I like it +5 Vote: I do not like it

use sqrtl for higher precision