A Short Problem
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 344 accepted submission (s): 131
Problem description according to a research, VIM users tend to have shorter fingers, compared with Emacs users.
Hence they prefer problems short, too. Here is a short one:
Given N (1 <=n <= 1018), You shoshould solve
G (N) mod 109 + 7
Where
G (n) = 3G (n-1) + g (n-2)
G (1) = 1
G (0) = 0
Input there are several test cases. For each test case there is an integer N in a single line.
Please process until EOF (end of file ).
Output for each test case, please print a single line with a integer, the corresponding answer to this case.
Sample input0 1 2
Sample output0 1 42837
Source2012 ACM/ICPC Asia Regional Chengdu online
Recommendliuyiding
I didn't even make such a water question competition...
Sorry !!!!
In fact, as long as the model is used, you can find the loop section.
The first time is MoD = 1000000007 find the loop section is 222222224
The second time is MoD = 222222224, find the cycle section 183120
You can find the cyclic festival brute force, and the matrix multiplication method is slower.
Attached cycle searching sectionProgram: View code
# Include <stdio. h> Const Long Long MoD = 222222224 ; // The first time is MoD = 1000000007 find the loop section is 222222224 // The second time is MoD = 222222224, find the cycle section 183120 Int Main (){ Long Long A, B; = 1 ; B = 3 ; For ( Int I = 1 ; I ++ ){ If (A = 0 & B =1 ) {Printf ( " % D \ n " , I ); Break ;} Long Long C = 3 * B + A; c % = MOD; = B; B = C ;} Return 0 ;}
The following is a program.
// 1004 # Include <stdio. h> # Include <Iostream> # Include <Map> # Include < Set > # Include <Algorithm> # Include < String . H> # Include <Stdlib. h> Using Namespace STD; Const Long Long MoD = 1000000007 ; Const Long Long Mod2 = 222222224 ; Const Long Long Mod3 =183120 ; Const Int Maxn = 10 ; Struct Matrix { Long Long Mat [maxn] [maxn]; Int N, m;}; matrix MUL (matrix A, matrix B, Long Long M) {matrix ret; ret. n =A. N; ret. m = B. m; For ( Int I = 0 ; I <A. N; I ++ ) For ( Int J = 0 ; J <B. m; j ++ ) {Ret. Mat [I] [J] = 0 ; For ( Int K =0 ; K <a. m; k ++ ) {Ret. Mat [I] [J] + = (A. Mat [I] [k] * B. Mat [k] [J] % M); ret. Mat [I] [J] % = M ;}} Return RET;} matrix POW (matrix, Long Long N, Long Long M ){ If (N = 1 )Return A; matrix RET = A; matrix temp = A; For ( Int I = 0 ; I <A. N; I ++ ) For ( Int J = 0 ; J <A. N; j ++ ){ If (I = J) ret. Mat [I] [J] = 1 ; Else Ret. Mat [I] [J] = 0 ;} While (N ){ If (N & 1 ) Ret = Mul (Ret, temp, m); temp = Mul (temp, temp, m); n >>= 1 ;} Return RET ;} Int Main (){ // Freopen ("D. In", "r", stdin ); // Freopen ("D. Out", "W", stdout ); Matrix A; A. N = A. m = 2 ; A. Mat [ 0 ] [ 0 ] = 3 ; A. Mat [ 0 ] [ 1 ] =1 ; A. Mat [ 1 ] [ 0 ] = 1 ; A. Mat [ 1 ] [ 1 ] = 0 ; Matrix F0; f0.n = 2 ; F0.m = 1 ; F0.mat [ 0 ] [0 ] = 1 ; F0.mat [ 1 ] [ 0 ] = 0 ; Long Long N; matrix TMP; matrix temp; While (Scanf ( " % I64d \ n " , & N )! = EOF) {temp =Pow (A, N, mod3); TMP = Mul (temp, F0, mod3 ); Long Long Tt = TMP. Mat [ 1 ] [ 0 ]; Temp = Pow (A, TT, mod2); TMP = Mul (temp, F0, mod2); TT = TMP. Mat [ 1 ] [ 0 ]; Temp = Pow (A, TT, MoD); TMP =Mul (temp, F0, MoD); TT = TMP. Mat [ 1 ] [ 0 ]; Printf ( " % I64d \ n " , TT );} Return 0 ;}