A Short problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 300 Accepted Submission (s): 110
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 Input
0
1
2
Sample Output
0
1
42837
Source
2012 ACM/ICPC Asia Regional Chengdu Online
Recommend
Liuyiding
This is mainly used to calculate the cyclic section. It is not difficult.
[Cpp]
// Calculate the cyclic Section
# Include <iostream>
# Include <stdio. h>
Using namespace std;
Typedef long LL;
Const LLMOD = 2017100007ll;
Int main ()
{
LL f0 = 0, f1 = 1, temp =-1;
For (LL I = 1; I ++)
{
Temp = (3 * f1 + f0) % MOD;
F0 = f1;
F1 = temp;
If (f0 = 0 & f1 = 1)
{
Printf ("% I64d \ n", I );
Break;
}
}
Return 0;
}
// Const ll mod = %%7ll;
// 222222224
// Const ll mod = 222222224LL;
// 183120
// Question A code
# Include <cstdio>
# Include <iostream>
# Include <cstdlib>
# Include <stdio. h>
# Include <math. h>
# Define ll _ int64
Using namespace std;
Const int MAX = 2;
Ll mm;
Typedef struct
{
Ll m [MAX] [MAX];
} Matrix;
Matrix P =
{
3, 1,
1, 0
};
Matrix I =
{
1, 0,
0, 1
};
Matrix matrixmul (Matrix a, Matrix B)
{
Int I, j, k;
Matrix c;
For (I = 0; I <MAX; I ++)
For (j = 0; j <MAX; j ++)
{
C. m [I] [j] = 0;
For (k = 0; k <MAX; k ++)
C. m [I] [j] + = (a. m [I] [k] * B. m [k] [j] % mm + mm) % mm;
C. m [I] [j] = (c. m [I] [j] % mm + mm) % mm;
}
Return c;
}
Matrix quickpow (ll n)
{
Matrix m = P, B = I;
While (n> = 1)
{
If (n & 1)
B = matrixmul (B, m );
N = n> 1;
M = matrixmul (m, m );
}
Return B;
}
Int main ()
{
Ll n;
While (scanf ("% I64d", & n )! = EOF)
{
If (n = 0)
{
Cout <0 <endl;
Continue;
}
Else if (n = 1)
{
Cout <1 <endl;
Continue;
}
Mm = 183120;
Matrix g = quickpow2 (n-1 );
Ll yy = g. m [0] [0];
Mm = 222222224LL;
If (yy! = 0 & yy! = 1)
{
G = quickpow2 (yy-1 );
Yy = g. m [0] [0];
}
Mm = 000000007ll;
If (yy! = 0 & yy! = 1)
{
G = quickpow2 (yy-1 );
Yy = g. m [0] [0] % mm;
}
Printf ("% I64d \ n", yy );
}
}