zoukankan      html  css  js  c++  java
  • 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
    ",A[1][1]);
    }
    int main()
    {
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d",&n);
    		solve();
    	}
    	return 0;
    }
    
    岂能尽如人意,但求无愧我心
  • 相关阅读:
    第一次练习总结
    第一次上机总结
    写在程序组干活之前
    虚拟机Centos7安装Mysql
    第一章 开发体验
    如何优雅的移植JavaScript组件到Blazor
    Asp.net core中RedisMQ的简单应用
    docker容器安装mysql
    Centos 8安装Docker
    c# 定时启动一个操作、任务(版本2)
  • 原文地址:https://www.cnblogs.com/Zforw/p/10963297.html
Copyright © 2011-2022 走看看