【矩阵乘法】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 < ...
随机推荐
- idea 调试远程tomcat
# ----- Execute The Requested Command ----------------------------------------- JAVA_OPTS="-age ...
- 【Windows使用笔记】使Onedrive同步任意文件夹
因为度盘实在是有点垃圾,经常看的剧之类的或者其他软件资源啥的动不动就被封. 所以跑去某宝买了一个5T的企业子账号,安全性不清楚,重要的隐私数据反正都用移动硬盘备份了.主要就是存一些资源性的文件吧.而且 ...
- printk %pF %pS含义【转】
作者:啐楼链接:https://www.zhihu.com/question/37769890/answer/73532192来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...
- 给自己立一个flag
工作理念:做完!做对!做好!做优! 1.请教问题方面 遇到问题先自己想办法解决(限定时长为30分钟). 请教问题的时候,明确:“问题是什么,为什么错在那里,结果是什么” 2.博客 一周两篇左右:对工作 ...
- 使用while循环遍历文件
/* 使用while循环遍历文件*/ [root@localhost test1]# vim 17.py //add #!/usr/bin/python ll = open('/tmp/1.txt') ...
- Assistor PS 切图工具的使用说明。
一.如何运行Assistor PS 使用这个Assistor PS 软件有一个最最重要的条件,那就是:你要打开你的Photoshop (官方建议版本在CS 3以上) 下载-安装-运行. 运行成 ...
- mac系统安装mysql
1.下载 打开官网:https://www.mysql.com 进入DOWNLOADS--->Community--->MySQL Community Server--->DOWNL ...
- NEERC 2016-2017 Probelm G. Game on Graph
title: NEERC 2016-2017 Probelm G. Game on Graph data: 2018-3-3 22:25:40 tags: 博弈论 with draw 拓扑排序 cat ...
- django celery异步框架
描述:实现运维平台的异步执行与定时任务,以下简单描述了安装过程及使用. 安装django和celery pip install django pip install celery pip inst ...
- 深入解析当下大热的前后端分离组件django-rest_framework系列四
查漏补缺系列 解析器 request类 django的request类和rest-framework的request类的源码解析 局部视图 from rest_framework.parsers im ...