题目链接

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. psp 第二周

    11号                                                                              12号 类别c 内容c 开始时间s 结 ...

  2. Eureka服务注册过程

    上篇博客<SpringCloud--Eureka服务注册和发现>介绍了Eureka的基本功能,这篇我们来聊聊eureka是如何实现的. 上图是eureka的架构图,Eureka分为Serv ...

  3. 【转】mysql force Index 强制索引

    其他强制操作,优先操作如下: mysql常用的hint 对于经常使用oracle的朋友可能知道,oracle的hint功能种类很多,对于优化sql语句提供了很多方法.同样,在mysql里,也有类似的h ...

  4. 面试- 阿里-. 大数据题目- 给定a、b两个文件,各存放50亿个url,每个url各占64字节,内存限制是4G,让你找出a、b文件共同的url?

    假如每个url大小为10bytes,那么可以估计每个文件的大小为50G×64=320G,远远大于内存限制的4G,所以不可能将其完全加载到内存中处理,可以采用分治的思想来解决. Step1:遍历文件a, ...

  5. vue & $data & data

    vue & $data & data vm.a === vm.$data.a https://vuejs.org/v2/api/#data https://flaviocopes.co ...

  6. 【EF】Entity Framework Core 软删除与查询过滤器

    本文翻译自<Entity Framework Core: Soft Delete using Query Filters>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 注意 ...

  7. dom变成jquery对象 先获取dom对象 然后通过$()转换成jquery对象

    dom变成jquery对象   先获取dom对象 然后通过$()转换成jquery对象

  8. wp开发(一)--应用发布篇

    本文非常简单,适合刚刚刚刚入门的菜鸟,且针对的是wp8版本.wp8应用的发布总体来说没什么难度,只是有几个值得注意的地方,希望本文可以减少菜鸟们不必要的担心. 首先假设项目已经完成,且要发布到应用商城 ...

  9. Linux 查询命令

    which       查看可执行文件的位置 whereis    查看文件的位置 locate       配合数据库查看文件位置 find          实际搜寻硬盘查询文件名称 (find也 ...

  10. 【bzoj4036】按位或

    Portal --> bzoj4036 Solution  感觉容斥的东西内容有点qwq多啊qwq还是以题目的形式来慢慢补档好了  这里补的是min-max容斥 ​    其实min-max容斥 ...