POJ 3734 Blocks (矩阵快速幂)
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 (矩阵快速幂)的更多相关文章
- [POJ 3734] Blocks (矩阵高速幂、组合数学)
Blocks Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3997 Accepted: 1775 Descriptio ...
- POJ 3744 【矩阵快速幂优化 概率DP】
搞懂了什么是矩阵快速幂优化.... 这道题的重点不是DP. /* 题意: 小明要走某条路,按照个人兴致,向前走一步的概率是p,向前跳两步的概率是1-p,但是地上有地雷,给了地雷的x坐标,(一维),求小 ...
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- POJ——3070Fibonacci(矩阵快速幂)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12329 Accepted: 8748 Descri ...
- POJ 3070 Fibonacci 矩阵快速幂模板
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18607 Accepted: 12920 Descr ...
- poj 3735 稀疏矩阵矩阵快速幂
设人数为 $n$,构造 $(n + 1) \times (n + 1)$ 的矩阵 得花生:将改行的最后一列元素 $+ 1$ \begin{gather}\begin{bmatrix}1 & 0 ...
- POJ 3070 Fibonacci矩阵快速幂 --斐波那契
题意: 求出斐波那契数列的第n项的后四位数字 思路:f[n]=f[n-1]+f[n-2]递推可得二阶行列式,求第n项则是这个矩阵的n次幂,所以有矩阵快速幂模板,二阶行列式相乘, sum[ i ] [ ...
- POJ 3613 floyd+矩阵快速幂
题意: 求s到e恰好经过n边的最短路 思路: 这题已经被我放了好长时间了. 原来是不会矩阵乘法,快速幂什么的也一知半解 现在终于稍微明白了点了 其实就是把矩阵乘法稍微改改 改成能够满足结合律的矩阵&q ...
- POJ 3734 Blocks 矩阵递推
POJ3734 比较简单的递推题目,只需要记录当前两种颜色均为偶数, 只有一种颜色为偶数 两种颜色都为奇数 三个数量即可,递推方程相信大家可以导出. 最后来个快速幂加速即可. #include< ...
随机推荐
- 【beta】Scrum站立会议第7次....11.9
小组名称:nice! 组长:李权 成员:于淼 刘芳芳韩媛媛 宫丽君 项目内容:约跑app(约吧) 时间:2016.11.9 12:00——12:30 地点:传媒西楼220室 本次对beta阶段 ...
- 【vue】vue安装卡住/报错
网上有很多教程怎么安装: 安装cnpmnpm install -g cnpm --registry=https://registry.npm.taobao.org 升级npmcnpm install ...
- ubuntu 手动apache记录
1.下载apache tar -xvzf httpd.xx 解压 2.下载安装pcre Download PCRE from PCRE.org 解压,进入文件夹中 ./configure --pre ...
- 第204天:js---重载和多态
一.根据arguments个数实现重载 js本身不支持重载,所以只能通过其他方式实现,arguments检测传参的个数,然后再执行不同的方式 function add() { var sum = 0 ...
- [乱搞]hdu 6406 Taotao picks apples 笛卡尔树+倍增
题目链接 Problem Description There is an apple tree in front of Taotao's house. When autumn comes, n app ...
- CSS布局-垂直居中问题
在前端页面的布局方面,很多时候需要用到垂直居中,如果父集元素或子元素的高度是固定的那么垂直居中可以用line-height或者定位 top:50%:margin-top:- height/2 px:实 ...
- xshell代理设置
1.宿主机设置隧道 上面的端口随意,不与本机使用的端口重合即可,下面的端口是管理系统的端口 2.宿主机上面创建的虚机设置代理 3.怎么通过web浏览器直接登录虚机ip网址:https://blog.c ...
- Cornfields POJ - 2019(二维RMQ板题)
就是求子矩阵中最大值与最小值的差... 板子都套不对的人.... #include <iostream> #include <cstdio> #include <sstr ...
- [洛谷P5147]随机数生成器
题目大意:$$f_n=\begin{cases}\frac{\sum\limits_{i=1}^nf_i}n+1&(n>1)\\0&(n=1)\end{cases}$$求$f_n ...
- HBase多租户机制分析
在HBase1.1.0发布之前,HBase同一集群上的用户.表都是平等的,没有优劣之分.这种’大同’社会看起来完美,实际上有很多问题.最棘手的主要有这么两个,其一是某些业务较其他业务重要,需要在资源有 ...