UVa 3704 Cellular Automaton(矩乘)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15129
【思路】
矩阵乘法-循环矩阵
题目中的转移矩阵是一个循环矩阵,循环矩阵的乘积依旧是循环矩阵,这样保留矩阵第一行进行快速幂乘法即可。
【代码】
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std; typedef long long LL;
const int N = +; int m,n,d,k; void mul(LL a[],LL b[]) {
LL c[N];
for(int i=;i<n;i++) {
c[i]=;
for(int j=;j<n;j++)
c[i]=(c[i]+a[j]*b[(i-j+n)%n])%m;
}
memcpy(a,c,sizeof(c));
}
LL ans[N],tmp[N],A[N]; void pow(int p) {
memset(ans,,sizeof(ans));
memset(tmp,,sizeof(tmp));
for(int i=-d;i<d+;i++) {
tmp[(i+n)%n]=;
}
ans[]=;
while(p) {
if(p&) mul(ans,tmp);
mul(tmp,tmp);
p>>=;
}
} int main() {
while(scanf("%d%d%d%d",&n,&m,&d,&k)==) {
pow(k);
for(int i=;i<n;i++) scanf("%d",&A[i]);
mul(ans,A);
printf("%lld",ans[]);
for(int i=;i<n;i++) printf(" %lld",ans[i]);
putchar('\n');
}
return ;
}
UVa 3704 Cellular Automaton(矩乘)的更多相关文章
- UVA 1386 - Cellular Automaton(循环矩阵)
UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...
- LA 3704 Cellular Automaton
题意概述: 等价地,本题可以转化为下面的问题: 考虑$n \times n$的$0-1$矩阵$A$,在第$i$行上第$[-d+i, d+i]$(模$n$意义下)列对应的元素为$1$,其余为$0$.求$ ...
- UVA 1386 Cellular Automaton
矩阵快速幂. 样例是这样构造矩阵的: 矩阵很好构造,但是500*500*500*logk的复杂度显然是无法通过这题的. 其实本题构造出来的矩阵是一个循环矩阵,只需直到第一行或者第一列,即可直到整个矩阵 ...
- UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)
题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离 ...
- UVA1386 【Cellular Automaton】题解
题面:UVA1386 Cellular Automaton 矩阵乘法+快速幂解法: 这是一个比较裸的有点复杂需要优化的矩乘快速幂,所以推荐大家先做一下下列洛谷题目练练手: (会了,差不多就是多倍经验题 ...
- 【POJ】3150 Cellular Automaton(矩阵乘法+特殊的技巧)
http://poj.org/problem?id=3150 这题裸的矩阵很容易看出,假设d=1,n=5那么矩阵是这样的 1 1 0 0 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 ...
- [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3048 Accepted: 12 ...
- POJ 3150 Cellular Automaton(矩阵快速幂)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3504 Accepted: 1421 C ...
- POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)
A cellular automaton is a collection of cells on a grid of specified shape that evolves through a nu ...
随机推荐
- 一个操作Sql2005数据库的类(备份,还原,分离,附加,添加删除用户等操作)(转载)
/* * 更新时间 :2011-09-01 16:06 * 更 新 人 :苏飞 */ using System; using System.Collections.Generic; using Sys ...
- POJ_1088 滑雪(记忆型DP+DFS)
Description Michael喜欢滑雪,这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...
- Lua与C/C++交互问题
初学lua,遇到注册C/C++交互函数问题 在lua与C/C++交互时,C/C++的注册Lua函数若是一个有返回类型(压栈)而不是获取类型的时候应该返回1而不是返回0,否则会出现在Lua中值为nil( ...
- 分享一个自己写的基于TP的关系模型(三)
这段时间对模型做了升级和优化,并将版本更新到TP3.2. 下载 下载后请将目录放置TP的Library目录下 1.数据节点优化,原来的节点为模型的名称或者表名,现在更新为定义关系的方法名 public ...
- QTableView使用自定义委托(QItemDelegate)
需要在表格中绘制流程图,主要有箭头,方向,颜色,字符串,由于QTableView没有可用的绘制函数,所以需要自己去定义. 委托(delegate)继承QItemDelegate,模型(model)继承 ...
- jQuery实现的分页功能,包括ajax请求,后台数据,有完整demo
一:需求分析 1)需要首页,末页功能 2)有点击查看上一页,下一页功能 3)页码到当前可视页码最后一页刷新页面 二:功能实现思路 也是分为三部分处理 1)点击首页,末页直接显示第一页或者最后一页内容, ...
- 新增的output元素 progress元素 meter元素 keygen元素
结果图 <output>是双标签 name:定义对象的唯一属性 for:定义输出域相关的一个或多个元素. form:定义所属的一个至多个表单. progress和meter一般和JS一起使 ...
- wamp安装注意点!
安装wamp前或者重装系统后,默认没有依赖的组件VC11,需要先安装才能运行 下载地址:http://www.microsoft.com/en-us/download/details.aspx?id= ...
- ProfessionalKnowledgeArchitecture
- Find your present (2) (位异或)
Problem Description In the new year party, everybody will get a "special present".Now it's ...