Codeforces Round #257 (Div. 2) Fast Power of B matrix,
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 bytes + limit 7 ).
Input
The first line contains two integersXAndY(|X|, Bytes |Y| Limit ≤ limit 109). The second line contains a single integerN(1 digit ≤ DigitNLimit ≤ limit 2-109 ).
Output
Output a single integer representingFNModulo 1000000007 (109 bytes + limit 7 ).
Sample test (s) input
2 33
Output
1
Input
0 -12
Output
1000000006
Note
In the first sample,F2 bytes = bytesF1 worker + workerF3, 3 rows = second 2 rows + secondF3,F3 rows = rows 1.
In the second sample,F2 bytes = average-cost 1; average-cost 1 modulo (109 bytes + limit 7) equals (109 bytes + limit 6 ).
Question
A simple question of the progressive method, I used the Matrix to quickly power it.
The constructor is also simple.
F1 + f3 = f2
F3 = f2-f1
F3 = 1 * f2 +-1 * f1
F2 = 1 * f2 + 0 * f1
So the matrix is
1,-1
1, 0
Then the modulo operation and negative number processing are completed. Negative modulo returns a negative number. Because I am writing a fast power, the number returned at last must be between (-Mod, Mod), so I just need to add a Mod directly and then take the modulo again.
Sample Code
/*************************************** **************************************** Copyright notice * Copyright (c) 2014 All rights reserved * ---- Stay Hungry Stay Foolish ---- ** @ author: Shen * @ name: B * @ file: G: \ My Source Code \ [ACM] competition \ 0719-CF \ B. cpp * @ date: 2014/07/19 20:57 * @ algorithm: matrix Fast Power Method ************************************ **************************************** **/# include <cst Dio >#include <string >#include <cstring >#include <iostream >#include <algorithm> using namespace std; typedef long int64; const int MAXN = 2; const int MAXM = 2; const int Mod = 1000000007; struct Matrax {int n, m; int64 mat [MAXN] [MAXM]; Matrax (): n (-1 ), m (-1) {} Matrax (int _ n, int _ m): n (_ n), m (_ m) {memset (mat, 0, sizeof (mat);} void Unit (int _ s) {n = _ s; m = _ s; for (int I = 0; I <n; I ++) {for (in T j = 0; j <n; j ++) {mat [I] [j] = (I = j )? 1: 0 ;}}void print () {printf ("n = % d, m = % d \ n", n, m); for (int I = 0; I <n; I ++) {for (int j = 0; j <m; j ++) printf ("% 8d", mat [I] [j]); printf ("\ n") ;}}; Matrax add_mod (const Matrax & a, const Matrax & B, const int64 mod) {Matrax ans (. n,. m); for (int I = 0; I <. n; I ++) {for (int j = 0; j <. m; j ++) {ans. mat [I] [j] = (. mat [I] [j] + B. mat [I] [j]) % mod ;}return ans ;}matrax mul (const Matrax & a, const Matrax & B) {Matrax ans (. n, B. m); for (int I = 0; I <. n; I ++) {for (int j = 0; j <B. m; j ++) {int64 tmp = 0; for (int k = 0; k <. m; k ++) {tmp + =. mat [I] [k] * B. mat [k] [j];} ans. mat [I] [j] = tmp;} return ans;} Matrax mul_mod (const Matrax & a, const Matrax & B, const int mod) {Matrax ans (. n, B. m); for (int I = 0; I <. n; I ++) {for (int j = 0; j <B. m; j ++) {int64 tmp = 0; for (int k = 0; k <. m; k ++) {tmp + = (. mat [I] [k] * B. mat [k] [j]) % mod;} ans. mat [I] [j] = tmp % mod;} return ans;} Matrax pow_mod (const Matrax & a, int64 k, const int mod) {Matrax p (. n,. m), ans (. n,. m); p = a; ans. unit (. n); if (k = 0) return ans; else if (k = 1) return a; else {while (k) {if (k & 1) {ans = mul_mod (ans, p, mod); k --;} else {k/= 2; p = mul_mod (p, p, mod );}} return ans ;}} int64 x, y, n, res; void solve () {cin >>> x >>> y >> n; if (n = 1) res = x; else if (n = 2) res = y; else {Matrax ans (2, 1); // tmp = cef ^ (n-2 ); // ans = tmp * beg; // res = ans. mat [0] [0]; Matrax cef (2, 2); cef. mat [0] [0] = 1; cef. mat [0] [1] =-1; cef. mat [1] [0] = 1; cef. mat [1] [1] = 0; // cef. print (); Matrax beg (2, 1); beg. mat [0] [0] = y; beg. mat [1] [0] = x; Matrax tmp (2, 2); tmp = pow_mod (cef, n-2, Mod); // tmp. print (); ans = mul_mod (tmp, beg, Mod); // ans. print (); res = ans. mat [0] [0];} if (res <0) res + = Mod; cout <res <endl;} int main () {solve (); return 0 ;}