题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=1005
Number Sequence
Description
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n).
Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output
For each test case, print the value of f(n) on a single line.
Sample Input
1 1 3
1 2 10
0 0 0
Sample Output
2
5
矩阵快速幂。。
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<map> using std::map; using std::min; using std::find; using std::pair; using std::vector; using std::multimap; #define pb(e) push_back(e) #define sz(c) (int)(c).size() #define mp(a, b) make_pair(a, b) #define all(c) (c).begin(), (c).end() #define iter(c) __typeof((c).begin()) #define cls(arr, val) memset(arr, val, sizeof(arr)) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define rep(i, n) for(int i = 0; i < (int)n; i++) #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i) const int M = 7; const int N = 100001; const int INF = 0x3f3f3f3f; struct Matrix { typedef vector<int> vec; typedef vector<vec> mat; inline mat mul(mat &A, mat &B) { mat C(sz(A), vec(sz(B[0]))); rep(i, sz(A)) { rep(k, sz(B)) { rep(j, sz(B[0])) { C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M; } } } return C; } inline mat pow(mat &A, int n) { mat ret(sz(A), vec(sz(A[0]))); rep(i, sz(A)) ret[i][i] = 1; while(n) { if(n & 1) ret = mul(ret, A); A = mul(A, A); n >>= 1; } return ret; } inline void solve(int a, int b, int n) { mat ans(2, vec(2)); ans[0][0] = a, ans[0][1] = b; ans[1][0] = 1, ans[1][1] = 0; ans = pow(ans, n - 2); printf("%d ", (ans[0][0] + ans[0][1]) % M); } }go; int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif int a, b, n; while(~scanf("%d %d %d", &a, &b, &n), a + b + n) { if(1 == n || 2 == n) { puts("1"); continue; } go.solve(a, b, n); } return 0; }