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





  • 相关阅读:
    mysql数据库 及 常用 SQL语句
    移动前端—图片压缩上传实践
    使用Nodejs 的http-proxy 模块做代理服务器的尝试
    Ajax请求参数为文件类型
    <iframe>框架标签的使用
    vue2 核心概念
    关于Web前端密码加密是否有意义的总结
    原生js 与 jQuery对比
    word文档操作
    js (ECMAScript) 对数据处理的 方法、属性总结
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681642.html
Copyright © 2011-2022 走看看