标签:
题目链接:http://poj.org/problem?id=3070
code:
//矩阵快速幂 #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #define Mod 10000 using namespace std; typedef struct juzhen{ int a11,a12,a21,a22; }JZ; JZ quickpow(JZ s,int n) { JZ base = s; // printf("%d %d\n%d %d\n",base.a11,base.a12,base.a21,base.a22); int ta11,ta12,ta21,ta22; s.a11 = s.a22 = 1; s.a12 = s.a21 = 0; while(n) { if((n & 1)) { ta11 = s.a11*base.a11 + s.a12*base.a21; ta12 = s.a11*base.a12 + s.a12*base.a22; ta21 = s.a21*base.a11 + s.a22*base.a21; ta22 = s.a21*base.a12 + s.a22*base.a22; s.a11 = ta11 % Mod; s.a12 = ta12 % Mod; s.a21 = ta21 % Mod; s.a22 = ta22 % Mod; } ta11 = base.a11*base.a11 + base.a12*base.a21; ta12 = base.a11*base.a12 + base.a12*base.a22; ta21 = base.a21*base.a11 + base.a22*base.a21; ta22 = base.a21*base.a12 + base.a22*base.a22; base.a11 = ta11 % Mod; base.a12 = ta12 % Mod; base.a21 = ta21 % Mod; base.a22 = ta22 % Mod; n >>= 1; } // printf("%d %d\n%d %d\n",s.a11,s.a12,s.a21,s.a22); return s; } int main() { int n; JZ s,e; s.a11 = s.a12 = s.a21 = 1; s.a22 = 0; while(cin >> n && n!=-1) { e = quickpow(s,n); //printf("%d %d\n%d %d\n",e.a11,e.a12,e.a21,e.a22); cout << e.a12 << endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangjialu2015/p/5499379.html