http://www.lightoj.com/volume_showproblem.php?problem=1065
题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - 1) +f(n - 2) 求f(n)
思路:给出了递推式就是水题。
/** @Date : 2016-12-17-15.54 * @Author : Lweleth (SoungEarlf@gmail.com) * @Link : https://github.com/ * @Version : */ #include<bits/stdc++.h> #define LL long long #define PII pair #define MP(x, y) make_pair((x),(y)) #define fi first #define se second #define PB(x) push_back((x)) #define MMG(x) memset((x), -1,sizeof(x)) #define MMF(x) memset((x),0,sizeof(x)) #define MMI(x) memset((x), INF, sizeof(x)) using namespace std; const int INF = 0x3f3f3f3f; const int N = 1e5+20; const double eps = 1e-8; struct matrix { LL mat[2][2]; void init() { mat[0][0] = mat[1][0] = mat[0][1] = mat[1][1] = 0; } }; matrix mul(matrix a, matrix b, LL mod) { matrix c; c.init(); for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) for(int k = 0; k < 2; k++) { c.mat[i][j] += a.mat[i][k] * b.mat[k][j]; c.mat[i][j] %= mod; } return c; } matrix fpow(matrix x, LL n, LL mod) { matrix r; r.init(); for(int i = 0; i < 2; i++) r.mat[i][i] = 1; while(n > 0) { if(n & 1) r = mul(r, x, mod); x = mul(x, x, mod); n >>= 1; } return r; } LL Tis(LL x, LL a, LL b, LL mod) { matrix t; t.init(); t.mat[0][0] = 1; t.mat[0][1] = 1; t.mat[1][0] = 1; matrix s; s = fpow(t, x - 1, mod); LL ans = s.mat[0][0]*b + s.mat[1][0]*a; return ans % mod; } int main() { int T; cin >> T; int cnt = 0; while(T--) { LL n, a, b, m; scanf("%lld%lld%lld%lld", &a, &b, &n, &m); LL x = 1; while(m--) { x*=10; } printf("Case %d: %lld ",++cnt, Tis(n, a, b, x)); } return 0; }