题目链接

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

题意:

给定n个方格排成一列,现在要用红、蓝、黄、绿四种颜色的油漆给这些方格染色。求染成红色的方块数和染成绿色的方块的个数同时位偶数的染色方案的个数,输出对10007取余后的答案。

分析:

我们从最左边开始染色。设染到第i个方块为止,红色和绿色都是偶数的方案数为Ai,红色和绿色恰有一个为偶数的方案数是Bi,红色和绿色都是奇数的方案数是Ci。这样染到第i+1个方格为止,红色和绿色都是偶数的方案数有如下两种可能:

1.到第i个方块为止,红色和绿色都是偶数个,并且第i+1个方块被染成了蓝色或者黄色。

2.到第i个方块为止红色和绿色恰有一个是奇数,并且第i+1个方块染成奇数的那个对应的颜色。

因此,有如下的递推关系:

Ai+1=2 ×Ai +Bi

同理,有

Bi+1=2 × Ai + 2 × Bi + 2 × Ci

Ci+1=bi + 2 × Ci

递推关系可以用矩阵表示如下:

之后就可以用矩阵快速幂求解了。

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int n;
struct matrix
{
int tu[10][10];
matrix()
{
memset(tu,0,sizeof(tu));
}
} A,B;
matrix mul(matrix &A,matrix &B)///定义矩阵的乘法
{
matrix C;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
for(int k=0; k<3; k++)
{
C.tu[i][j]=(C.tu[i][j]+(A.tu[i][k]*B.tu[k][j]%10007))%10007;
}
return C;
} matrix quick_mi(matrix A,int b)///求一个矩阵的A的b次方
{
matrix C;
for(int i=0; i<3; i++)
C.tu[i][i]=1;
while(b)
{
if(b&1)
C=mul(C,A);
b>>=1;
A=mul(A,A);
}
return C;
} int main()
{
int T;
scanf("%d",&T);
matrix A;
while(T--)
{
scanf("%d",&n);
A.tu[0][0]=2;
A.tu[0][1]=1;
A.tu[0][2]=0;
A.tu[1][0]=2;
A.tu[1][1]=2;
A.tu[1][2]=2;
A.tu[2][0]=0;
A.tu[2][1]=1;
A.tu[2][2]=2;
A=quick_mi(A,n);
printf("%d\n",A.tu[0][0]%10007);
}
return 0;
}

POJ 3734 Blocks (矩阵快速幂)的更多相关文章

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

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

  2. POJ 3744 【矩阵快速幂优化 概率DP】

    搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...

  3. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  4. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  5. POJ——3070Fibonacci(矩阵快速幂)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12329   Accepted: 8748 Descri ...

  6. POJ 3070 Fibonacci 矩阵快速幂模板

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18607   Accepted: 12920 Descr ...

  7. poj 3735 稀疏矩阵矩阵快速幂

    设人数为 $n$,构造 $(n + 1) \times (n + 1)$ 的矩阵 得花生:将改行的最后一列元素 $+ 1$ \begin{gather}\begin{bmatrix}1 & 0 ...

  8. POJ 3070 Fibonacci矩阵快速幂 --斐波那契

    题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...

  9. POJ 3613 floyd+矩阵快速幂

    题意: 求s到e恰好经过n边的最短路 思路: 这题已经被我放了好长时间了. 原来是不会矩阵乘法,快速幂什么的也一知半解 现在终于稍微明白了点了 其实就是把矩阵乘法稍微改改 改成能够满足结合律的矩阵&q ...

  10. POJ 3734 Blocks 矩阵递推

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

随机推荐

  1. Laravel中如何添加新字段,如何指定在某个字段后而不是添加在最后

    解答:

  2. jquery中on绑定click事件在苹果手机失效问题解决(巨坑啊)

    描述:用一个div写一个按钮,并给这个按钮添加一个点击事件,在安卓机器上一切正常,但是在苹果机型上会出现点击事件失效. <!DOCTYPE html> <html lang=&quo ...

  3. 剖析Vue原理&实现双向绑定MVVM-1

    本文能帮你做什么?1.了解vue的双向数据绑定原理以及核心代码模块2.缓解好奇心的同时了解如何实现双向绑定为了便于说明原理与实现,本文相关代码主要摘自vue源码, 并进行了简化改造,相对较简陋,并未考 ...

  4. Beats Solo3 Wireless 无法链接 MacBook pro

    Beats Solo3 Wireless 无法链接 MacBook pro 问题解决了,原来只要长按耳机的开关按钮就能被识别到了,貌似需要5秒钟不松手. https://bbs.feng.com/re ...

  5. J2EE十三种技术规范介绍

    J2EE的十三个技术规范 J2EE体系结构 一.JDBC:Java Data Base Connectivity,数据库连接 我们大家对微软公司的ODBC数据库访问接口比较熟悉,而在Java中创建数据 ...

  6. angularjs 常用功能练习

    <!DOCTYPE html> <html ng-app="app"> <head> <meta charset="utf-8& ...

  7. 【数据库_Postgresql】sql查询结果添加序号列

    ROW_NUMBER () OVER (ORDER BY A .ordernumber ASC) AS 序号

  8. Contest 9

    A:搜索好难啊根本不会啊. B:暴力枚举给哪段前缀乘,维护一些前后缀最大最小值之类的东西就很好算了. #include<iostream> #include<cstdio> # ...

  9. eclipse启动报错: No Java virtual machine

    在 scala-ide下载集成scala包的eclipse版本使用,启动时报错: A java runtime environment (JRE) or java development kit (J ...

  10. RDD 算子补充

    一.RDD算子补充 1.mapPartitions         mapPartitions的输入函数作用于每个分区, 也就是把每个分区中的内容作为整体来处理.   (map是把每一行) mapPa ...