码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #113 (Div. 2)E---Tetrahedron(矩阵,水题)

时间:2015-03-21 12:42:08      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:矩阵

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?-?D
D?-?B?-?D
D?-?C?-?D 

水题

/*************************************************************************
    > File Name: CF-113-E.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月21日 星期六 11时03分31秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long 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---Tetrahedron(矩阵,水题)

标签:矩阵

原文地址:http://blog.csdn.net/guard_mine/article/details/44514347

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!