题意:一个细胞自动机包含 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. ztree-持续更新中

    版本v3快速入门: 1,官网下载https://gitee.com/zTree/zTree_v3 2,zTree-zTree_v3-master\zTree_v3下复制css和js文件夹到项目下 3, ...

  2. Bootstrap(6)辅组类和响应式工具

    一.辅助类 Bootstrap 在布局方面提供了一些细小的辅组样式,用于文字颜色以及背景色的设置.显示关闭图标等等. 1.情景文本颜色 各种色调的字体 <p class="text-m ...

  3. 31-java中知识总结:list, set, map, stack, queue

    import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Lin ...

  4. 快速排序中BUG int 与 int *

    #include <iostream>using namespace std;int QKPass(int* , int , int);  //若声明为 int QKPass(int, i ...

  5. Beyound Compare中比较java字节码class文件

    背景 项目维护的时候版本混乱或者外出在现场项目排错的时候难免要比对两个jar/class/war文件的源代码. 通常情况下这个时候我们用jd-gui直接把文件拖进去比对,这种情况只适合单一文件的比对. ...

  6. auth 认证模块

    . auth认证模块: http://www.cnblogs.com/liwenzhou/p/9030211.html auth模块的知识点: . 创建超级用户 python manage.py cr ...

  7. linux下的压缩命令

    linux zip命令 zip -r myfile.zip ./* 将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzip unzip - ...

  8. iOS.OpenSource.PopularProject

    1. Core Plot Core Plot is a plotting framework for OS X and iOS. It provides 2D visualization of dat ...

  9. SNP(单核苷酸多态性)准确性的验证,你造吗?

    SNP(单核苷酸多态性)准确性的验证,你造吗? [2016-12-12]       SNP(全称Single Nucleotide Polymorphisms)即单核苷酸多态性,主要是指在基因组水平 ...

  10. mfc获取exe的版本信息

    CString GetFileVersion(const CString& sTargetFileName){ DWORD nInfoSize = 0, dwHandle = 0; nInfo ...