HDU 3292 Pell方程第k小解

2 篇文章 0 订阅

HDU 3292 Pell方程第k小解

链接:HDU3292

方法:Pell方程递推式 + 矩阵快速幂

AC code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 8191;
const int NUM = 2;
int N = 2;
struct mat{
    int a[NUM][NUM];
    void init(){
        memset(a, 0, sizeof(a));
        for(int i = 0; i < N; ++i) a[i][i] = 1;
    }
    mat operator * (const mat &b) const{
        mat c;
        for(int i = 0; i < N; ++i){
            for(int j = 0; j < N; ++j){
                c.a[i][j] = 0;
                for(int k = 0; k < N; ++k){
                    c.a[i][j] = (c.a[i][j] + (long long)a[i][k] * b.a[k][j] % mod) % mod;
                }
            }
        }
        return c;
    }
};
mat fast_pow(mat a, int b){
    mat res;
    res.init();
    while(b > 0){
        if(b & 1){
            res = res * a;
        }
        a = a * a;
        b >>= 1;
    }
    return res;
}

/*

[ xn+1 ] = [x0  d * y0] [ xn ]
[ yn+1 ] = [y0  x0]     [ yn ]
*/
inline bool issq(int d){
    int sq = floor(sqrt(d + 0.5));
    return (sq * sq == d);
}
void Find(int &x, int &y, int d){
    y = 1;
    while(1){
        x = floor(sqrt((long long)d * y * y + 1.5));
        if((ll)x * x == (long long)d * y * y + 1LL) break;
        ++y;
    }
}
int main(){
    int d, k;
    while(cin >> d >> k){
        if(issq(d)){
            puts("No answers can meet such conditions");
            continue ;
        }
        int x, y;
        Find(x, y, d);
        if(k == 1){
            printf("%d\n", x);
            continue ;
        }
        mat A;
        A.a[0][0] = x % mod, A.a[0][1] = (ll)d * y % mod;
        A.a[1][0] = y % mod, A.a[1][1] = x % mod;
        A = fast_pow(A, k - 1);
        int ansx = ((ll)A.a[0][0] * x  % mod + (ll)A.a[0][1]  * y % mod) % mod;
        printf("%d\n", ansx);
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值