天天看點

2012年亞洲區域賽東京站

C. One-Dimensional Cellular Automaton

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

[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(i, t). The state at time t + 1 is defined by the equation

S(i, t + 1) = 
2012年亞洲區域賽東京站
A x S(i - 1, t) + B x S(i, t) + C x S(i + 1, t)
2012年亞洲區域賽東京站
 mod M,
(1)

where A, B and C are non-negative integer constants. For i < 0 or N

2012年亞洲區域賽東京站

i, we define S(i, t) = 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 N, M, A, B, C and T. N is the number of cells. M is the modulus in the equation (1). A, B 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

2012年亞洲區域賽東京站

50, 0 < M

2012年亞洲區域賽東京站

1000, 0

2012年亞洲區域賽東京站

A, B, C < M and 0

2012年亞洲區域賽東京站

T

2012年亞洲區域賽東京站

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, T) S(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;
}