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

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: [email protected]
    > 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;
}
时间: 2024-08-07 02:58:52

Codeforces Round #113 (Div. 2)E---Tetrahedron(矩阵,水题)的相关文章

Codeforces Round #113 (Div. 2) E. Tetrahedron

题目链接:http://codeforces.com/problemset/problem/166/E 题意:给你一个四面体,从最上面那个顶点出发,走n步之后回到起点的方法有多少种.状态转移很简单dp[i][j] += dp[i-1][k](k != j); 但是n很大,这样做会超时,于是用矩阵快速幂来加速.A矩阵为0 1 1 1 1 0 1 1  1 1 0 1  1 1 1 0 #include <cstdio> #include <cstring> #include <

Codeforces Round #279 (Div. 2) A. Team Olympiad 水题

#include<stdio.h> #include<iostream> #include<memory.h> #include<math.h> using namespace std; int flag1[5000]; int flag2[5000]; int flag3[5000]; int main() { memset(flag1,0,sizeof(flag1)); memset(flag2,0,sizeof(flag2)); memset(flag

Codeforces Round #259 (Div. 2) (简单模拟实现题)

题目链接:http://codeforces.com/problemset/problem/454/A A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine

Codeforces Round #260 (Div. 2) A. Laptops(简单题)

题目链接:http://codeforces.com/problemset/problem/456/A A. Laptops time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day Dima and Alex had an argument about the price and quality of laptops.

Codeforces Round #423 (Div. 2) D. High Load(构造题)

题目链接:Codeforces Round #423 (Div. 2) D. High Load 题意: 给你一个数n和k,让你构造出一颗树,有k个叶子节点,使得这棵树的任意两个点的距离的最大值最小. 题解: 显然要使得这棵树的任意两个点的距离的最大值最小,每个点离树根越近越好. 然后要求有k个叶子节点,所以我就任意选一个点为根,然后构成一棵m叉的树就行了. 最大距离的最小值就是离根最远的和最近的加一加就行了. 1 #include<cstdio> 2 #define F(i,a,b) for

Codeforces Round #561 (Div. 2) (还差2题)

总结:bitset的基本操作:http://www.cnblogs.com/RabbitHu/p/bitset.html B题中求每行每列均有...,只要在下一行中把上一行的第一个放到最后一个就能构造满足条件的解: C题中这种,如果直接讨论绝对值的情况有点多,直接自己写几个例子试试会快上很多: E题中用bitset处理这些集合是否重合特别的快,代码也很简洁: 题目链接:http://codeforces.com/contest/1166 A: 题意:自己看看,练练英语,英语太菜了 题解:签到就行

Codeforces Round #256 (Div. 2) A. Rewards(简单题)

题目链接:http://codeforces.com/contest/448/problem/A ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

Codeforces Round #277 (Div. 2)A. Calculating Function 水

A. Calculating Function For a positive integer n let's define a function f: f(n) =  - 1 + 2 - 3 + .. + ( - 1)nn Your task is to calculate f(n) for a given integer n. Input The single line contains the positive integer n (1 ≤ n ≤ 1015). Output Print f

Codeforces Round #345 (Div. 2)C. Watchmen(想法题)

传送门 Description Watchmen are in a danger and Doctor Manhattan together with his friend Daniel Dreiberg should warn them as soon as possible. There are n watchmen on a plane, the i-th watchman is located at point (xi, yi). They need to arrange a plan,