标签:tin lse namespace ret min simple 展开 first instead
For a given positive integer n, please find the smallest positive integer x that we can find an integer y such that y^2 = n +x^2.
Input
The first line is an integer T, which is the the number of cases.
Then T line followed each containing an integer n (1<=n <= 10^9).
Output
For each integer n, please print output the x in a single line, if x does not exit , print -1 instead.
2
2
3
-1
1
把这个式子移项,然后用平方差展开。
就会发现:(y-x)(y+x)=n
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int main(){
//freopen("1.in","r",stdin);
int t;scanf("%d",&t);
while(t--){
int n;scanf("%d",&n);
int ans=0x3f3f3f3f;
for(int i=1;i*i<=n;i++){
if(n%i==0){
int x=i,y=n/i;
int now=y-x;
//printf("%d\n",now);
if(now%2)continue;
if(now!=0)ans=min(ans,now/2);
}
}
if(ans==0x3f3f3f3f)printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}
标签:tin lse namespace ret min simple 展开 first instead
原文地址:https://www.cnblogs.com/Lour688/p/12958474.html