POJ 3734_Blocks
题意:
用红绿蓝黄四种颜色对一序列n个方块涂色,求出绿和红色方块数同时为偶数的染色方案数。mod=10007
分析:
dp+矩阵快速幂
首先明确有三种状态:
- 红和绿均为偶数
- 红和绿只有一个为奇数
- 红和绿均为奇数
设前三种方案数分别为ai,bi,ci,则可以得到以下递推式:
再利用矩阵快速幂求解即可。
代码:
#include<cstdio>
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int mod = 10007;
const int N=3;
struct Matrix
{
int row,cal;
ll m[N][N];
Matrix()
{
row=3, cal=3;
m[0][0]=2, m[0][1]=1, m[0][2]=0;
m[1][0]=2, m[1][1]=2, m[1][2]=2;
m[2][0]=0, m[2][1]=1, m[2][2]=2;
}
};
Matrix init(Matrix a, ll t)
{
for(int i = 0; i < a.row; i++)
for(int j = 0; j < a.cal; j++)
a.m[i][j] = t;
return a;
}
Matrix mul(Matrix a,Matrix b)
{
Matrix ans;
ans.row = a.row, ans.cal = b.cal;
ans = init(ans,0);
for(int i = 0; i < a.row; i++)
for(int j = 0; j < b.cal; j++)
for(int k = 0; k < a.cal; k++)
ans.m[i][j] = (ans.m[i][j] + a.m[i][k] * b.m[k][j])%mod;
return ans;
}
ll quick_pow(ll n)
{
Matrix ans, t;
ans.row=1, ans.cal=3;
ans.m[0][0]=1, ans.m[0][1] = 0, ans.m[0][2]=0;
while(n)
{
if(n&1) ans = mul(ans, t);
t = mul(t, t);
n>>=1;
}
return ans.m[0][0];
}
int main (void)
{
int T;scanf("%d",&T);
int n;
while(T--){
scanf("%d",&n);
printf("%d\n",quick_pow(n));
}
return 0;
}
POJ 3734_Blocks的更多相关文章
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
- poj 2352 Stars 数星星 详解
题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
随机推荐
- Spring注解驱动开发之AOP
前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...
- iOS Programming Subclassing UITableViewCell
iOS Programming Subclassing UITableViewCell 1.Creating BNRItemCell UITableViewCell is a UIView subc ...
- Python学习 Day 9 property 多重继承 Mixin
在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 为了限制score的范围,可以通过一 ...
- SQL Server调试存储过程
一. 调试SQL Server 2000 1. 设置帐户. <1> 在windows服务中找到MSSQLSERVER,双击弹出对话框. <2> 选择 ...
- css定位position属性深究
1.static:对象遵循常规流.此时4个定位偏移属性不会被应用. 2.relative:对象遵循常规流,并且参照自身在常规流中的位置通过top,right,bottom,left这4个定位偏移属性进 ...
- PHP自定义函数及内部函数考察点
变量的作用域和静态变量 变量的作用域 变量的作用域也称变量的范围,变量的范围即它定义的上下文背景(也是它的生效范围).大部分的PHP变量只有一个单独的范围.这个单独的范围跨度同样包含了include和 ...
- Java软件开发不同薪资级别-技术要求
15~20万 WEB应用服务器(Tomcat.Weblogic.Jetty.JBoss.WebSphere) NoSQL(Redis.MongoDB.HBase.Memcache) 消息中间件(Kaf ...
- JavaSE-17 泛型
学习要点 泛型接口 泛型类 泛型方法 多参数泛型类 泛型类的继承 泛型的定义 允许在定义类.接口.方法时使用类型形参,类型形参将会在声明变量.创建对象或者调用方法时候动态指定. 泛型接口 1 定义 ...
- Solaris 默认Shell 修改
ssh登陆远程的solaris 10,backspace出现乱码. ssh登陆远程的solaris 10默认Shell不是bash 把solaris10的shell环境改为bash就行 dev13% ...
- 【PS切图】前端工程师必备,但又无需精通的一项技能。
前端主要从事一些代码开发工作,PS使用是前端工程师必备,但又无需精通的一项技能. 前端切图四大面板:在“窗口”菜单下开启 1,信息(手动开启)2,字符(手动开启)3,历史记录(手动开启)4,图层(默认 ...