ScorpioDagger's blog

By ScorpioDagger, 2 years ago, In English

Hello mates. In the recently finished contest, regarding problem B Ela's Fitness and the Luxury Number,I submitted it using cpp 20 64-bit compiler, I got 4 WA verdicts which had cost me a lot of penalty till I solved it(pure coincidence as I tried int128 instead of ll then knew from the compiler error that type-casting is the problem so type-casted the sqrt() argument into long double and AC)

After the contest, I looked over the submissions and saw that many got it AC by cpp 17 compiler so tried my EXACT WA4 code ,but changing the compiler from cpp20 to cpp17. Magically, the same exact code was AC by cpp 17. I concluded the issue is with cpp 20's sqrt() fn, its argument should be type-casted to long double or rather use sqrtl(), but I wanna elaboration on why that happened and how not to fall in that on next contests ?? Here are my submissions: 175013082 The one Finally AC during contest after 10 tries, 175001140 WA pretest 4, 175043791 same WA4 code but AC by cpp17

UPD: It was really a well prepared contest that it rose awareness to avoid using built_in functions like sqrt() or even sqrtl() as they depend on floating pt calculation, I found this blog which is an absolute gem discussing the alternatives to those fns.

I tried implementing sqrt()fn myself using binary search returning an integer as in 175151224 and indeed got AC by cpp20 without any further type-casting avoiding any precision errors. Its complexity is logarithmic and accuracy is guaranteed.

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

»
2 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Hey , I have checked for the square root https://mirror.codeforces.com/contest/1737/submission/175044526 it is giving AC in sqrtl and it is giving WA https://mirror.codeforces.com/contest/1737/submission/175047080 What is The difference ??

  • »
    »
    2 years ago, # ^ |
    Rev. 2   Vote: I like it +1 Vote: I do not like it

    Precision issues. sqrtl() deals with the argument as long double. On the other hand, sqrt() deals with it as double, so precision is important for an accurate answer Mine worked with sqrt() in cpp20 because i type casted the argument into long double which is equivalent to using sqrtl(), the weird part is that the code with sqrt() and no type casting was ACC normally by cpp 17, but WA for cpp20, so it was pure luck that some people got it AC from first try and some kept getting WA4 !

»
2 years ago, # |
  Vote: I like it +1 Vote: I do not like it

Yeah, I fixed the same C++20 code link

»
2 years ago, # |
  Vote: I like it +5 Vote: I do not like it