poj3734 Blocks[矩阵优化dp or 组合数学]
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6578 | Accepted: 3171 |
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
Source
//f[i]=6*f[i-1]-8*f[i-2]{i>=3,f[1]=2,f[2]=6}
#include<cstdio>
#include<cstring>
typedef long long ll;
using namespace std;
const ll mod=;
struct matrix{
ll s[][];
matrix(){
memset(s,,sizeof s);
}
}A,F;int n,T;
matrix operator *(const matrix &a,const matrix &b){
matrix c;
for(int i=;i<;i++){
for(int j=;j<;j++){
for(int k=;k<;k++){
c.s[i][j]+=a.s[i][k]*b.s[k][j];
c.s[i][j]%=mod;
}
}
}
return c;
}
matrix fpow(matrix a,int p){
matrix res;
for(int i=;i<;i++) res.s[i][i]=;
for(;p;p>>=,a=a*a) if(p&) res=res*a;
return res;
}
int main(){
for(scanf("%d",&T);T--;){
scanf("%d",&n);
if(n==){puts("");continue;}
if(n==){puts("");continue;}
A.s[][]=;A.s[][]=-;
A.s[][]=;A.s[][]=;
F.s[][]=;F.s[][]=;
F.s[][]=;F.s[][]=;
A=fpow(A,n-);
F=A*F;
printf("%lld\n",(F.s[][]+mod)%mod);
}
return ;
}
方法2:
//f(n)=2^(2n-2)+2^(n-1)
#include<cstdio>
#include<cstring>
typedef long long ll;
using namespace std;
const ll mod=;
ll ans=;int T,n;
ll fpow(ll a,ll p){
ll res=;
for(;p;p>>=,a=a*a%mod) if(p&) res=res*a%mod;
return res;
}
int main(){
for(scanf("%d",&T);T--;){
scanf("%d",&n);
ans=fpow(,n-<<)+fpow(,n-);
printf("%I64d\n",(ans+mod)%mod);
}
return ;
}
poj3734 Blocks[矩阵优化dp or 组合数学]的更多相关文章
- 矩阵优化dp
链接:https://www.luogu.org/problemnew/show/P1939 题解: 矩阵优化dp模板题 搞清楚矩阵是怎么乘的构造一下矩阵就很简单了 代码: #include < ...
- bzoj 3120 矩阵优化DP
我的第一道需要程序建矩阵的矩阵优化DP. 题目可以将不同的p分开处理. 对于p==0 || p==1 直接是0或1 对于p>1,就要DP了.这里以p==3为例: 设dp[i][s1][s2][r ...
- HDU - 2294: Pendant(矩阵优化DP&前缀和)
On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K ki ...
- [六省联考2017]组合数问题 (矩阵优化$dp$)
题目链接 Solution 矩阵优化 \(dp\). 题中给出的式子的意思就是: 求 nk 个物品中选出 mod k 为 r 的个数的物品的方案数. 考虑朴素 \(dp\) ,定义状态 \(f[i][ ...
- [Sdoi2017]序列计数 矩阵优化dp
题目 https://www.lydsy.com/JudgeOnline/problem.php?id=4818 思路 先考虑没有质数限制 dp是在同余系下的,所以\(f[i][j]\)表示前i个点, ...
- bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 首先想到 确保模式串不出现 就是 确保每个位置的后缀不是该模式串. 为了dp,需要记录 ...
- 矩阵优化DP类问题应用向小结
前言 本篇强调应用,矩阵的基本知识有所省略(也许会写篇基础向...). 思想及原理 为什么Oier们能够想到用矩阵来加速DP呢?做了一些DP题之后,我们会发现,有时候DP两两状态之间的转移是定向的,也 ...
- 洛谷P3193 GT考试 kmp+矩阵优化dp
题意 求\(N\)位数字序列(可以有前导0)中不出现某\(M\)位子串的个数,模\(K\). \(N<=10^9,M<=20,K<=1000\) 分析 设\(dp[i][j]\)表示 ...
- $[TJOI2017]$ 可乐 矩阵优化$dp$
\(Sol\) 设\(f_i\)为到第\(i\)秒的方案数,显然\(f_i=\)在第\(i\)秒前爆炸的方案数+在第\(i\)秒爆炸的方案数+在第\(i\)秒停下的方案数+在第\(i\)秒走向下一个城 ...
随机推荐
- iOS边练边学--(Quartz2D)图片裁剪,带圆环的裁剪
一.图片裁剪,示意图 二.带圆环的图片裁剪示意图
- 清除DataGridView显示的数据
一.DataGridView未绑定数据时清空数据 this.dgv_PropDemo.DataSource = null 二.DataGridView绑定数据时清空数据 DataGridView绑定了 ...
- C/C++-中的sort排序用法
转载于:http://www.cnblogs.com/luorende/p/6121906.htmlSTL中就自带了排序函数sortsort 对给定区间所有元素进行排序 要使用此函数只需用#inclu ...
- 获取表单提交MVC错误信息
if (!ModelState.IsValid) { List<string> Keys = ModelState.Ke ...
- (转)c++多态实现的机制
原文地址:http://blog.csdn.net/zyq0335/article/details/7657465 1 什么是多态?多态性可以简单的概括为“1个接口,多种方法”,在程序运行的过程中才决 ...
- (转)ALSA配置文件(alsa.conf, asoundrc, asound.conf)及其自动加载 And HDMI Adiuo
原文出处:http://blog.sina.com.cn/s/blog_a04184c101010kry.html 警告:错误的EDID会造成HDMI发声异常 #title:box:HDMI Audi ...
- MathType初级教程:怎么安装MathType
MathType 由美国Design Science公司开发,是一款功能强大的数学公式编辑器,它同时支持Windows和Macintosh 两种操作系统,有很好的兼容性,能够在各种文档中加入复杂的数学 ...
- Linux,unix,cygwin,centeros下的tar压缩解压缩命令具体解释
tar Examples: tar -cf archive.tar foo bar # Create archive.tar from files foo and bar. tar -tvf ...
- python2.0_day21_bbs系统评论自动加载+文章创建
day20中我们已经实现了bbs系统的前端展示,后台admin管理,以及前端动态显示顶部\登录和评论的分级展示功能.其中评论的分级展示功能最为复杂.上一节中我们只是在文章明细页面中加了一个button ...
- python2.0_day19_后台数据库设计思路
from django.db import models # Create your models here. from django.contrib.auth.models import User ...