Week14 作业 D - Q老师染砖(选做)

Description
衣食无忧的 Q老师 有一天突发奇想,想要去感受一下劳动人民的艰苦生活。
具体工作是这样的,有 N 块砖排成一排染色,每一块砖需要涂上红、蓝、绿、黄这 4 种颜色中的其中 1 种。且当这 N 块砖中红色和绿色的块数均为偶数时,染色效果最佳。
为了使工作效率更高,Q老师 想要知道一共有多少种方案可以使染色效果最佳,你能帮帮他吗?
Input
第一行为 T,代表数据组数。(1 ≤ T ≤ 100)
接下来 T 行每行包括一个数字 N,代表有 N 块砖。(1 ≤ N ≤ 1e9)
Output
输出满足条件的方案数,答案模 10007。
Sample Input
2
1
2
Sample Output
2
6
解题思路:
在这里插入图片描述

#include <iostream>
#include <cstring>
# define LL long long
using namespace std ;

const int MOD = 10007 ;

struct Matrix
{
    LL mat[3][3];
};

Matrix mul(Matrix a,Matrix b) //矩阵乘法
{
    Matrix c;
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        {
            c.mat[i][j]=0;
            for(int k=0;k<3;k++)
            {
                c.mat[i][j]=(c.mat[i][j] + a.mat[i][k]*b.mat[k][j])%MOD;
            }
        }
    return c;
}
Matrix pow_M(Matrix a,int k)  //矩阵快速幂
{
    Matrix ans;
    memset(ans.mat,0,sizeof(ans.mat));
    for (int i=0;i<3;i++)
        ans.mat[i][i]=1;
    Matrix temp=a;
    while(k)
    {
        if(k&1)ans=mul(ans,temp);
        temp=mul(temp,temp);
        k>>=1;
    }
    return ans;
}



int main ()
{
    int T;
    cin>>T ;
    Matrix t ;
    t.mat[0][0] = 2 ; t.mat[0][1] = 1 ; t.mat[0][2] = 0 ;
    t.mat[1][0] = 2 ; t.mat[1][1] = 2 ; t.mat[1][2] = 2 ;
    t.mat[2][0] = 0 ; t.mat[2][1] = 1 ; t.mat[2][2] = 2 ;
    while(T--)
    {
        int n ;
        cin>>n ;
        Matrix ans = pow_M(t,n) ;
        cout<<ans.mat[0][0]<<endl ;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值