题意:一个细胞自动机包含 n 个格子,每个格子取值是 0 ~ m-1,给定距离,则每次操作后每个格子的值将变成到它距离不超过 d 的所有格子在操作之前的值之和取模 m 后的值,其中 i 和 j 的距离为 min{|i-1|,  n-|i-j|}。给定 n,m,d,k 和自动机每个格子的初始值,求 k 次操作后的各个格子的值。

析:由于能够直接能推出公式,而且 k 比较大,很容易想到是矩阵快速幂,并且也能够写出矩阵方程。假设 d = 1

很容易得到这个矩阵,然后使用矩阵快速幂,但是复杂度是 O(n^3*logk),而且还有多组数据,会TLE的,然后考虑优化,从这个矩阵可以看出这是一个循环矩阵,也就是第 i 列可以由第 i-1 列通过向下移动一个得到,而且还有结论,那就是两个循环矩阵相乘得到的矩阵依然是循环矩阵,既然的话,我们就可以只保留第一列就可以了,因为其他列都可以由于第一列得到,由于只要算一次,那么在矩阵相乘的时候,时间复杂度就不是O(n^3) 了,而是O(n^2),然后再加上快速幂,总时间复杂度就是O(n^2*logk),可以解决这个问题。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 500 + 5;
const int maxm = 1e6 + 2;
const LL mod = 1000000007;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Matrix{
int a[maxn], n;
void init(){ ms(a, 0); }
void toOne(){ a[0] = 1; } Matrix operator * (const Matrix &rhs){
Matrix res; res.n = n; res.init();
FOR(i, n, 0) FOR(j, n, 0) res.a[i] = (res.a[i] + (LL)a[(i-j+n)%n] * rhs.a[j]) % m;
return res;
}
}; Matrix fast_pow(Matrix x, int n){
Matrix res; res.n = x.n; res.init(); res.toOne();
while(n){
if(n&1) res = res * x;
x = x * x;
n >>= 1;
}
return res;
} int main(){
int d, k;
while(scanf("%d %d %d %d", &n, &m, &d, &k) == 4){
Matrix x, y; x.init(); y.init();
x.n = y.n = n;
for(int i = 0; i < n; ++i) scanf("%d", &x.a[i]);
y.a[0] = 1;
int cnt = 1;
while(cnt <= d) y.a[cnt] = 1, ++cnt;
cnt = 1;
while(cnt <= d) y.a[n-cnt] = 1, ++cnt;
Matrix ans = x * fast_pow(y, k);
for(int i = 0; i < n; ++i) printf("%d%c", ans.a[i], " \n"[i+1==n]);
}
return 0;
}

  

UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)的更多相关文章

  1. Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)

    Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂) Description 广义的斐波那契数列是指形如\[A_n=p*a_{n-1}+q*a_{n-2}\]的数列.今给定数列的两系数p和q, ...

  2. 洛谷 P4910 帕秋莉的手环 矩阵乘法+快速幂详解

    矩阵快速幂解法: 这是一个类似斐波那契数列的矩乘快速幂,所以推荐大家先做一下下列题目:(会了,差不多就是多倍经验题了) 注:如果你不会矩阵乘法,可以了解一下P3390的题解 P1939 [模板]矩阵加 ...

  3. Qbxt 模拟赛 Day4 T2 gcd(矩阵乘法快速幂)

    /* 矩阵乘法+快速幂. 一开始迷之题意.. 这个gcd有个规律. a b b c=a*x+b(x为常数). 然后要使b+c最小的话. 那x就等于1咯. 那么问题转化为求 a b b a+b 就是斐波 ...

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

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

  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. LA 3704 Cellular Automaton

    题意概述: 等价地,本题可以转化为下面的问题: 考虑$n \times n$的$0-1$矩阵$A$,在第$i$行上第$[-d+i, d+i]$(模$n$意义下)列对应的元素为$1$,其余为$0$.求$ ...

  7. UVa 3704 Cellular Automaton(矩乘)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15129 [思路] 矩阵乘法-循环矩阵 题目中的转移矩阵是一个循环矩 ...

  8. 【BZOJ-1009】GT考试 KMP+DP+矩阵乘法+快速幂

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 2745  Solved: 1694[Submit][Statu ...

  9. 矩阵乘法快速幂 codevs 1574 广义斐波那契数列

    codevs 1574 广义斐波那契数列  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond   题目描述 Description 广义的斐波那契数列是指形如 ...

随机推荐

  1. java 基础之--java动态代理

    1.抽象角色:声明真实对象与代理对象的共同接口: 2.代理角色:相当于中介的作用,bridge,内部包含对真实角色的reference,在执行真实操作对象时,附加其他操作,相当于对真实角色的封装: 3 ...

  2. mysql中如何使用一句话将一个表的数据导入到另一个表中:insert into ...select

    INSERT INTO cdcd2015(`filename`,`xmlfile`,`updatetime`,`isDel`) SELECT `filename`,`xmlfile`,`updatet ...

  3. firefox打开链接自动跳转至新页面设置

    Firefox打开新页面时,活动页面会自动跳转到刚刚打开的页面,用着很不舒服,想打开新页面标签时,页面依然会停留在之前的页面. 在网上找了一下,设置方法如下: 在地址栏里输入about:config, ...

  4. PAT 1001 害死人不偿命的(3n+1)猜想 (15)(C++&JAVA&Python)

    1001 害死人不偿命的(3n+1)猜想 (15)(15 分) 卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反 ...

  5. javascript 高级程序设计 九

    JS 面向对象的程序设计思想(1)深入理解JS对象 1.js的中没有OO语言中的'类'的概念.ECMAjs中把对象定义为:‘无序属性的集合,其属性可以包含基本值,对象或函数’. 2.ECMAScrip ...

  6. BZOJ1217或洛谷2279 [HNOI2003]消防局的设立

    BZOJ原题链接 洛谷原题链接 该题有两种做法,树形\(DP\)和贪心. 先讲贪心. 先将所有点按深度从大到小排序,然后从大到小依次取出点,若已经被覆盖则跳过,否则就在它的祖父点建立消防站. 考虑如何 ...

  7. RavenDb使用

    在Raven中查询数据,查询条件必须在index中. 如果查询条件不在index中就会出现如下异常 var query = session.DynamicIndexQuery<ServicePr ...

  8. Codeforces 757B. Bash's Big Day GCD

    B. Bash's Big Day time limit per test:2 seconds memory limit per test:512 megabytes input:standard i ...

  9. Window7安装tensorflow整套环境详细流程

    安装tensorflow方式有好多种,为了方便编译环境以及包管理,这里采用Anaconda平台安装tensorflow. tensorflow官网:http://www.tensorflow.org/ ...

  10. 用visual studio 2017来调试python

    https://www.visualstudio.com/zh-hans/thank-you-downloading-visual-studio/?sku=Professional&rel=1 ...