一、连分数法求解最小正整数解。
自己看着公式敲的。。没有题目检验,但是暴力对拍了一会,应该是对的吧。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<vector>
#define ll long long
#define llu unsigned ll
using namespace std;
const int maxn=100010;
ll ans[maxn];
bool pell(ll n,ll &x,ll &y)
{
ll m=(ll)sqrt(n*1.0);
double sq=sqrt(n*1.0);
if(m*m==n) return false;
int cnt=0;
ans[++cnt]=m;
ll b=m,c=n-b*b;
do
{
ans[++cnt]=(ll)floor((sq+b)/c);
b=c*ans[cnt]-b;
c=(n-b*b)/c;
}while(ans[cnt]!=2*ans[1]);
ll p=1,q=0;
for(int i=cnt-1;i>=1;i--)
{
ll t=p;
p=q+p*ans[i];
q=t;
}
if((cnt-1)%2==0) x=p,y=q;
else x=2*p*p+1,y=2*p*q;
return true;
}
int main(void)
{
ll n,x,y;
while(scanf("%lld",&n)!=EOF)
{
if(pell(n,x,y)) printf("%lld %lld\n",x,y);
else printf("NO\n");
}
return 0;
}