zoukankan      html  css  js  c++  java
  • 3734 Blocks DP矩阵优化入门

    Blocks
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 3665   Accepted: 1611

    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

    The first line of the input contains an integer T(1≤T≤100), the number of test cases. Each of the next T lines contains an integer N(1≤N≤10^9) indicating the number of blocks.

    Output

    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.

    Sample Input

    2
    1
    2

    Sample Output

    2
    6
    -----

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    using namespace std;
    typedef long long LL;
    const int M=10007;
    typedef vector<int>vec;
    typedef vector<vec>mat;
    mat mul(mat &A,mat &B){
        mat C(A.size(),vec(B[0].size()));
        for (int i=0;i<(int)A.size();i++){
            for (int k=0;k<(int)B.size();k++){
                for (int j=0;j<(int)B[0].size();j++){
                    C[i][j]=(C[i][j]+A[i][k]*B[k][j])%M;
                }
            }
        }
        return C;
    }
    mat pow(mat A,LL n){
        mat B(A.size(),vec(A.size()));
        for (int i=0;i<(int)A.size();i++){
            B[i][i]=1;
        }
        while (n>0){
            if (n&1) B=mul(B,A);
            A=mul(A,A);
            n>>=1;
        }
        return B;
    }
    const int f[3][3]={ {2,1,0},{2,2,2},{0,1,2} };
    const int a[1][3]={ {1,0,0} };
    int main()
    {
        int T;
        scanf("%d",&T);
        while (T--){
            int n;
            scanf("%d",&n);
            mat A(3,vec(3));
            for (int i=0;i<3;i++)
                for (int j=0;j<3;j++)
                    A[i][j]=f[i][j];
            A=pow(A,n);
            mat B(1,vec(3));
            for (int i=0;i<3;i++) B[0][i]=a[0][i];
            A=mul(A,B);
            printf("%d
    ",A[0][0]);
        }
        return 0;
    }
    





  • 相关阅读:
    复利完结观看评价
    汉堡计划
    0406 复利计算完结篇
    构建之法 第四章 读后感
    复利计算单元测试
    实验一 cmd命令的编写
    构建之法 前三章读后感
    复利计算完成总结
    复利计算软件v3
    实验 0 了解和熟悉操作系统
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681642.html
Copyright © 2011-2022 走看看