Poj 3734 Blocks【递推+矩阵快速幂】好题

Blocks
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6239 Accepted: 2998

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

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

Source


题目大意:

一共有四种颜色:红、绿、蓝、黄.问一共涂N个连续排列的格子,最终使得红、绿色的格子为偶数个的方案数。


思路:


1、

设ai表示涂到第i个格子的时候,红、绿格子都是偶数个数的情况。

设bi表示涂到第i个格子的时候,红、绿有一个是偶数个数,有一个是奇数个数的情况。

设ci表示涂到第i个格子的时候,红、绿格子都是奇数个数的情况。


2、那么我们尝试递推ai+1:

ai+1表示涂到第i+1个格子的时候,红、绿格子都是偶数个数的情况。那么其当前情况可以从ai这种情况涂一个蓝色格子,或者是黄色格子而来,也可以从bi这种情况对应将红、绿中奇数个数的那个颜色的格子再涂上一个格子,使得红、绿两种颜色的格子都变成偶数个数。

那么ai+1=2*ai+bi


3、同理递推bi+1:

bi+1表示涂到第i+1个格子的时候,红、绿有一个是偶数个数,有一个是奇数个数的情况。那么其当前情况可以从ai这种情况涂一个红色格子或者是绿色格子而来,也可以从bi这种情况对应涂一个蓝色或者黄色格子而来,也可以从ci这个情况涂一个红色格子或者是绿色格子而来。

那么bi+1=2*ai+2*bi+2*ci


4、同理递推ci+1:

设ci+1表示涂到第i+1个格子的时候,红、绿格子都是奇数个数的情况。那么其当前情况可以从bi这种情况对应将红、绿中偶数个数的那个颜色的格子再涂上一个格子,使得红、绿两种颜色的格子都变成奇数个数,也可以从ci这种情况对应涂一个蓝色格子或者是红色格子而来。

那么ci+1=bi+2*ci


5、然后考虑到N最大可以达到10^9,显然O(n)的直接迭代是会超时的,考虑使用矩阵快速幂进行优化:

①首先考虑递推式:


②那么最终解:



3、构造好矩阵和思路,写代码的时候注意取模问题即可。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
#define mod 10007
typedef struct Matrix
{
    int mat[3][3];
}matrix;
matrix A,B;
Matrix matrix_mul(matrix a,matrix b)
{
    matrix c;
    memset(c.mat,0,sizeof(c.mat));
    int i,j,k;
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<3;j++)
        {
            for(int k=0;k<3;k++)
            {
                c.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
                c.mat[i][j]%=mod;
            }
        }
    }
    return c;
}
Matrix matrix_quick_power(matrix a,int k)//矩阵快速幂0.0
{
    matrix b;
    memset(b.mat,0,sizeof(b.mat));
    for(int i=0;i<3;i++)
    b.mat[i][i]=1;//单位矩阵b
    while(k)
    {
        if(k%2==1)
        {
            b=matrix_mul(a,b);
            k-=1;
        }
        else
        {
            a=matrix_mul(a,a);
            k/=2;
        }
    }
    return b;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        A.mat[0][0]=2;A.mat[0][1]=1;A.mat[0][2]=0;
        A.mat[1][0]=2;A.mat[1][1]=2;A.mat[1][2]=2;
        A.mat[2][0]=0;A.mat[2][1]=1;A.mat[2][2]=2;
        B=matrix_quick_power(A,n);
        printf("%d\n",B.mat[0][0]);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值