Blocks OpenJ_Bailian - 3744(快速矩阵幂)

Blocks

OpenJ_Bailian - 3744

Panda has received an assignment of painting a line of blocks. Since Panda is such an intelligent boy, he start 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

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

Output

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.

Sample Input

2
1
2

Sample Output

2
6

 思路:设染到第i个方块为止,红绿都是偶数的方案数为ai,红绿恰有一个是偶数的方案数为bi,红绿都是奇数的方案数为ci。

那么到i+1个方块为止,红绿都是偶数的方案有如下两种可能

1)到第i个方块为止红绿都是偶数,并且第i+1个方块染成了蓝色或者黄色

2)到第i个方块红绿恰有一个是奇数,并且第i+1个方块染成奇数个对应的那种颜色,则

a(i+1)=2*ai+bi;

同理很容易得到

b(i+1)=2*ai+2*bi+2*ci;

c(i+1)=bi+2*ci;

ai,bi,ci的递推式用矩阵表示如下:

\begin{pmatrix} a_{i+1}\\ b_{i+1}\\ c_{i+1} \end{pmatrix} = \begin{pmatrix} 2 &1 &0 \\ 2& 2&2 \\ 0& 1 & 2 \end{pmatrix}\begin{pmatrix} a_i\\ b_i\\ c_i \end{pmatrix}

则简化成:

\begin{pmatrix} a_i\\ b_i\\ c_i \end{pmatrix} =\begin{pmatrix} 2 &1 &0 \\ 2 & 2& 2\\ 0&1 &2 \end{pmatrix} \begin{pmatrix} a_0\\ b_0\\ c_0 \end{pmatrix}= \begin{pmatrix} 2 &1 &0 \\ 2 & 2& 2\\ 0&1 &2 \end{pmatrix} \begin{pmatrix} 1\\ 0\\ 0 \end{pmatrix}

AC代码如下:

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=15;
const int MOD = 1e4 + 7;
struct node
{
    int a[N][N];
} s,e;

node mul(node p, node q)///求两个矩阵的积;
{
    node tem;
    for(int i=0; i<3; i++)
    {
        for(int j=0; j<3; j++)
        {
            tem.a[i][j] = 0;
            for(int k=0; k<3; k++)
                tem.a[i][j] = (tem.a[i][j]+p.a[i][k]*q.a[k][j])%MOD;
        }
    }
    return tem;
}
node Pow(node p, int n)
{
    node tem;
    for(int i=0; i<3; i++)
        for(int j=0; j<3; j++)
            tem.a[i][j] = (i==j);
    while(n)
    {
        if(n&1)///n是奇数;
            tem = mul(tem, p);///让矩阵e和矩阵a相乘;
        n/=2;
        p = mul(p, p);
    }
    return tem;
}

int main()
{
    s.a[0][0]=2;
    s.a[0][1]=1;
    s.a[0][2]=0;
    s.a[1][0]=2;
    s.a[1][1]=2;
    s.a[1][2]=2;
    s.a[2][0]=0;
    s.a[2][1]=1;
    s.a[2][2]=2;
    int n,t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        e = Pow(s, n);
        printf("%d\n", e.a[0][0]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值