hdu 3292 No more tricks, Mr Nanguo

            佩尔方程+矩阵快速幂。先暴力求出佩尔方程的特解(最小解),然后利用佩尔方程的矩阵递推式,推出第k项。

            

#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<cmath>
#include<map>
#define LL long long
#define REP(i, n) for(int i = 0; i < n; i ++)
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
const int N = 2;
const int MOD = 8191;

struct Matrix
{
    int m[N][N];
}per, mul;

int x, y, d;

void search()
{
    y = 1;
    while(true)
    {
        x = sqrt((double)d * y * y + 1);
        if(x * x - d * y * y == 1)
            break;
        y ++;
    }
}

void init()
{
    mul.m[0][0] = x % MOD;
    mul.m[0][1] = d * y % MOD;
    mul.m[1][0] = y % MOD;
    mul.m[1][1] = x % MOD;
    REP(i, 2) REP(j, 2)
        per.m[i][j] = (i == j);
}

Matrix Mul(Matrix a, Matrix b)
{
    Matrix c;
    REP(i, 2) REP(j, 2)
    {
        c.m[i][j] = 0;
        REP(k, 2)
            c.m[i][j] += a.m[i][k] * b.m[k][j];
        c.m[i][j] %= MOD;
    }
    return c;
}

Matrix Pow(Matrix a, int b)
{
    Matrix ret = per;
    while(b)
    {
        if(b & 1)
        {
            ret = Mul(ret, a);
        }
        a = Mul(a, a);
        b >>= 1;
    }
    return ret;
}

int main()
{
    int k, ans;
    while(scanf("%d%d", &d, &k) != EOF)
    {
        int tmp = sqrt(d + 0.0);
        if(tmp * tmp == d)
        {
            puts("No answers can meet such conditions");
            continue;
        }
        search();
        init();
        Matrix ma = Pow(mul, k - 1);
        ans = ma.m[0][0] * x % MOD + ma.m[0][1] % MOD * y % MOD;
        printf("%d\n", ans % MOD);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值