给你一个一维细胞自动机,第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的更多相关文章

  1. 【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 ...

  2. [POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)

    Cellular Automaton Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 3048   Accepted: 12 ...

  3. UVA 1386 - Cellular Automaton(循环矩阵)

    UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...

  4. POJ 3150 Cellular Automaton(矩阵快速幂)

    Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3504 Accepted: 1421 C ...

  5. POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)

    A cellular automaton is a collection of cells on a grid of specified shape that evolves through a nu ...

  6. POJ 3150 Cellular Automaton(矩阵高速幂)

    题目大意:给定n(1<=n<=500)个数字和一个数字m,这n个数字组成一个环(a0,a1.....an-1).假设对ai进行一次d-step操作,那么ai的值变为与ai的距离小于d的全部 ...

  7. POJ 3150 Cellular Automaton --矩阵快速幂及优化

    题意:给一个环,环上有n块,每块有个值,每一次操作是对每个点,他的值变为原来与他距离不超过d的位置的和,问k(10^7)次操作后每块的值. 解法:一看就要化为矩阵来做,矩阵很好建立,大白书P157页有 ...

  8. POJ3150—Cellular Automaton(循环矩阵)

    题目链接:http://poj.org/problem?id=3150 题目意思:有n个数围成一个环,现在有一种变换,将所有距离第i(1<=i<=n)个数小于等于d的数加起来,对m取余,现 ...

  9. UVA1386 【Cellular Automaton】题解

    题面:UVA1386 Cellular Automaton 矩阵乘法+快速幂解法: 这是一个比较裸的有点复杂需要优化的矩乘快速幂,所以推荐大家先做一下下列洛谷题目练练手: (会了,差不多就是多倍经验题 ...

  10. 「BZOJ2510」弱题(矩阵乘法,降维)

    有M个球,一开始每个球均有一个初始标号,标号范围为1-N且为整数,标号为i的球有ai个,并保证Σai = M. 每次操作等概率取出一个球(即取出每个球的概率均为1/M),若这个球标号为k(k < ...

随机推荐

  1. IBM InfoSphere DataStage and QualityStage

    Info coms from https://www.ibm.com/support/knowledgecenter/en/SSZJPZ_9.1.0/com.ibm.swg.im.iis.ds.nav ...

  2. rabbitmq之配置文件详解(二)

    前言 前面介绍了erlang环境的安装和rabbitmq环境安装,接下来对rabbitmq详细配置: 设置配置文件 rabbitmq的系统配置文件一般是rabbitmq.conf,可以登录后台查看它的 ...

  3. C++之初始化问题

    首先,我们应该明确的是在C++中初始化不是赋值,因为初始化是必要的,如果读取了未初始化的值将会导致不明确的行为.初始化指创建变量并且给它赋初值,而赋值则是擦除对象的当前值并用新值代替.C++支持两种初 ...

  4. pillow模块的学习

    https://github.com/wangbinyq/pillow_example http://pillow.readthedocs.org/en/latest/handbook/tutoria ...

  5. linux-open-source-development-tools【重点】

    https://www.pluralsight.com/blog/software-development/linux-open-source-development-tools https://ww ...

  6. yum安装的Apache的各种配置文件的位置

    //配置文件 /etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.d/README /etc/httpd/conf.d/proxy_ajp.conf / ...

  7. Java Redis 连接池 Jedis 工具类

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import re ...

  8. mac date命令

    usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd ...

  9. Letter Combinations of a Phone Number——简单的回溯算法

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  10. CentOS 7下OpenLDAP编译安装及配置

    一.环境 Server:基于CentOS-7-x86_64-1511 Server IP: 172.18.12.203 二.软件获取 OpenLDAP OpenLDAP官网下载地址:http://ww ...