Blocks-矩阵快速幂

Blocks

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)

Total Submission(s) : 16   Accepted Submission(s) : 4

Problem Description

Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he starts to think of a math problem of painting. Suppose there are N blocks in a line and each block can be paint red, blue, green or yellow. For some myterious reasons, Panda want both the number of red blocks and green blocks to be even numbers. Under such conditions, Panda wants to know the number of different ways to paint these blocks.

Input

<div><p>The first line of the input contains an integer <i>T</i>(1≤<i>T</i>≤100), the number of test cases. Each of the next <i>T</i> lines contains an integer <i>N</i>(1≤<i>N</i>≤10^9) indicating the number of blocks. </p>

Output

<div><p>For each test cases, output the number of ways to paint the blocks in a single line. Since the answer may be quite large, you have to module it by 10007. </p>

Sample Input

2 1 2

Sample Output

扫描二维码关注公众号,回复: 2389273 查看本文章

2 6

Source

PKU

#include<cstdio>
#include<cstring>
using namespace std;
const int mod=1e4+7;
typedef struct mat
{
    int m[3][3];
}mat;
mat mul(mat a,mat b)
{
    mat t;
    memset(t.m,0,sizeof(t.m));
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        for(int k=0;k<3;k++)
        t.m[i][j]=(t.m[i][j]+a.m[i][k]*b.m[k][j])%mod;
    return t;
}
mat pow(mat a,int n)
{
    mat b;
    memset(b.m,0,sizeof(b.m));
    for(int i=0;i<3;i++) b.m[i][i]=1;
    while(n>0)
    {
        if(n&1) b=mul(b,a);
        a=mul(a,a);
        n>>=1;
    }
    return b;
}
int main()
{
    int T,n;
    scanf("%d",&T);
    mat a;
    a.m[0][0]=2,a.m[0][1]=1,a.m[0][2]=0;
    a.m[1][0]=2,a.m[1][1]=2,a.m[1][2]=2;
    a.m[2][0]=0,a.m[2][1]=1,a.m[2][2]=2;
    while(T--)
    {
        scanf("%d",&n);
        mat ans=pow(a,n);
        printf("%d\n",ans.m[0][0]);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41061455/article/details/81157670