• 原题如下:

    Blocks
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8020   Accepted: 3905

    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
  • 题解:从最左边开始染色,设染到第i个方块为止,红绿都是偶数的方案数为ai,红绿恰有一个是偶数的方案数为bi,红绿都是奇数的方案数为ci。这样,染到第i+1个方块为止,红绿都是偶数的方案数有两种可能:①到第i个方块为止红绿都是偶数,并且第i+1个方块染成了蓝色或者黄色 ② 到第i个方块为止红绿恰有一个是奇数, 并且第i+1个方块染成了奇数个对应的那种颜色。因此有递推式ai+1=2*ai+bi,同样地,有bi+1=2*ai+2*bi+2*ci,ci+1=bi+2*ci,把ai,bi,ci的递推式用矩阵表示如下:

    因此就有:

  • 代码:
     #include <cstdio>
    #include <cctype>
    #define number s-'0'
    #include <cstring>
    #include <vector> using namespace std; typedef vector<int> vec;
    typedef vector<vec> mat; const int M=;
    int N; void read(int &x)
    {
    char s;
    x=;
    bool flag=;
    while (!isdigit(s=getchar()))
    (s=='-')&&(flag=true);
    for (x=number; isdigit(s=getchar());x=x*+number);
    (flag)&&(x=-x);
    } void write(int x)
    {
    if (x<)
    {
    putchar('-');
    x=-x;
    }
    if (x>) write(x/);
    putchar(x%+'');
    } mat mul(mat &A, mat &B)
    {
    mat C(A.size(), vec(B[].size()));
    for (int i=; i<A.size(); i++)
    {
    for (int j=; j<B[].size(); j++)
    {
    for (int k=; k<B.size(); k++)
    {
    C[i][j]=(C[i][j]+A[i][k]*B[k][j])%M;
    }
    }
    }
    return C;
    } mat pow(mat A, int n)
    {
    mat B(A.size(), vec(A.size()));
    for (int i=; i<A.size(); i++) B[i][i]=;
    while (n>)
    {
    if (n&) B=mul(B, A);
    A=mul(A, A);
    n>>=;
    }
    return B;
    } int main(int argc, char * argv[])
    {
    int t;
    read(t);
    while (t>)
    {
    t--;
    read(N);
    mat A(, vec());
    A[][]=;A[][]=;A[][]=;
    A[][]=;A[][]=;A[][]=;
    A[][]=;A[][]=;A[][]=;
    A=pow(A, N);
    printf("%d\n",A[][]);
    }
    }

Blocks(POJ 3734)的更多相关文章

  1. [POJ 3734] Blocks (矩阵高速幂、组合数学)

    Blocks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3997   Accepted: 1775 Descriptio ...

  2. Blocks(POJ 3734 矩阵快速幂)

    Blocks Input The first line of the input contains an integer T(1≤T≤100), the number of test cases. E ...

  3. POJ 3734 Blocks (矩阵快速幂)

    题目链接 Description Panda has received an assignment of painting a line of blocks. Since Panda is such ...

  4. poj 3734 Blocks

    ゲート 分析:这题过的人好多,然后大家好像是用矩阵过的(((φ(◎ロ◎;)φ))).我自己是推公式的. 对于任意的有这个式子, 就是先从里面选偶数个涂成两个指定的颜色,再在选出的里面选定涂某种颜色,选 ...

  5. poj 3734 Blocks 快速幂+费马小定理+组合数学

    题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...

  6. POJ 3734 Blocks(矩阵快速幂+矩阵递推式)

    题意:个n个方块涂色, 只能涂红黄蓝绿四种颜色,求最终红色和绿色都为偶数的方案数. 该题我们可以想到一个递推式 .   设a[i]表示到第i个方块为止红绿是偶数的方案数, b[i]为红绿恰有一个是偶数 ...

  7. POJ 3734 Blocks (线性递推)

    定义ai表示红色和绿色方块中方块数为偶数的颜色有i个,i = 0,1,2. aij表示刷到第j个方块时的方案数,这是一个线性递推关系. 可以构造递推矩阵A,用矩阵快速幂求解. /*********** ...

  8. poj 3734 Blocks【指数型生成函数】

    指数型生成函数,推一推可得: \[ (1+\frac{x^1}{1!}+\frac{x^2}{2!}+\frac{x^3}{3!}+...)^2+(1+\frac{x^2}{2!}+\frac{x^4 ...

  9. POJ 3734 Blocks 矩阵递推

    POJ3734 比较简单的递推题目,只需要记录当前两种颜色均为偶数, 只有一种颜色为偶数 两种颜色都为奇数 三个数量即可,递推方程相信大家可以导出. 最后来个快速幂加速即可. #include< ...

随机推荐

  1. C#LeetCode刷题之#189-旋转数组(Rotate Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3700 访问. 给定一个数组,将数组中的元素向右移动 k 个位置, ...

  2. JavaScript npm/nrm 切换安装依赖的镜像源

    nrm: npm registry manager npm 镜像源管理工具 安装nrm npm install -g nrm 查看所有的镜像源 nrm ls # nrm ls npm -------- ...

  3. Butterfly侧边栏引入一言

    此教程涉及修改源码 背景 在修改每页显示7篇文章后,出现了这种情况. 这是完美主义(强迫症)的我所不能忍受的,有什么可以占据这里的呢?{% btn 'https://hitokoto.cn/',一言, ...

  4. 使用 .NET Core 3.x 构建 RESTFUL Api (续)

    关于Entity Model vs 面向外部的Model Entity Framework Core 使用 Entity Model 用来表示数据库里面的记录. 面向外部的Model 则表示要传输的东 ...

  5. 洛谷P1149.火柴棒等式(暴力搜索)

    题目描述 给你n根火柴棍,你可以拼出多少个形如"A+B=C"的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注 ...

  6. 【Floyd】珍珠

    [题目描述] 有n颗形状和大小都一致的珍珠,它们的重量都不相同.n为整数,所有的珍珠从1到n编号.你的任务是发现哪颗珍珠的重量刚好处于正中间,即在所有珍珠的重量中,该珍珠的重量列(n+1)/2位.下面 ...

  7. Mybatis-plus 实体类新增属性,使用实体类执行sql操作时忽略该属性 注解

    @TableField(exist = false) 注解加载bean属性上,表示当前属性不是数据库的字段,但在项目中必须使用,这样在新增等使用bean的时候,mybatis-plus就会忽略这个,不 ...

  8. eclipse项目文件夹整理

    1.点击倒三角 2.系统默认为Projects,选择第二个working sets 3.点击Configure Working Sets,点new 4.点击后,选中点Add 5.添加一个名字,Fins ...

  9. JAVA使用urlrewrite实现伪静态化

    什么是伪静态? 伪静态字面理解就是假的静态,说的官方点就是“地址重写,用户得到的全部地址都是经过处理后的URL地址”. 为什么要伪静态呢? 提高安全性,可以有效的避免一些参数名.ID等完全暴露在用户面 ...

  10. 将composer切换到国内镜像

    composer config -g repo.packagist composer https://packagist.phpcomposer.com