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;
    }
    





  • 相关阅读:
    cookie和session
    ViewState与Session 的重要区别
    C#.Net 中ArrayList 与 Array的区别
    c#如何把文件夹压缩打包然后下载
    关于并发的处理
    const和readonly的区别
    计算某列的字符串相加sql语句
    MS SQL Server存储过程批量修改用户表所有者
    已有打开的与此命令相关联的DataReader,必须首先将它关闭
    XML与数据库
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681642.html
Copyright © 2011-2022 走看看