POJ3734 Blocks

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.

Code

#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const int mod = 10007;
int n,T;
struct Matrix{
    ll data[10][10];
    int r,c;
    Matrix(int R,int C)
    {
        r = R;
        c = C;
        memset(data,0,sizeof(data));
    }
    ll* operator [](int pos)
    {
        return data[pos];   
    }
    Matrix operator *(Matrix B)
    {
        Matrix C(r,B.c);
        for(int i = 1;i <= r;++i){
            for(int j = 1;j <= B.c;++j){
                for(int k = 1;k <= c;++k){
                    C[i][j] = (C[i][j]%mod + (data[i][k]%mod)*(B[k][j]%mod)%mod)%mod;
                }
            }
        }
        return C;
    }
};
Matrix Pow(Matrix A,int b){
    Matrix res(A.r,A.c);
    for(int i = 1;i <= A.r;++i) res[i][i] = 1;//单位矩阵,相当于整数乘法中的1
    for(;b;b >>= 1){
        if(b&1) res = res*A;
        A = A*A;
    }
    return res;
}
void solve()
{
	Matrix A(3,3);
	A[1][1] = 2;A[1][2] = 1;A[1][3] = 0;
	A[2][1] = 2;A[2][2] = 2;A[2][3] = 2;
	A[3][1] = 0;A[3][2] = 1;A[3][3] = 2;
	A = Pow(A,n);
	printf("%d\n",A[1][1]);
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		solve();
	}
	return 0;
}
posted @ 2019-06-02 16:26  Zforw  阅读(42)  评论(0编辑  收藏  举报