One-Dimensional

One-Dimensional

Consider a one-dimensional cellular automaton containing the N cells. Cells from 0 to N-1 numbers.
A per cell is expressed as a non-negative integer smaller than M state. State of the cells sudden change will occur at each integer times.
We define S (i, t) denotes the i th state at time t cells. At time t + state 1 is expressed as S (i, t + 1) = (A × S (i-1, t) + B × S (i, t) + C × S (i + 1, t) ) mod M,
where a, B, C are given non-negative integer. For i <0 or N≤i, we define S (i, t) = 0 .
Given a defined automaton and the cell at the time of an initial state 0, the state of each task is your cell calculation time T.

Input

Test input comprising a plurality of sets of data. The first line of each data contains six integers N, M, A, B, C, T.
The second line contained in a small non-negative integer N M sequentially per cell at time 0 state. Enter zero as the end of six.

Output

For each test, to output a small non-negative integer N of M, between each two adjacent numbers separated by a space, the state of each cell represents a time T.

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

Ideas:

a simple matrix fast power

\[ \left[ \begin{matrix} B&C&0&\cdots&0\\ A&B&C&\cdots&0\\ 0&A&B&\cdots&0\\ \vdots&\vdots&\vdots&\ddots&0\\ 0&0&0&\cdots&B\\ \end{matrix}\right]^T \left[ \begin{matrix} x_1\\x_2\\x_3\\\vdots\\x_n \end{matrix}\right] \]

\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)

#include<map>
#include<cstdio>
#include<queue>
#include<bitset>
#include<vector>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
# define Type template<typename T>
# define read read1<int>()
Type inline T read1()
{
    T t=0;
    char k;
    bool fl=0;
    do k=getchar(),(k=='-')&&(fl=1);while('0'>k||k>'9');
    while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
    return fl?-t:t;
}
struct re{Type re operator >> (T &a){a=read1<T>();return *this;}}re;
# define fre(k) freopen(k".in","r",stdin);freopen(k".ans","w",stdout)
# define ll int
class Mat
{
    # define Vec vector<ll>
    # define Arr vector<Vec>
    Arr a;
    public:
        Mat(){}
        Mat(Arr k):a(k){}
        Mat(ll x,ll y){a.resize(x);for(ll i=0;i<x;++i)a[i].resize(y);for(int i=0;i<x;++i)for(int j=0;j<y;++j)a[i][j]=0;}
        Vec& operator [](const ll k){return a[k];}
        ll wide(){return a.size();}
        ll len(){return a.empty()?0:a[0].size();}
        Mat operator *(Mat k)
        {
            Arr tem;
            tem.resize(wide());
            for(ll i=0;i<wide();++i)
            {
                tem[i].resize(k.len());
                for(ll j=0;j<len();++j)
                    for(ll l=0;l<k.len();++l)
                        tem[i][l]+=a[i][j]*k[j][l];
            }
            return Mat(tem);
        }
        Mat operator +(Mat k)
        {
            ll o=max(wide(),k.wide()),p=max(len(),k.len());
            Mat tem(o,p);
            for(ll i=0;i<o;++i)
                for(ll j=0;j<p;++j)
                {
                    if(i<wide()&&j<len())tem[i][j]=a[i][j];
                    if(i<k.wide()&&j<k.len())tem[i][j]+=k[i][j];
                }
            return tem;
        }
        Mat operator %(ll k)
        {
            Mat tem(wide(),len());
            for(ll i=0;i<wide();++i)
                for(ll j=0;j<len();++j)
                    tem[i][j]=a[i][j]%k;
            return tem;
        }
        Mat& operator %=(ll k){return *this=*this%k;}
        Mat& operator *=(Mat k){return *this=*this*k;}
        Mat& operator +=(Mat k){return *this=*this+k;}
        bool scan(ll x,ll y,const ll value)
        {
            if(x>=wide()||y>=len()||x<0||y<0)return 0;
            a[x][y]=value;
            return 1;
        }
    # undef Vec
    # undef Arr
};
Type T quickpow(T k,const ll n,ll Mod)
{
    if(n==1)return k;
    T tem=quickpow(k,n>>1,Mod);
    if(Mod!=0)
    {
        tem=(tem*tem)%Mod;
        if(n&1)tem=(tem*k)%Mod;
    }
    else
    {
        tem*=tem;
        if(n&1)tem*=k;
    }
    return tem;
}
int main(){
    for(int n,m,A,B,C,T;n=read,m=read,A=read,B=read,C=read,T=read,n|m|A|B|C|T;){
        Mat x(n,n),y(n,1);A%=m;B%=m;C%=m;
        for(int i=0;i<n;++i)
            y[i][0]=read;
        for(int i=0;i<n;++i){
            x[i][i]=B;
            if(i)x[i][i-1]=A;
            if(i+1<n)x[i][i+1]=C;
        }
        if(T)y=quickpow(x,T,m)*y%m;
        for(int i=0;i<n;++i)
            printf("%d ",y[i][0]);
        putchar('\n');
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/SYDevil/p/12401004.html