【矩阵乘法】Gym - 101412C - One-Dimensional Cellular Automaton
给你一个一维细胞自动机,第i个格子在时刻t的状态是这样获得的
,问你t时刻的状态。
把0时刻的状态视作一个列向量,发现状态转移其实是一个n*n的矩阵(以n=5为例),
| B | C | |||
| A | B | C | ||
| A | B | C | ||
| A | B | C | ||
| A | B |
直接快速幂即可。
#include<cstdio>
#include<vector>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;
int n,m,A,B,C,T;
mat operator * (const mat &A,const mat &B){
mat C(A.size(),vec(B[0].size()));
for(int i=0;i<A.size();++i){
for(int k=0;k<B.size();++k){
for(int j=0;j<B[0].size();++j){
C[i][j]=(C[i][j]+A[i][k]*B[k][j])%m;
}
}
}
return C;
}
mat I;
mat Quick_Pow(mat a,int p){
if(!p){
return I;
}
mat res=Quick_Pow(a,p>>1);
res=res*res;
if(p&1){
res=res*a;
}
return res;
}
int main(){
// freopen("c.in","r",stdin);
while(1){
scanf("%d%d%d%d%d%d",&n,&m,&A,&B,&C,&T);
if(!n && !m && !A && !B && !C && !T){
return 0;
}
mat P(n,vec(1));
int x;
for(int i=0;i<n;++i){
scanf("%d",&x);
P[i][0]=x;
}
I.assign(n,vec(n));
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
I[i][j]=(i==j);
}
}
mat R(n,vec(n));
for(int i=0;i<n;++i){
if(i-1>=0){
R[i][i-1]=A;
}
R[i][i]=B;
if(i+1<n){
R[i][i+1]=C;
}
}
mat ans=Quick_Pow(R,T)*P;
for(int i=0;i<n-1;++i){
printf("%d ",ans[i][0]);
}
printf("%d\n",ans[n-1][0]);
}
return 0;
}
【矩阵乘法】Gym - 101412C - One-Dimensional 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 ...
- UVA 1386 - Cellular Automaton(循环矩阵)
UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...
- 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 ...
- POJ 3150 Cellular Automaton(矩阵高速幂)
题目大意:给定n(1<=n<=500)个数字和一个数字m,这n个数字组成一个环(a0,a1.....an-1).假设对ai进行一次d-step操作,那么ai的值变为与ai的距离小于d的全部 ...
- POJ 3150 Cellular Automaton --矩阵快速幂及优化
题意:给一个环,环上有n块,每块有个值,每一次操作是对每个点,他的值变为原来与他距离不超过d的位置的和,问k(10^7)次操作后每块的值. 解法:一看就要化为矩阵来做,矩阵很好建立,大白书P157页有 ...
- POJ3150—Cellular Automaton(循环矩阵)
题目链接:http://poj.org/problem?id=3150 题目意思:有n个数围成一个环,现在有一种变换,将所有距离第i(1<=i<=n)个数小于等于d的数加起来,对m取余,现 ...
- UVA1386 【Cellular Automaton】题解
题面:UVA1386 Cellular Automaton 矩阵乘法+快速幂解法: 这是一个比较裸的有点复杂需要优化的矩乘快速幂,所以推荐大家先做一下下列洛谷题目练练手: (会了,差不多就是多倍经验题 ...
- 「BZOJ2510」弱题(矩阵乘法,降维)
有M个球,一开始每个球均有一个初始标号,标号范围为1-N且为整数,标号为i的球有ai个,并保证Σai = M. 每次操作等概率取出一个球(即取出每个球的概率均为1/M),若这个球标号为k(k < ...
随机推荐
- document的属性与方法小结
document节点是文档的根节点,每张网页都有自己的document节点.属性:1:document.doctype----它是一个对象,包含了当前文档类型 (Document Type Decla ...
- 边绘边理解prototype跟__proto__
网上流传着一张讲解prototype跟__proto__关系的图,尽管他已经描绘的很清楚了,但对于初学者来说,江太公感觉还是过于纠结,于是起心重绘,让他们之间的关系更加明晰可理解,一方面出于分享目的, ...
- Android控件——ImageView
android:orientation="vertical" 修改布局文件垂直排列 放置图片: 1.通过src引入图片: 2.通过background引入背景图片 3.baco ...
- Xmind 8 update5 破解
Step 1. Download XMind Step 2. Run XMind at least once after installation, xmind will init the confi ...
- python基础===理解Class的一道题
解题如下: from random import randint class Die(): def __init__(self,sides=6): self.sides = sides def rol ...
- 利用keepalive+mysql replication 实现数据库的高可用
利用keepalive+mysql replication 实现数据库的高可用 http://www.xuchanggang.cn/archives/866.html
- 【sam复习】用sam实现后缀排序
没错,一定是无聊到一定境界的人才能干出这种事情. 这个无聊的zcysky已经不满足于用后缀平衡树求sa了,他想用sam试试. 我们回顾下sam的插入过程,如果我们从最后一个state沿着suffix ...
- ado中dispose和close的区别,摘自网络
Close() and Dispose() are basically the same thing on an ADO.NET connection object for providers shi ...
- MYSQL5.5源码安装 linux下
/* 首先安装必要的库 */ yum -y install gcc* ###### 安装 MYSQL ###### 首先安装camke 一.支持YUM,则 yum install -y cmake 二 ...
- Redis、mongdb、memcached的个人总结
有测试的实例:http://colbybobo.iteye.com/blog/1986786 详细描述优缺点:https://www.cnblogs.com/binyue/p/4582550.html