2012年亚洲区域赛东京站

C. One-Dimensional Cellular Automaton

Time Limit: 3000ms
Memory Limit: 131072KB
64-bit integer IO format:  %lld      Java class name:  Main

[PDF Link]

There is a one-dimensional cellular automaton consisting of N cells. Cells are numbered from 0 to N - 1.

Each cell has a state represented as a non-negative integer less than M. The states of cells evolve through discrete time steps. We denote the state of the i-th cell at time t as S(it). The state at time t + 1 is defined by the equation

 

S(it + 1) = $\displaystyle \left(\vphantom{A \times S(i - 1, t) + B \times S(i, t) + C \times S(i + 1, t) }\right.$A x S(i - 1, t) + B x S(it) + C x S(i + 1, t)$\displaystyle \left.\vphantom{A \times S(i - 1, t) + B \times S(i, t) + C \times S(i + 1, t) }\right)$ mod M,(1)

where AB and C are non-negative integer constants. For i < 0 or N$ \le$i, we define S(it) = 0.

Given an automaton definition and initial states of cells, your mission is to write a program that computes the states of the cells at a specified time T.

 

Input

The input is a sequence of datasets. Each dataset is formatted as follows.

 


N M A B C T
S(0, 0) S(1, 0) ... S(N - 1, 0)

 


The first line of a dataset consists of six integers, namely NMABC and TN is the number of cells. M is the modulus in the equation (1). AB and C are coefficients in the equation (1). Finally, T is the time for which you should compute the states.

You may assume that 0 < N$ \le$500 < M$ \le$10000$ \le$ABC < M and 0$ \le$T$ \le$109.

The second line consists of N integers, each of which is non-negative and less than M. They represent the states of the cells at time zero.

A line containing six zeros indicates the end of the input.

 

Output

For each dataset, output a line that contains the states of the cells at time T. The format of the output is as follows.

 


S(0, TS(1, T) ... S(N - 1, T)

 


Each state must be represented as an integer and the integers must be separated by a space.

 

Sample Input

5 4 1 3 2 0
0 1 2 0 1
5 7 1 3 2 1
0 1 2 0 1
5 13 1 3 2 11
0 1 2 0 1
5 5 2 0 1 100
0 1 2 0 1
6 6 0 2 3 1000
0 1 2 0 1 4
20 1000 0 2 3 1000000000
0 1 2 0 1 0 1 2 0 1 0 1 2 0 1 0 1 2 0 1
30 2 1 0 1 1000000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
30 2 1 1 1 1000000000
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
30 5 2 3 1 1000000000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0

Sample Output

0 1 2 0 1
2 0 0 4 3
2 12 10 9 11
3 0 4 2 1
0 4 2 0 4 4 
0 376 752 0 376 0 376 752 0 376 0 376 752 0 376 0 376 752 0 376
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 3 2 2 2 3 3 1 4 3 1 2 3 0 4 3 3 0 4 2 2 2 2 1 1 2 1 3 0

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
const int maxn=55;
int map[maxn][maxn][maxn];
int num[maxn];//存储N个数据
int flag_1[maxn][maxn];
int flag_2[maxn];
int N;//含义如题
int M;
int A;
int B;
int C;
int T;
int main()
{
    while(~scanf("%d%d%d%d%d%d",&N,&M,&A,&B,&C,&T))
    {
        if(N==0&&M==0&&A==0&&B==0&&C==0&&T==0)
            break;
        for(int i=1; i<=N; i++) //接收数据
            scanf("%d",&num[i]);
        if(T==0)//特判
        {
            printf("%d",num[1]);
            for(int i=2; i<=N; i++)
                printf(" %d",num[i]);
            printf("\n");
            continue;
        }
        for(int i=1; i<=N; i++)
        {
            map[0][i-1][i]=A;//存储系数
            map[0][i][i]=B;
            map[0][i+1][i]=C;
        }
        int len=0;//长度
        while(T>1)
        {
            flag_2[++len]=T%2;
            T=T/2;
        }
        for(int i=1; i<=len/2; i++)
        {
            int temp=flag_2[i];
            flag_2[i]=flag_2[len-i+1];
            flag_2[len-i+1]=temp;
        }
        for(int l=1; l<=len; l++)
        {
            for(int i=1; i<=N; i++)
                for(int j=1; j<=N; j++)
                    map[l][i][j]=0;
            for(int k=1; k<=N; k++)
                for(int i=1; i<=N; i++)
                    for(int j=1; j<=N; j++)
                        map[l][i][j]=(map[l][i][j]+map[l-1][i][k]*map[l-1][k][j])%M;
            if(flag_2[l]==1)
            {
                for(int i=1; i<=N; i++)
                    for(int j=1; j<=N; j++)
                    {
                        flag_1[i][j]=map[l][i][j];
                        map[l][i][j]=0;
                    }
                for(int k=1; k<=N; k++)
                    for(int i=1; i<=N; i++)
                        for(int j=1; j<=N; j++)
                            map[l][i][j]=(map[l][i][j]+flag_1[i][k]*map[0][k][j])%M;
            }
        }
        memset(flag_1,0,sizeof(flag_1));
        for(int k=1; k<=N; k++)
            for(int i=1; i<=N; i++)
                for(int j=1; j<=N; j++)
                    flag_1[i][j]=(flag_1[i][j]+num[k]*map[len][k][j])%M;
        printf("%d",flag_1[1][1]);
        for(int i=2; i<=N; i++)
            printf(" %d",flag_1[1][i]);
        printf("\n");
    }
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
int mod;
struct lt
{
    int m[55][55];
    int l;
    lt operator *(lt b)
    {
        lt c(l);
        for(int i=0; i<l; ++i)
            for(int j=0; j<l; ++j)
            {
                if(m[j][i])
                    for(int k=0; k<l; ++k)
                    {
                        c.m[j][k]+=m[j][i]*b.m[i][k]%mod;
                        c.m[j][k]%=mod;
                    }
            }
        return c;
    }
    lt(int len)
    {
        l=len;
        memset(m,0,sizeof(m));
    }
};
lt ksm(lt a,int b)
{
    lt c(a.l);
    for(int i=0; i<c.l; ++i)
        c.m[i][i]=1;
    while(b)
    {
        if(b&1)
            c=c*a;
        a=a*a;
        b>>=1;
    }
    return c;
}
int main()
{
    int n,a,b,c,t;
    while(~scanf("%d%d%d%d%d%d",&n,&mod,&a,&b,&c,&t))
    {
        if(!n&&!mod&&!a&&!b&&!c&&!t)
            break;
        lt m0(n),temp(n);
        for(int i=0; i<n; ++i)
            scanf("%d",&m0.m[0][i]);
        for(int i=0; i<n; ++i)
        {
            if(i)
                temp.m[i-1][i]=a;
            temp.m[i][i]=b;
            if(i+1<n)
                temp.m[i+1][i]=c;
        }
        temp=ksm(temp,t);
        lt mt(n);
        mt=m0*temp;
        for(int i=0; i<n; ++i)
            if(i)
                printf(" %d",mt.m[0][i]);
            else
                printf("%d",mt.m[0][i]);
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值