HDU-2256(矩阵快速幂)

来源:互联网 发布:简单软件编程教程 编辑:程序博客网 时间:2024/05/22 11:53

 
Input
The first line of input gives the number of cases, T. T test cases follow, each on a separate line. Each test case contains one positive integer n. (1 <= n <= 10^9) 
Output
For each input case, you should output the answer in one line. 
Sample Input
3125
Sample Output
997841


/*题意:求{[sqrt(2)+sqrt(3)]^2n}%1024取整题解:见上图*/#include <stdio.h>#include <string.h>#include<iostream>#include <algorithm>using namespace std;#define LL __int64struct node{LL maxtri[6][6];};node res, roi;LL n, kk = 1024;node operator * (node &a, node &b){node ans;for (int i = 0; i < 2; i++){for (int j = 0; j < 2; j++){ans.maxtri[i][j] = 0;for (int k = 0; k < 2; k++){ans.maxtri[i][j] += a.maxtri[i][k] * (b.maxtri[k][j]%kk)%kk;ans.maxtri[i][j] %= kk;}}}return ans;}void quickmuti(LL nn){node temp;for (int i = 0; i < 2; i++){for (int j = 0; j < 2; j++){temp.maxtri[i][j] = (i == j);}}while (nn){if (nn & 1){temp = temp*roi;}nn >>= 1;roi = roi*roi;}res = temp;}void init(){memset(roi.maxtri, 0, sizeof(roi.maxtri));memset(res.maxtri, 0, sizeof(res.maxtri));roi.maxtri[0][0] = roi.maxtri[1][1] = 5;roi.maxtri[1][0] = 2;roi.maxtri[0][1] = 12;res.maxtri[0][0] = 5;res.maxtri[1][0] = 2;}int main(){int t;cin >> t;while (t--){scanf("%I64d", &n);int num[3] = { 1,1,2 };if (n == 1){cout << '9' << endl;continue;}init();quickmuti(n-1);LL ans = 0;ans = (LL)(res.maxtri[0][0]*5 + 2*res.maxtri[0][1])%kk;printf("%I64d\n", (2*ans-1)%kk);}return 0;}




原创粉丝点击