UVaLive 3704 Cellular Automaton (循环矩阵 + 矩阵快速幂)
题意:一个细胞自动机包含 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 (循环矩阵 + 矩阵快速幂)的更多相关文章
- Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂)
Luogu 1349 广义斐波那契数列(递推,矩阵,快速幂) Description 广义的斐波那契数列是指形如\[A_n=p*a_{n-1}+q*a_{n-2}\]的数列.今给定数列的两系数p和q, ...
- 洛谷 P4910 帕秋莉的手环 矩阵乘法+快速幂详解
矩阵快速幂解法: 这是一个类似斐波那契数列的矩乘快速幂,所以推荐大家先做一下下列题目:(会了,差不多就是多倍经验题了) 注:如果你不会矩阵乘法,可以了解一下P3390的题解 P1939 [模板]矩阵加 ...
- Qbxt 模拟赛 Day4 T2 gcd(矩阵乘法快速幂)
/* 矩阵乘法+快速幂. 一开始迷之题意.. 这个gcd有个规律. a b b c=a*x+b(x为常数). 然后要使b+c最小的话. 那x就等于1咯. 那么问题转化为求 a b b a+b 就是斐波 ...
- UVA 1386 - Cellular Automaton(循环矩阵)
UVA 1386 - Cellular Automaton option=com_onlinejudge&Itemid=8&page=show_problem&category ...
- POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)
A cellular automaton is a collection of cells on a grid of specified shape that evolves through a nu ...
- LA 3704 Cellular Automaton
题意概述: 等价地,本题可以转化为下面的问题: 考虑$n \times n$的$0-1$矩阵$A$,在第$i$行上第$[-d+i, d+i]$(模$n$意义下)列对应的元素为$1$,其余为$0$.求$ ...
- UVa 3704 Cellular Automaton(矩乘)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15129 [思路] 矩阵乘法-循环矩阵 题目中的转移矩阵是一个循环矩 ...
- 【BZOJ-1009】GT考试 KMP+DP+矩阵乘法+快速幂
1009: [HNOI2008]GT考试 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2745 Solved: 1694[Submit][Statu ...
- 矩阵乘法快速幂 codevs 1574 广义斐波那契数列
codevs 1574 广义斐波那契数列 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 广义的斐波那契数列是指形如 ...
随机推荐
- Union and Intersection of two sorted lists 并集和交集
跟面试官确认是arrayList还是singly-linked list /* Union 并集:两个升序的list a, b, 返回其并集(升序排序)*/ public class UnionTw ...
- Httpclient 表单,json,multipart/form-data 提交 ---总结常用的方法
最近在项目中,一直在使用HttpClient 中的方法,这里我进行一些方法的汇总,也是结合了一些大牛写的代码,以备不时之需 官话:HttpClient 是Apache Jakarta Common 下 ...
- spring BeanUtils 工具实现对象之间的copy
一般我们会开发中会遇到返回用户信息的时候,不需要返回密码或者其他参数,这时候我们需要重新定义一个VO类去除不需要的参数,将原对象copy到VO类中 使用spring的BeanUtils可以实现对象的c ...
- 编程,计算data段中的第一组数据的3次方,结果保存在后面一组dword单元中
assume cs:code data segment dw ,,,,,,, dd ,,,,,,, data ends code segment start: mov ax,data mov ds,a ...
- 运行SVO
安装与运行的所有文档:https://github.com/uzh-rpg/rpg_svo/wiki 有两种安装方式: 有ros:https://github.com/uzh-rpg/rpg_svo/ ...
- MVC报错:找到多个与名为“Home”的控制器匹配的类型。
错误原因是:在根目录中的Controller中有HomeController,而在Areas中也有一个HomeController,只是他们的命名空间不一样. 这样的话,只需要在对应的路由注册中加入命 ...
- vue通过代理实现跨域
http://www.cnblogs.com/wangyongcun/p/7665687.html
- Jmeter常用脚本开发之Debug Sampler
Debug Sampler编辑脚本时调试用的,跟Java项目打断点测试同理,它可以Debug Jmeter中所有自定义变量的值 如何添加Debug Sampler? 打开测试计划—>线程组—&g ...
- PAT 1022 D进制的A+B (20)(20 分)
输入两个非负10进制整数A和B(<=2^30^-1),输出A+B的D (1 < D <= 10)进制数. 输入格式: 输入在一行中依次给出3个整数A.B和D. 输出格式: 输出A+B ...
- linux安全分析
history 查看历史命令 last | grep -i norco //最后一次登录时间