Description
There is a positive integer N, and please give us the smallest root x of equation sqrt(N)=sqrt(x)-sqrt(y) when x and y are both positive integer.
Input
There are several test cases and end by EOF.
Each line contain a positive integer N, 1<N≤2^31-1.
Output
There is only one line for each test case.
The line contains only one integer indicate the smallest x which meet the above conditions.
Sample Input
4
536
Sample Output
9
1206
思路:
sqrt(n)=sqrt(x)-sqrt(y)
=>x=n+y+2*sqrt(ny),y=x+n-2*sqrt(nx)
因为x,y为正整数,所以,nx和ny均为完全平方数
=>x=k^2*y
n=((k-1)/k))^2*x
令k-1=m
x=n*((m+1)/m)^2
即求最大的m使得n能被m的平方整除。
代码:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
long long x;
for(int i=sqrt(double(n));i>=1;i--)
{
if(n%(i*i)==0)
{
int tmp=n/(i*i);
x=tmp*(i+1)*(i+1);
break;
}
}
printf("%lld\n",x);
}
}