Codeforces and Polygon may be unavailable from December 6, 19:00 (UTC) to December 6, 21:00 (UTC) due to technical maintenance. ×

atulcf24's blog

By atulcf24, 13 hours ago, In English

adding floating point numbers in sqrt make them more accuracte

i submitted a solution containing

 int sq = sqrt(sum);
            if (sq * sq == sum && sq % 2 == 1) cnt++;

although this seems perfectly fine, it doesn't pass the test case listed below

`14'

'1 10 10 100 1 1 10 1 10 2 10 2 10 1`

this must return 3 as the first,fourth and last number fit the condition but returns 2

the issue with it is that sqrt works well with floating point numbers this code fixes the rounding issues

long long sq = (long long)sqrt(sum + 0.5); // Add 0.5 to handle rounding issues
if (sq * sq == sum && sq % 2 == 1) cnt++;

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

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

Auto comment: topic has been updated by atulcf24 (previous revision, new revision, compare).

»
13 hours ago, # |
  Vote: I like it -10 Vote: I do not like it

use sqrtl for numbers that exceeds the int limit for higher precision

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

My code was working with typecasting and without the need for approximation