Blocks
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

方法1:
//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 组合数学]的更多相关文章

  1. 矩阵优化dp

    链接:https://www.luogu.org/problemnew/show/P1939 题解: 矩阵优化dp模板题 搞清楚矩阵是怎么乘的构造一下矩阵就很简单了 代码: #include < ...

  2. bzoj 3120 矩阵优化DP

    我的第一道需要程序建矩阵的矩阵优化DP. 题目可以将不同的p分开处理. 对于p==0 || p==1 直接是0或1 对于p>1,就要DP了.这里以p==3为例: 设dp[i][s1][s2][r ...

  3. HDU - 2294: Pendant(矩阵优化DP&前缀和)

    On Saint Valentine's Day, Alex imagined to present a special pendant to his girl friend made by K ki ...

  4. [六省联考2017]组合数问题 (矩阵优化$dp$)

    题目链接 Solution 矩阵优化 \(dp\). 题中给出的式子的意思就是: 求 nk 个物品中选出 mod k 为 r 的个数的物品的方案数. 考虑朴素 \(dp\) ,定义状态 \(f[i][ ...

  5. [Sdoi2017]序列计数 矩阵优化dp

    题目 https://www.lydsy.com/JudgeOnline/problem.php?id=4818 思路 先考虑没有质数限制 dp是在同余系下的,所以\(f[i][j]\)表示前i个点, ...

  6. bzoj 1009 [HNOI2008]GT考试——kmp+矩阵优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1009 首先想到 确保模式串不出现 就是 确保每个位置的后缀不是该模式串. 为了dp,需要记录 ...

  7. 矩阵优化DP类问题应用向小结

    前言 本篇强调应用,矩阵的基本知识有所省略(也许会写篇基础向...). 思想及原理 为什么Oier们能够想到用矩阵来加速DP呢?做了一些DP题之后,我们会发现,有时候DP两两状态之间的转移是定向的,也 ...

  8. 洛谷P3193 GT考试 kmp+矩阵优化dp

    题意 求\(N\)位数字序列(可以有前导0)中不出现某\(M\)位子串的个数,模\(K\). \(N<=10^9,M<=20,K<=1000\) 分析 设\(dp[i][j]\)表示 ...

  9. $[TJOI2017]$ 可乐 矩阵优化$dp$

    \(Sol\) 设\(f_i\)为到第\(i\)秒的方案数,显然\(f_i=\)在第\(i\)秒前爆炸的方案数+在第\(i\)秒爆炸的方案数+在第\(i\)秒停下的方案数+在第\(i\)秒走向下一个城 ...

随机推荐

  1. REST构架风格介绍:状态表述转移(转)

    本文总结了一些REST风格构架设计的经验,介绍了REST架构的语言生态环境,状态表述转移,无状态服务器等特点,并通过举例Google来说明REST风格的味道. REST(Representationa ...

  2. C++中的类继承之单继承&多继承&菱形继承

     C++中的类继承之单继承&多继承&菱形继承 单继承是一般的单一继承,一个子类只 有一个直接父类时称这个继承关系为单继承.这种关系比较简单是一对一的关系: 多继承是指 一个子类有两个或 ...

  3. iOS开发小技巧--cell往左拖拽出现很多按钮的实现,仅仅适用于iOS8以后

    // 往左拖拽cell出现多个按钮的实现,仅仅适用于iOS_8.0以后 - (NSArray<UITableViewRowAction *> *)tableView:(UITableVie ...

  4. Sphinx速成指南

    目录 1. Sphinx简介 1.1. 什么是全文检索 1.2. 介绍 1.3. Sphinx的特性 2. Sphinx安装(For MySQL) 2.1. Windows下安装 2.2. Linux ...

  5. js学习笔记29----拖拽

    原理:先计算鼠标与拖拽目标的左侧距离 跟 上面距离,再计算拖动后的位置. 示例代码: <!DOCTYPE html> <html lang="en"> &l ...

  6. 随机数Math.random()公式

    1. 0-x之间的随机数: Math.round(Math.random()*x); 2. x至y之间的随机数 Math.round(Math.random()*(y-x)+x); 3. 1-x之间的 ...

  7. Spring 4 官方文档学习(十)数据访问之ORM

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/orm.html 占位用,暂略.

  8. MFC HTTP GET 请求

    //HTTP GET 下载文件 CInternetSession *pInetSession = new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_I ...

  9. VS2008兼容安装

    1. 直接安装出现问题:该项目的所有配置都需要系统提供对某些 平台的支持,但在此计算机上没有安装这些平台.因此无法加载该项目. 解决方法:先卸载原来安装的, 再设置安装包中的setup.exe的兼容性 ...

  10. 使用ffmpeg获取视频流后如何封装存储成mp4文件

    int main(int argc,char *argv[]) 02 { 03  AVFormatContext *pFormatCtx; 04  int i,videoStream; 05  AVC ...