POJ 3734 Blocks

有一排砖,数量为N。现要将砖全部染上色,有红、蓝、绿、黄四种颜色。
要求被染成红色和绿色的砖块数量必须为偶数,问一共有多少种染色方案。
(由于答案较大,模10007)

矩阵快速幂
这个写的好

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#define pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const int maxn = 4;
const int mod = 10007;

struct Matrix{
    int n, m;
    int a[maxn][maxn];
    Matrix(){   // 矩阵初始化 (叫做构造函数 会自动执行 我学了c++后才知道的..)
        n = m = 0;
        memset(a, 0, sizeof(a));
    }
    Matrix operator * (const Matrix &b) const {
        Matrix tmp;
        tmp.n = n; tmp.m = b.m;
        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++){
                if (!a[i][j]) continue; //稀疏矩阵乘法优化
                for (int k = 0; k < b.m; k++){
                    tmp.a[i][k] = (tmp.a[i][k] + a[i][j] * b.a[j][k]) % mod;
                }
            }
        }
        return tmp;
    }
};
int fast_muti(int n)
{
    Matrix base, ans;
    base.n = base.m = 4;
    base.a[0][0] = 2; base.a[0][1] = 1; base.a[0][2] = 1; base.a[0][3] = 0;
    base.a[1][0] = 1; base.a[1][1] = 2; base.a[1][2] = 0; base.a[1][3] = 1;
    base.a[2][0] = 1; base.a[2][1] = 0; base.a[2][2] = 2; base.a[2][3] = 1;
    base.a[3][0] = 0; base.a[3][1] = 1; base.a[3][2] = 1; base.a[3][3] = 2;
    ans.n = ans.m = 4;
    for (int i = 0; i < 4; i++)
        ans.a[i][i] = 1;
    while (n){
        if (n & 1)
            ans = base * ans;
        base = base * base;
        n >>= 1;
    }
    return ans.a[0][0];
}
int main(void)
{
//  freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin);
    int n, T;
    cin >> T;
    while (T--)
    {
        cin >> n;
        printf("%d\n", fast_muti(n));
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值