[POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 3048 | Accepted: 1227 | |
Case Time Limit: 2000MS |
Description
A cellular automaton is a collection of cells on a grid of specified shape that evolves through a number of discrete time steps according to a set of rules that describe the new state of a cell based on the states of neighboring cells. Theorder
of the cellular automaton is the number of cells it contains. Cells of the automaton of order n are numbered from 1 to n.
The order of the cell is the number of different values it may contain. Usually, values of a cell of order m are considered to be integer numbers from 0 to m − 1.
One of the most fundamental properties of a cellular automaton is the type of grid on which it is computed. In this problem we examine the special kind of cellular automaton — circular cellular automaton of order n with cells of orderm.
We will denote such kind of cellular automaton as n,m-automaton.
A distance between cells i and j in n,m-automaton is defined as min(|i − j|, n − |i − j|). A d-environment of a cell is the set of cells at a distance not greater than d.
On each d-step values of all cells are simultaneously replaced by new values. The new value of cell i after d-step is computed as a sum of values of cells belonging to the d-enviroment of the cell i modulo m.
The following picture shows 1-step of the 5,3-automaton.
The problem is to calculate the state of the n,m-automaton after k d-steps.
Input
The first line of the input file contains four integer numbers n, m, d, and k (1 ≤ n ≤ 500, 1 ≤ m ≤ 1 000 000, 0 ≤ d < n⁄2 , 1 ≤ k ≤ 10 000 000). The second
line contains n integer numbers from 0 to m − 1 — initial values of the automaton’s cells.
Output
Output the values of the n,m-automaton’s cells after k d-steps.
Sample Input
sample input #1
5 3 1 1
1 2 2 1 2 sample input #2
5 3 1 10
1 2 2 1 2
Sample Output
sample output #1
2 2 2 2 1 sample output #2
2 0 0 2 2
Source
b =
1 1 0 0 1
1 1 1 0 0
0 1 1 1 0
0 0 1 1 1
1 0 0 1 1
[1, 1, 0, 0, 1]
[1, 1, 1, 0, 0]
[0, 1, 1, 1, 0]
[0, 0, 1, 1, 1]
[1, 0, 0, 1, 1]
b^2 =
[3, 2, 1, 1, 2]
[2, 3, 2, 1, 1]
[1, 2, 3, 2, 1]
[1, 1, 2, 3, 2]
[2, 1, 1, 2, 3]
b^3 =
[7, 6, 4, 4, 6]
[6, 7, 6, 4, 4]
[4, 6, 7, 6, 4]
[4, 4, 6, 7, 6]
[6, 4, 4, 6, 7]
b^4 =
[19, 17, 14, 14, 17]
[17, 19, 17, 14, 14]
[14, 17, 19, 17, 14]
[14, 14, 17, 19, 17]
[17, 14, 14, 17, 19]
/*
ID: wuqi9395@126.com
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
#define INF (1<<30)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define For(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
const int maxn = 505;
const int maxm = 505;
int mod, n, k, d;
void Matrix_pow(int a[], int b[]) {
ll c[505] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) c[i] += ((ll)a[j] * b[(i + j) % n]) % mod;
c[i] %= mod;
}
for (int i = 0; i < n; i++) b[i] = c[i];
}
int a[505], b[505];
int main () {
while(scanf("%d%d%d%d", &n, &mod, &d, &k) != EOF) {
for (int i = 0; i < n; i++) scanf("%d", b + i);
memset(a, 0, sizeof(a));
a[0] = 1;
for (int i = 1; i <= d; i++) a[i] = a[n - i] = 1;
while(k) {
if (k & 1) Matrix_pow(a, b);
k >>= 1;
Matrix_pow(a, a);
}
for (int i = 0; i < n; i++) printf("%d%c", b[i], " \n"[i == n - 1]);
}
return 0;
}
參考:http://www.cppblog.com/varg-vikernes/archive/2011/02/08/139804.html
[POJ 3150] Cellular Automaton (矩阵高速幂 + 矩阵乘法优化)的更多相关文章
- POJ 3150 Cellular Automaton(矩阵高速幂)
题目大意:给定n(1<=n<=500)个数字和一个数字m,这n个数字组成一个环(a0,a1.....an-1).假设对ai进行一次d-step操作,那么ai的值变为与ai的距离小于d的全部 ...
- 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 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化
题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮, ...
- POJ 3150 Cellular Automaton --矩阵快速幂及优化
题意:给一个环,环上有n块,每块有个值,每一次操作是对每个点,他的值变为原来与他距离不超过d的位置的和,问k(10^7)次操作后每块的值. 解法:一看就要化为矩阵来做,矩阵很好建立,大白书P157页有 ...
- POJ 3150 Cellular Automaton(矩阵乘法+二分)
题目链接 题意 : 给出n个数形成环形,一次转化就是将每一个数前后的d个数字的和对m取余,然后作为这个数,问进行k次转化后,数组变成什么. 思路 :下述来自here 首先来看一下Sample里的第一组 ...
- poj 3150 Cellular Automaton
首先来看一下Sample里的第一组数据.1 2 2 1 2经过一次变换之后就成了5 5 5 5 4它的原理就是a0 a1 a2 a3 a4->(a4+a0+a1) (a0+a1+a2) (a1+ ...
- poj 2778 AC自己主动机 + 矩阵高速幂
// poj 2778 AC自己主动机 + 矩阵高速幂 // // 题目链接: // // http://poj.org/problem?id=2778 // // 解题思路: // // 建立AC自 ...
- poj 3233(矩阵高速幂)
题目链接:http://poj.org/problem?id=3233. 题意:给出一个公式求这个式子模m的解: 分析:本题就是给的矩阵,所以非常显然是矩阵高速幂,但有一点.本题k的值非常大.所以要用 ...
随机推荐
- 5款帮助简化的HTML5 Audio开发的Javascript类库
HTML5的audio标签提供了我们方便控制声音的功能,可是使用原生的HTML5来开发声音或者音乐相关的项目仍旧很的麻烦.在今天这篇文章中,我们将介绍5款帮助你简化开发的javascript audi ...
- OWAP Top 10
2013 Top 10 List A1-Injection Injection flaws, such as SQL, OS, and LDAP injection occur when untr ...
- redis 获取key 过期时间
<pre name="code" class="html">127.0.0.1:6379> keys *b4f107c6-e96c-4a1e- ...
- DataSnap起源于MIDAS(十几篇博客)
作为MIDAS起始于Delphi3,Delphi4是MIDAS II,Delphi5中是MIDASIII,而后基于COM远程数据模块方式使用TCP/IP,HTTP,(D)COM构建出强大的通讯能力.从 ...
- [Android学习笔记]ShareSDK的使用
ShareSDK使用方便,集成简单,正式客户端开发人员的首选组件 集成步骤,使用说明见官方文档: http://wiki.sharesdk.cn/Android_快速集成指南 记录: 直接使用官方De ...
- 【OpenCV新手教程第14】OpenCVHough变换:霍夫变换线,霍夫变换圆汇编
本系列文章由@浅墨_毛星云 出品.转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/26977557 作者:毛星云(浅墨) ...
- NavigationBar 隐藏底部边线,阴影
NavigationBar 底部默认有一条边线 假设项目中须要隐藏何以採用这个库 https://github.com/samwize/UINavigationBar-Addition/
- VSTO 得到Office文档的选中内容(Word、Excel、PPT、Outlook)
原文:VSTO 得到Office文档的选中内容(Word.Excel.PPT.Outlook) 目的:得到在Word.Excel.PPT.Outlook中选中的一段内容. Word: private ...
- File already exists: filesystem '/path/file', transaction svn常见错误解决方法
前言 多人任务基本都会用到SVN,于是提交的时候如果不先更新在提交或者操作顺序不对,会经常出现错误,其中File already exists: filesystem这个就是个常见问题,上网找了半天没 ...
- BroadcastReceiver总结
一.工程整体图 二.activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ ...