保存大佬博客:
http://www.matrix67.com/blog/archives/5556
http://blog.sina.com.cn/s/blog_5d06e2390100ll92.html
http://m.blog.csdn.net/blog/wh2124335/8871535
连分数:
https://blog.csdn.net/qq_33737036/article/details/78199297
//求特解
#include <set>
#include <map>
#include <bitset>
#include <deque>
#include <stack>
#include <queue>
#include <time.h>
#include <vector>
#include <string>
#include <math.h>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define PI acos(-1.0)
#define ll long long
#define ull unsigned long long
using namespace std;
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;
}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;
}
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);
}
}
}
//已知特解求通解
x(n)=x(n-1)x(1)+dy(n-1)y(1);
y(n)=x(n-1)y(1)+y(n-1)x(1);