Link: http://codeforces.com/problemset/problem/450/ B
----------------------------------------------------------------------------------------------------------------------------------------------------------
Welcome to tianci Hut: http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
B. jzzhu and sequencestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
Jzzhu has sorted Ted a kind of sequences, they meet the following property:
You are givenXAndY, Please calculateFNModulo 1000000007 (109? +? 7 ).
Input
The first line contains two integersXAndY(|X| ,? |Y|? ≤? (109). The second line contains a single integerN(1? ≤?N? ≤? 2 · 109 ).
Output
Output a single integer representingFNModulo 1000000007 (109? +? 7 ).
Sample test (s) Input
2 33
Output
1
Input
0 -12
Output
1000000006
Note
In the first sample,F2? =?F1? +?F3, 3? =? 2? +?F3,F3? =? 1.
In the second sample,F2? = ?? -? 1 ;? -? 1 modulo (109? +? 7) equals (109? +? 6 ).
The Code is as follows:
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; struct a {int mat [2] [2] ;}; a D, F; __int64 N, MOD; a mul (A, a B) {A T; memset (T. mat, 0, sizeof (T. mat); For (INT I = 0; I <n; I ++) {for (int K = 0; k <n; k ++) {if (. mat [I] [k]) for (Int J = 0; j <n; j ++) {T. mat [I] [J] + =. mat [I] [k] * B. mat [k] [J]; T. mat [I] [J] % = mod ;}} return t;} A quickp (int K) {A P = D, M; memset (M. mat, 0, sizeof (M. mat); For (INT I = 0; I <n; ++ I) // unit matrix {M. mat [I] [I] = 1 ;}while (k) {If (K & 1) M = MUL (m, p); P = MUL (P, P ); k> = 1;} return m;} int main () {n = 2; int K, t ;__ int64 x, y, z; while (scanf ("% i64d % i64d", & X, & Y )! = EOF) {int S = 0; scanf ("% i64d", & Z); mod = 1000000007; If (Z = 1) {If (x <0) printf ("% i64d \ n", x + mod); else printf ("% i64d \ n", x); continue;} D. mat [0] [1] =-1; D. mat [1] [1] = 0; D. mat [0] [0] = D. mat [1] [0] = 1; A ret = quickp (Z-2); // Number of Z-2 multiplication _ int64 ans = (Ret. mat [0] [0] * Y % mod + ret. mat [0] [1] * x % mod) % MOD; If (ANS <0) ans + = MOD; printf ("% i64d \ n", ANS );} return 0 ;}
Codeforces round #258 (Div. 2) B. jzzhu and sequences (matrix fast power)