Pell方程小总结

保存大佬博客:

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值