Blocks(POJ 3734)
- 原题如下:
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
2Sample 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)的更多相关文章
- [POJ 3734] Blocks (矩阵高速幂、组合数学)
Blocks Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3997 Accepted: 1775 Descriptio ...
- Blocks(POJ 3734 矩阵快速幂)
Blocks Input The first line of the input contains an integer T(1≤T≤100), the number of test cases. E ...
- POJ 3734 Blocks (矩阵快速幂)
题目链接 Description Panda has received an assignment of painting a line of blocks. Since Panda is such ...
- poj 3734 Blocks
ゲート 分析:这题过的人好多,然后大家好像是用矩阵过的(((φ(◎ロ◎;)φ))).我自己是推公式的. 对于任意的有这个式子, 就是先从里面选偶数个涂成两个指定的颜色,再在选出的里面选定涂某种颜色,选 ...
- poj 3734 Blocks 快速幂+费马小定理+组合数学
题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...
- POJ 3734 Blocks(矩阵快速幂+矩阵递推式)
题意:个n个方块涂色, 只能涂红黄蓝绿四种颜色,求最终红色和绿色都为偶数的方案数. 该题我们可以想到一个递推式 . 设a[i]表示到第i个方块为止红绿是偶数的方案数, b[i]为红绿恰有一个是偶数 ...
- POJ 3734 Blocks (线性递推)
定义ai表示红色和绿色方块中方块数为偶数的颜色有i个,i = 0,1,2. aij表示刷到第j个方块时的方案数,这是一个线性递推关系. 可以构造递推矩阵A,用矩阵快速幂求解. /*********** ...
- 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 ...
- POJ 3734 Blocks 矩阵递推
POJ3734 比较简单的递推题目,只需要记录当前两种颜色均为偶数, 只有一种颜色为偶数 两种颜色都为奇数 三个数量即可,递推方程相信大家可以导出. 最后来个快速幂加速即可. #include< ...
随机推荐
- 为什么你的 64-bit 程序可能占用巨大的虚拟空间
出于很多目的,我从最新的 Go 系统内核开发源码复制了一份代码,在一个正常的运行环境中构建(和重新构建)它,在构建版本基础上周期性地重新构建 Go 程序.近期我在用 ps 查看我的一个程序的内存使用情 ...
- Android 开发学习进程0.15 adb cardview framelayout 控件设置状态获取焦点
Android设备调试桥 即adb 使用adb进行无线调试的一些常用命令 adb tcpip 5555 设置调试端口为5555 防止冲突 adb shell ifconfig wlan0 查询局域网中 ...
- 初始JAVA第十五章String的总结
字符串的概述 1.什么是字符串:零个或多个字符组成的有限序列 2.如何使用字符串:(使用字符串分为两步) 1)定义并初始化字符串 2)使用字符,对字符串进行一些处理 // 穿件一个字符串 // 语法 ...
- ubuntu18.04配置与美化
一:初步系统配置 1 不可或缺的更新 如果在上一步中勾选了安装 Ubuntu 时下载更新,那么大部分的系统更新已经下载完毕. 不过为了确保,先移步到 设置→详细信息 ,点击右下角的 检查更新 ,如果存 ...
- 金题大战Vol.0 C、树上的等差数列
金题大战Vol.0 C.树上的等差数列 题目描述 给定一棵包含\(N\)个节点的无根树,节点编号\(1-N\).其中每个节点都具有一个权值,第\(i\)个节点的权值是\(A_i\). 小\(Hi\)希 ...
- PythonCrashCourse 第八章习题
编写一个名为display_message() 的函数,它打印一个句子,指出你在本章学的是什么.调用这个函数,确认显示的消息正确无误 def display_message(): print(&quo ...
- extJS--尚
ExtJS依赖JavaScript,JavaScript推荐两本书:<JavaScript高级程序设计>初阶阶段, <JavaScript设计模式>中级阶段 fun1();// ...
- python基础 Day8
python Day8 文件操作的识 利用python代码写一个脚本操作文件的过程 文件的路径:path 打开方式:读,写,追加,读写,写读 编码方式:utf-8,gbk,gb2312 简单文件读取( ...
- .NET 设计模式 思维导图
关于.NET 设计模式 思维导图 背景说明 以前都在匆匆忙忙写代码,在无穷无尽的需求中间左冲右突,最近终于有一些闲暇的时间,来总结和思考编程中的一些核心思想,磨刀不误砍柴的功夫,期望通过总结和学习,能 ...
- Kafka内部实现原理
Kafka是什么 在流式计算中,Kafka一般用来缓存数据,Storm通过消费Kafka的数据进行计算. 1)Apache Kafka是一个开源消息系统,由Scala写成.是由Apache软件基金会开 ...