hihocoder #1143 : 骨牌覆盖问题·一
http://hihocoder.com/problemset/problem/1143 斐波那契数列,快速矩阵幂解法 #include <cstdio> #include <cstring> #include <vector> using namespace std; const int N = 19999997; void copy(long long a[2][2],long long b[2][2]) { for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) a[i][j] = b[i][j]; } void calc(long long a[2][2],long long b[2][2]) { long long c[2][2]={0}; c[0][0] = (a[0][0]*b[0][0]+a[0][1]*b[1][0])%N; c[0][1] = (a[0][0]*b[0][1]+a[0][1]*b[1][1])%N; c[1][0] = (a[1][0]*b[0][0]+a[1][1]*b[1][0])%N; c[1][1] = (a[1][0]*b[0][1]+a[1][1]*b[1][1])%N; copy(a,c); } int main() { int n; while(scanf("%d",&n)!=EOF) { long long c[2][2]={0,1,1,1}; long long res[2] = {1,1}; int flag = 0; for(int k = 0; (1<<k) <= n;k++) { if(n & (1<<k)) { long long u = (res[0]*c[0][0]+res[1]*c[0][1])%N; long long v = (res[0]*c[1][0]+res[1]*c[1][1])%N; res[0] = u; res[1] = v; } calc(c,c); } printf("%lld\n",res[0]); } return 0; }
posted on 2015-11-20 15:46 zyz913614263 阅读(337) 评论(0) 编辑 收藏 举报