• 原题如下:

    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. 第二章 Kuberbetes实践指南

    kubernetes安装与配置 网络,安全,服务启动配置 参考: kubernetes权威指南第二版 kubectl命令行工具用法详解 kubectl [command] [type] [name] ...

  2. JDK1.8源码学习-HashMap

    JDK1.8源码学习-HashMap 目录 一.HashMap简介 HashMap 主要用来存放键值对,它是基于哈希表的Map接口实现的,是常用的Java集合之一. 我们都知道在JDK1.8 之前 的 ...

  3. MySQL是如何实现事务的ACID

    前言 最近在面试,有被问到,MySQL的InnoDB引擎是如何实现事务的,又或者说是如何实现ACID这几个特性的,当时没有答好,所以自己总结出来,记录一下. 事务的四大特性ACID 事务的四大特性AC ...

  4. Overcoming Forgetting in Federated Learning on Non-IID Data

    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布! 以下是对本文关键部分的摘抄翻译,详情请参见原文. NeurIPS 2019 Workshop on Federated Learning ...

  5. muduo源码解析10-logstream类

    FixedBuffer和logstream class FixedBuffer:noncopyable { }: class logstream:noncopyable { }: 先说一下包含的头文件 ...

  6. py_选择排序

    # 选择排序 # 一趟排序记录最小值,放到第一个位置 #再一趟排序记录记录列表无序区最小的数,放到第二个位置 #.... # 关键点:有序区.无序区.无序区最小值 #方法一 def select_So ...

  7. 以Winsows Service方式运行JupyterLab

    有数据分析,数据挖掘,以及机器学习和深度学习实践经验的读者应该会对Jupyter Notebook这一工具十分熟悉,而JupyterLab是它的升级版本,其提供了更具扩展性,更加可定制化的功能选项. ...

  8. 广度优先搜索(BFS)解题总结

    定义 广度优先搜索算法(Breadth-First-Search),是一种图形搜索算法. 简单的说,BFS是从根节点开始,沿着树(图)的宽度遍历树(图)的节点. 如果所有节点均被访问,则算法中止. B ...

  9. 你可能不了解的java枚举

    枚举在java里也算个老生长谈的内容了,每当遇到一组需要类举的数据时我们都会自然而然地使用枚举类型: public enum Color { RED, GREEN, BLUE, YELLOW; pub ...

  10. Kubernetes K8S之资源控制器Daemonset详解

    Kubernetes的资源控制器Daemonset详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-master CentOS7.7 2C/ ...