题目链接题意:求sqrt(sqrt(2)+sqrt(3))^2nMOD1024思路:代码:#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>usingnamespacestd;constint"/>

HDU2256-Problem of Precision(矩阵构造+高速幂)

Posted mfmdaoyou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU2256-Problem of Precision(矩阵构造+高速幂)相关的知识,希望对你有一定的参考价值。

php?

pid=2256">题目链接


题意:求sqrt(sqrt(2) + sqrt(3)) ^ 2n MOD 1024

思路:

技术分享

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;


const int MOD = 1024;

int n;

struct mat {
    int s[2][2];
    mat(int a = 0, int b = 0, int c = 0, int d = 0) {
        s[0][0] = a; 
        s[0][1] = b; 
        s[1][0] = c; 
        s[1][1] = d; 
    }
    mat operator * (const mat& c) {
        mat ans; 
        sizeof(ans.s, 0, sizeof(ans.s));
        for (int i = 0; i < 2; i++) 
            for (int j = 0; j < 2; j++)
                for (int k = 0; k < 2; k++)
                    ans.s[i][j] = (ans.s[i][j] + s[i][k] * c.s[k][j]) % MOD;
        return ans;
    }
}tmp(5, 12, 2, 5);

mat pow_mod(int k) {
    if (k == 1)
        return tmp;
    mat a = pow_mod(k / 2);
    mat ans = a * a;
    if (k % 2)
        ans = ans * tmp;
    return ans;
}

int main() {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%d", &n); 
        mat ans = pow_mod(n); 
        printf("%d\n", (ans.s[0][0] * 2 - 1) % MOD); 
    }
    return 0;
}






以上是关于HDU2256-Problem of Precision(矩阵构造+高速幂)的主要内容,如果未能解决你的问题,请参考以下文章

HDU2256-Problem of Precision(矩阵构造+高速幂)

hdu-2256 Problem of Precision---矩阵快速幂+数学技巧

HDU 2256 Problem of Precision 数论矩阵快速幂

HDU 2256 Problem of Precision(矩阵快速幂)

HDU2110 Crisis of HDU母函数

HDU 2108 Shape of HDU