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 <algorithm>
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
typedef long long LL;
const int mod = 1e9+7;
struct Matrix  {
    LL a[4][4];
    void clear()  {
        memset(a, 0, sizeof(a));
    }
}A;
Matrix multi(Matrix A, Matrix B)  {
    Matrix C;
    C.clear();
    for(int i = 0; i < 4; i++)
        for(int j = 0; j < 4; j++)  {
            for(int k = 0; k < 4; k++)  C.a[i][j] = (C.a[i][j]+A.a[i][k]*B.a[k][j])%mod;
        }
    return C;
}
Matrix cal(int n)  {
    if(n == 1)  return A;
    Matrix ret = cal(n/2);
    ret = multi(ret, ret);
    if(n&1)  ret = multi(ret, A);
    return ret;
}
int main()
{
    int n;
    cin >> n;
    if(n == 1)  {
        puts("0");
        return 0;
    }
    A.clear();
    for(int i = 0; i < 4; i++)
        for(int j = 0; j < 4; j++)  if(i != j)  A.a[i][j] = 1;
    Matrix B = cal(n-1);
    int ans = 0;
    for(int i = 1; i < 4; i++)  ans = (ans+B.a[i][0])%mod;
    cout << ans << endl;
}
时间: 2024-12-27 23:48:10

Codeforces Round #113 (Div. 2) E. Tetrahedron的相关文章

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

Codeforces Round #259 (Div. 2) 解题报告

终于重上DIV1了.... A:在正方形中输出一个菱形 解题代码: 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月01日 星期五 23时27分55秒 4 5 #include<vector> 6 #include<set> 7 #include<deque> 8 #include<stack> 9 #include<bitset> 10 #inclu

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #250 (Div. 1)

这几次CF都挺惨.. A 没条边权设为两端点的最小点权,最后加起来. 数组开小,WA一次 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #

Codeforces Round #424 (Div. 2) E. Cards Sorting(线段树)

题目链接:Codeforces Round #424 (Div. 2) E. Cards Sorting 题意: 将n个数放进一个队列,每次检查队首,看看是不是队列中最小的数,如果是就扔掉,如果不是就放到队尾. 这样直到队列为空,为需要操作多少次. 题解: 考虑用两个指针模拟,最开始now指针指向第一个数,然后nxt指针指向下一个将要被删除的数. 然后我们要算出这里需要移动多少步,然后删掉这个数,一直重复操作,直到将全部的数删完. nxt指针可以用set来维护,now指针可以用并查集来维护. 计

Codeforces Round #366 (Div. 2) ABC

Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 1 #I hate that I love that I hate it 2 n = int(raw_input()) 3 s = "" 4 a = ["I hate that ","I love that ", "I hate it","I love it"] 5 fo

二分查找/暴力 Codeforces Round #166 (Div. 2) B. Prime Matrix

题目传送门 1 /* 2 二分查找/暴力:先埃氏筛选预处理,然后暴力对于每一行每一列的不是素数的二分查找最近的素数,更新最小值 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <algorithm> 7 using namespace std; 8 9 const int MAXN = 5e2 + 10; 10 const int MAXM = 1e6 + 10; 11 const int INF = 0

CodeCraft-19 and Codeforces Round #537 (Div. 2) - D. Destroy the Colony(动态规划+组合数学)

Problem  CodeCraft-19 and Codeforces Round #537 (Div. 2) - D. Destroy the Colony Time Limit: 2000 mSec Problem Description Input Output For each question output the number of arrangements possible modulo 10^9+7. Sample Input abba21 41 2 Sample Output