wrong ans in case #3

Code removed

*Edited by author 20.08.2019 21:12*

Re: wrong ans in case #3

8*n-7 <--- integer overflow

Re: wrong ans in case #3

Integer overflow. But why? And How?

*Edited by author 17.08.2019 19:12*

Re: wrong ans in case #3

#include <iostream>

#include <cmath>

using namespace std;

bool per_sq(int n) {

long double x=sqrt(n);

return ((x-floor(x))?0:1);

}

int main() {

int t;

cin>>t;

while(t--) {

long int n;

cin>>n;

if(per_sq(8*n-7)) cout<<"1 ";

else cout<<"0 ";

}

}

This code gets wrong answer in case #3.

*Edited by author 18.08.2019 20:49*

*Edited by author 18.08.2019 20:49*

Re: wrong ans in case #3

On timus 'long int' is equal to 'int', to use 64-bit integers you need to write 'long long'.

Also, there is a precision error in your 'per_sq' function. This line:

return ((x-floor(x))?0:1);

must be written as

return (abs(x-floor(x)) > 1e-8 ? 0 : 1);

Re: wrong ans in case #3

Thanks. I didn't know about the bit size of long in Timus. And what you said about the function. I had problem in the argument. I used double instead of int then it got accepted.