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个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
随机推荐
- 来自锐动天地的直播ios SDK
直播iOS SDK,可以在手机iOS端实时采集视频,同时在拍摄过程中支持多种实时滤镜效果,只要调用视频直播接口,通过3G.4G.WIFI等网络,推流发送给云端流媒体直播系统处理,并通过CDN视频加速分 ...
- 在SQLServer 2005附加SQLServer 2008数据库异常处理
远程服务器软件系统不算新,数据库是SQL Server 2005.本地开发基本是用新的软件系统.数据库采用SQL Server 2008. 这样在用远程服务器SQL 2005选择附加SQL 2008的 ...
- Node.js——fs常用API
文件状态 文件删除 文件信息 读取文件夹 文件的截取 创建文件夹 删除目录 文件监视,可以设置监视频率 文件重命名,可以用来剪切文件 注意 fs.open() fs.close() 这是最原始的读写方 ...
- vue2.0 静态prop和动态prop
动态prop: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- numpy基本用法
numpy 简介 numpy的存在使得python拥有强大的矩阵计算能力,不亚于matlab. 官方文档(https://docs.scipy.org/doc/numpy-dev/user/quick ...
- 学习嵌入式开发板的Android平台体系结构和源码结构
本文转自迅为论坛资料:http://www.topeetboard.com 推荐学习嵌入式开发板平台:iTOP-4412开发板 下面这张图出自Google官方,展示了Android系统的主要组成部分. ...
- 用数据集跑一个模型遇到bug如何解决
自己在用fast rcnn和ssd跑自己数据集过程中都遇到了bug,fast rcnn中是loss下降但值较高,并且测试出来结果一直不对,ssd是loss从一开始到后面loss都一直为0. 遇到这种情 ...
- 第2节 mapreduce深入学习:13、mapreduce的整个运行过程(多看几遍)
两个问题: 1.mapTaks的个数怎么确认:与block块相关2.reducetask的个数怎么确认:没法确认,需要反复的设置尝试,找到最优值. 手动进行设置 job.setNumReduceTa ...
- 修改python注册表
转自:http://blog.csdn.net/u014680513/article/details/51005650 # script to register Python 2.0 or later ...
- 【Pytorch】关于torch.matmul和torch.bmm的输出tensor数值不一致问题
发现 对于torch.matmul和torch.bmm,都能实现对于batch的矩阵乘法: a = torch.rand((2,3,10))b = torch.rand((2,2,10))### ma ...