Codeforces Round #113 (Div. 2)E

You are given a tetrahedron. Let’s mark its vertices with letters A, B, C and D correspondingly.

An ant is standing in the vertex D of the tetrahedron. The ant is quite active and he wouldn’t stay idle. At each moment of time he makes a step from one vertex to another one along some edge of the tetrahedron. The ant just can’t stand on one place.

You do not have to do much to solve the problem: your task is to count the number of ways in which the ant can go from the initial vertex D to itself in exactly n steps. In other words, you are asked to find out the number of different cyclic paths with the length of n from vertex D to itself. As the number can be quite large, you should print it modulo 1000000007 (109+7). Input

The first line contains the only integer n (1≤n≤107) — the required length of the cyclic path. Output

Print the only integer — the required number of ways modulo 1000000007 (109+7). Sample test(s) Input

2

Output

3

Input

4

Output

21

Note

The required paths in the first sample are:

D-A-DD-B-DD-C-D

水题

/*************************************************************************> File Name: CF-113-E.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: 2015年03月21日 星期六 11时03分31秒 ************************************************************************/;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;LL;typedef pair <int, int> PLL;const int mod = 1000000007;class MARTIX{public:LL mat[4][4];MARTIX();MARTIX operator * (const MARTIX &b)const;MARTIX& operator = (const MARTIX &b);};MARTIX :: MARTIX(){memset(mat, 0, sizeof(mat));}MARTIX MARTIX :: operator * (const MARTIX &b)const{MARTIX ret;for (int i = 0; i < 4; ++i){for (int j = 0; j < 4; ++j){for (int k = 0; k < 4; ++k){ret.mat[i][j] += this -> mat[i][k] * b.mat[k][j];ret.mat[i][j] %= mod;}}}return ret;}MARTIX& MARTIX :: operator = (const MARTIX &b){for (int i = 0; i < 4; ++i){for (int j = 0; j < 4; ++j){this -> mat[i][j] = b.mat[i][j];}}return *this;}MARTIX fastpow(MARTIX A, int cnt){MARTIX ret;ret.mat[0][0] = ret.mat[1][1] = ret.mat[2][2] = 1;while (cnt){if (cnt & 1){ret = ret * A;}A = A * A;cnt >>= 1;}return ret;}int main(){int n;MARTIX A;for (int i = 0; i < 4; ++i){for (int j = 0; j < 4; ++j){A.mat[i][j] = (i != j);}}while (~scanf(“%d”, &n)){MARTIX B = fastpow(A, n);printf(“%lld\n”, B.mat[0][0]);}return 0;}

,流转的时光,都成为命途中美丽的点缀,

Codeforces Round #113 (Div. 2)E

相关文章:

你感兴趣的文章:

标签云: