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

  1. 2
  2. 1
  3. 2

Sample Output

  1. 2
  2. 6

Source

方法1:
  1. //f[i]=6*f[i-1]-8*f[i-2]{i>=3,f[1]=2,f[2]=6}
  2. #include<cstdio>
  3. #include<cstring>
  4. typedef long long ll;
  5. using namespace std;
  6. const ll mod=;
  7. struct matrix{
  8. ll s[][];
  9. matrix(){
  10. memset(s,,sizeof s);
  11. }
  12. }A,F;int n,T;
  13. matrix operator *(const matrix &a,const matrix &b){
  14. matrix c;
  15. for(int i=;i<;i++){
  16. for(int j=;j<;j++){
  17. for(int k=;k<;k++){
  18. c.s[i][j]+=a.s[i][k]*b.s[k][j];
  19. c.s[i][j]%=mod;
  20. }
  21. }
  22. }
  23. return c;
  24. }
  25. matrix fpow(matrix a,int p){
  26. matrix res;
  27. for(int i=;i<;i++) res.s[i][i]=;
  28. for(;p;p>>=,a=a*a) if(p&) res=res*a;
  29. return res;
  30. }
  31. int main(){
  32. for(scanf("%d",&T);T--;){
  33. scanf("%d",&n);
  34. if(n==){puts("");continue;}
  35. if(n==){puts("");continue;}
  36. A.s[][]=;A.s[][]=-;
  37. A.s[][]=;A.s[][]=;
  38. F.s[][]=;F.s[][]=;
  39. F.s[][]=;F.s[][]=;
  40. A=fpow(A,n-);
  41. F=A*F;
  42. printf("%lld\n",(F.s[][]+mod)%mod);
  43. }
  44. return ;
  45. }

方法2:

  1. //f(n)=2^(2n-2)+2^(n-1)
  2. #include<cstdio>
  3. #include<cstring>
  4. typedef long long ll;
  5. using namespace std;
  6. const ll mod=;
  7. ll ans=;int T,n;
  8. ll fpow(ll a,ll p){
  9. ll res=;
  10. for(;p;p>>=,a=a*a%mod) if(p&) res=res*a%mod;
  11. return res;
  12. }
  13. int main(){
  14. for(scanf("%d",&T);T--;){
  15. scanf("%d",&n);
  16. ans=fpow(,n-<<)+fpow(,n-);
  17. printf("%I64d\n",(ans+mod)%mod);
  18. }
  19. return ;
  20. }

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. mysql数据库对时间进行默认的设置

    //----------------------------------------------------------sql语句----------------------------------- ...

  2. ggplot饼图

    目录: 原始图样 如何去除饼图中心的杂点 如何去除饼图旁边的标签 如何去掉左上角多出来的一横线 如何去掉图例的标题,并将图例放到上面 如何对图例的标签加上百分比 如何让饼图的小块按顺时针从大到小的顺序 ...

  3. C++/C语言的标准库函数与运算符的区别new/delete malloc/free

    malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符.它们都可用于申请动态内存和释放内存.下面来看他们的区别. 一.操作对象有所不同 malloc与free是C++ ...

  4. Netstat命令(windows下)

    功能: 一般用于检验本机各端口的网络连接情况. 例子:检查本机3389远程连接端口是否可用 netstat -nao|find  "3389" 查看某进程使用的端口号: netst ...

  5. e676. 把彩色图像转换为灰色

    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); ColorConvertOp op = new ColorConvertOp(c ...

  6. JavaScript 学习笔记(二)

    学习内容: 一.变量的定义 二.JS的基本数据类型 三.JS的基本语法 1.变量的定义:   任何语言中最基本的东西就属于变量了,那么如何定义变量以及运用,其实是一件很简单的事情.. JS中提供了如何 ...

  7. javascript -- addEventListener()和removeEventListener

    addEventListener()与removeEventListener()用于处理指定和删除事件处理程序操作.所有的DOM节点中都包含这两种方法,并且它们都接受3个参数:要处理的事件名.作为事件 ...

  8. bootstrap -- css -- 按钮

    本文中提到的按钮样式,适用于:<a>, <button>, 或 <input> 元素上 但最好在 <button> 元素上使用按钮 class,避免跨浏 ...

  9. 防止 apk反编译 jocky-- java混淆代码 (转至:http://my.oschina.net/f839903061/blog/72554)

    1.下载jocky,解压后把整个文件夹复制到Eclipse的plugin目录.2.重启Eclipse,在项目上点右键,如果出现jocky菜单,则安装成功. 3.在项目上点右键,选菜单jocky-> ...

  10. 截取scrollview的滑动事件,传递给子控件

    重写一个ScrollView public class MyScrollView extends ScrollView{ public MyScrollView(Context context, At ...