Blocks(POJ 3734) (矩阵快速幂)

本文探讨了一道有趣的数学问题:给定一行N个方块,每个方块可以涂成红、蓝、绿或黄色,但红色和绿色方块的数量必须为偶数。文章提供了计算在这些条件下不同涂色方式数量的算法,并给出了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

2 6

Source

PKU

Ai+1=2*Ai+Bi

Bi+1=2*Ai+2*Bi+2*Ci

Ci+1=Bi+2*Ci

A0=1,B0=C0=0

#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]);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值