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< ...
随机推荐
- Mware中CentOS设置静态IP
Mware中CentOS设置静态IP 因为之前搭建的MongoDB分片没有采用副本集,最近现网压力较大,所以准备研究一下,于是在自己电脑的虚拟机中搭建环境,但是发现之前VMware设置的是DHCP ...
- 微信Web开发者工具-下载、安装和使用图解
开发和测试小程序,需要借助微信官方提供的微信Web开发者工具进行预览和调试代码,从下载安装到使用,大致的流程如下: 1.下载安装包 下载地址传送门:https://developers.weixin. ...
- Windows下多线程编程(一)
前言 熟练掌握Windows下的多线程编程,能够让我们编写出更规范多线程代码,避免不要的异常.Windows下的多线程编程非常复杂,但是了解一些常用的特性,已经能够满足我们普通多线程对性能及其他要求. ...
- ES2015中的解构赋值
ES2015中允许按照一定的模式,从数组和对象中提取值,对变量进行赋值,被称为”解构(Destructering)“. 以前,为变量赋值,只能指定值. /** * 以前,为变量赋值,只能直接指定值 * ...
- 【明哥报错簿】tomcat 安装时出现 Failed to install Tomcat7 service
安装tomcat时提示 Failed to install Tomcat7 service 应该是卸载时直接删除目录导致的. Failed to install Tomcat7 service Che ...
- Java操作excel(POI)
由于在项目中使用了将excel数据导入到数据库.在这里分享一下. 这里使用的POI方式,支持两种格式(xls,xlsx) package com.entity; import java.io.File ...
- bzoj3173
Description 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? Input 第一行一 ...
- Windows用户相关操作
获取所有用户 NET_API_STATUS NetUserEnum( LPCWSTR servername, DWORD level, DWORD filter, LPBYTE* bufptr, DW ...
- 洛谷P4559 [JSOI2018]列队 【70分二分 + 主席树】
题目链接 洛谷P4559 题解 只会做\(70\)分的\(O(nlog^2n)\) 如果本来就在区间内的人是不用动的,区间右边的人往区间最右的那些空位跑,区间左边的人往区间最左的那些空位跑 找到这些空 ...
- 【读书笔记】《HTTP权威指南》:Web Hosting
一.概述 从零开始构建一个真正意义的网站需要做很多事情,包括购买计算机硬件.建造机房.注册域名.购买网络带宽.开发Web服务器软件.管理网站内容等等.在互联网发展的早期,构建网站的这一系列动作通常都是 ...