标签:typedef scanf pre \n minimum using names type lse
1.连分数
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
using namespace std;
typedef long long ll;
ll a[20000];
bool pell_minimum_solution(ll n,ll &x0,ll &y0){
ll m=(ll)sqrt((double)n);
double sq=sqrt(n);
int i=0;
if(m*m==n)return false;//当n是完全平方数则佩尔方程无解
a[i++]=m;
ll b=m,c=1;
double tmp;
do{
c=(n-b*b)/c;
tmp=(sq+b)/c;
a[i++]=(ll)(floor(tmp));
b=a[i-1]*c-b;
//printf("%lld %lld %lld\n",a[i-1],b,c);
}while(a[i-1]!=2*a[0]);
ll p=1,q=0;
for(int j=i-2;j>=0;j--){
ll t=p;
p=q+p*a[j];
q=t;
//printf("a[%d]=%lld %lld %lld\n",j,a[j],p,q);
}
if((i-1)%2==0){x0=p;y0=q;}
else{x0=2*p*p+1;y0=2*p*q;}
return true;
}
int main(){
ll n,x,y;
while(~scanf("%lld",&n)){
if(pell_minimum_solution(n,x,y)){
printf("%lld^2-%lld*%lld^2=1\t",x,n,y);
printf("%lld-%lld=1\n",x*x,n*y*y);
}
}
2.暴力
递推式
x[n]=x[n-1]*x[1]+d*y[n-1]*y[1];
y[n]=x[n-1]*y[1]+y[n-1]*x[1];
标签:typedef scanf pre \n minimum using names type lse
原文地址:http://blog.51cto.com/14093713/2323468