A Simple Problem

Posted lour688

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了A Simple Problem相关的知识,希望对你有一定的参考价值。

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.

Sample Input

2
2
3

Sample Output

-1
1

把这个式子移项,然后用平方差展开。
就会发现:(y-x)(y+x)=n

Code

#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
",now);
                if(now%2)continue;
                if(now!=0)ans=min(ans,now/2);
            }
        }
        if(ans==0x3f3f3f3f)printf("-1
");
        else printf("%d
",ans);    
    }
    return 0;
}






以上是关于A Simple Problem的主要内容,如果未能解决你的问题,请参考以下文章

POJ-3468-A Simple Problem with integers

poj 3468 A Simple Problem with Integers

A Simple Problem with Integers-POJ3468

POJ 3468 A Simple Problem with Integers

POJ 3468 A Simple Problem with Integers (线段树)

poj 3468 : A Simple Problem with Integers 线段树 区间修改