Title Link: Http://codeforces.com/problemset/problem/185/A
Dwarfs has planted a very interesting plant, which is a triangle directed "upwards". This plant have an amusing feature. After one year a triangle plant directed "upwards" divides to four triangle plants:three of them would point "upwards" a nd one would point "downwards". After another year, each triangle plant divides to four triangle plants:three of them would be directed in the same dire Ction as the parent plant, and one of the them'll be directed in the opposite direction. Then the process repeats. The figure below illustrates this process.
Help the Dwarfs find out how many triangle plants that point "upwards" would be is in N years.
Input
The first line contains a single integer n (0?≤? n≤?10) -the number of full years when the plant grew.
%lld specifier to read or write 64-bit integers inс++. It is preferred to use cin, cout streams or the %i64dspecifier.
Output
Print a single integer-the remainder of dividing the number of plants that would point ' upwards ' in N years by c2>1000000007 (9+?7).
Sample Test (s) input
1
Output
3
Input
2
Output
10
Note
The first Test, sample corresponds to the second triangle, is on the figure in the statement. The second test sample corresponds to the third one.
Ps:
number of upper triangles: - +1*▽
number of lower triangles: ▽-> 1* +3*▽
Matrix Fast Power;
The equal ratio matrix is:
3 1
1 3
The code is as follows:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD; #define LL __int64struct matrix{ll m[5][5];} I,a,b,t; LL A, b, n;int ssize = 2, #define MOD 1000000007Matrix Mul (Matrix A,matrix b) {int I, j, K; Matrix C; for (i = 1; I <= ssize; i++) {for (j = 1; J <= Ssize; j + +) {c.m[i][j]=0; for (k = 1; k <= ssize; k++) {c.m[i][j]+= (a.m[i][k]*b.m[k][j]); C.m[i][j]%=mod; }}} return C;} Matrix Quickpagow (LL N) {matrix M = A, B = I; while (n) {if (n & 1) b = Mul (b,m); n = n >> 1; m = Mul (m,m); } return B; int main () {//a1 downward, B1 upward LL C; while (~SCANF ("%i64d", &n)) {memset (i.m,0,sizeof (I.M)); memset (A.m,0,sizeof (a.m.)); memset (b.m,0,sizeof (B.M)); for (int i = 1; I <= ssize; i++) {//Unit matrix I.m[i][i]=1; } B.m[1][1] = 0, b.m[2][1] = 1; A.M[1][1] = 3;//initialization of the matrix a.m[1][2] = 1; A.M[2][1] = 1; A.M[2][2] = 3; T = Quickpagow (n); T = Mul (b,t); printf ("%i64d\n", t.m[2][1]%mod); } return 0;} /*1231000000100000000000000000010*/
Codeforces 185A. Plant (Matrix fast Power)