POJ 3150 Cellular Automaton

Cellular Automaton
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 3553 Accepted: 1434
Case Time Limit: 2000MS

Description

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. The order 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 order m. 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 nmd, and k (1 ≤ n ≤ 500, 1 ≤ m ≤ 1 000 000, 0 ≤ d < n2 , 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

将初始值看做一个1*n的矩阵s,分析题意,容易得到ans=s* A^k;

(A是根据题意构造的矩阵)

时间复杂度O(N^3),显然爆炸。

仔细观察发现A是一个循环矩(方)阵。

循环方阵A满足:A[x][y]=A[1][(y-x+n)%n+1]; 

我们只计算和存储A的第一行,时空复杂度下降到O(n^2)可以通过。

代码如下:

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#define LL long long
#define CLEAR(xxx) memset(xxx,0,sizeof(xxx))
using namespace std;
const LL maxn=500+5,inf=1e9;
LL n,m,d,k,s[maxn];
LL A[maxn],C[maxn],ans[maxn],z[maxn];
void multi(LL *A,LL *B){
	CLEAR(C);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			(C[i]+=A[j]*B[(i-j+n)%n+1])%=m;
	memcpy(A,C,sizeof(C));
} 
void Pow(int x){
	int i,k,j;
	CLEAR(ans); ans[1]=1;
	for(;x;x>>=1,multi(A,A))
		if(x&1)multi(ans,A);
}
void solve(){
	int i,j;
	CLEAR(A);CLEAR(s);
	for(i=1;i<=n;i++)scanf("%d",&s[i]);
	for(i=1;i<=d+1;i++)A[i]=1;
	for(i=n-d+1;i<=n;i++)A[i]=1;
	Pow(k);
	multi(s,ans);
	for(i=1;i<=n;i++)printf("%d ",C[i]);
	putchar('\n'); 
}
int main(){
	while(cin>>n>>m>>d>>k)
		solve();
	return 0;
}
<pre name="code" class="cpp">#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define mod m
#define clear(xxx) memset(xxx,0,sizeof(xxx))
using namespace std;
const long long maxn=505;
typedef long long arr[maxn];
arr a,b,ans,z;
long long n,m,d,k;
void multiply(arr x,arr y){ 
    memset(z,0,sizeof(z)); 
    for(long long i=1;i<=n;i++){ 
        for(long long h=1;h<=n;h++){ 
            z[i]=(z[i]+x[h]*y[(i-h+n)%n+1])%mod; 
        } 
    } 
    memcpy(x,z,sizeof(z)); 
} 
void mont(long long t){ 
    memset(ans,0,sizeof(ans)); 
    ans[1]=1;
    while(t>0){ 
        if(t&1)multiply(ans,b); 
        t>>=1; 
        multiply(b,b); 
    } 
} 
int main(){
	while(scanf("%I64d%I64d%I64d%I64d",&n,&m,&d,&k)!=EOF){
		clear(a);
		clear(b);
		clear(ans);
		clear(z);
		long long i,j;
		for(i=1;i<=n;i++){
			scanf("%I64d",&a[i]);
		}
		for(i=1;i<=d+1;i++)b[i]=1;
		for(i=n;i>=n-d+1;i--)b[i]=1;
		mont(k);
		multiply(a,ans);
		for(i=1;i<=n;i++){
			printf("%I64d ",z[i]);
		}
		printf("\n");
	}
}


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值