Poj 3150 Cellular Automaton

Source: Internet
Author: User

Idea: rapid matrix Power Analysis: 1 The number of n given by the question is 0 ~ The distance between two numbers is min (| I-j |, n-| I-j | ). Given d and k, each number is converted into a new number after each transformation. This new number is equal to the sum of % m2, which is less than or equal to all numbers of d. This is similar to the previous two questions, hdu2276 and FZU1692, all of them belong to the problem of cyclic homogeneity. Let's first look at what each number becomes after a transformation. Because the distance is less than or equal to d, the first | I-j | = d, then j = I + d, the second case n-| I-j | = d, therefore, j = n-d + I. The first number equals to = num [1] + num [2] + .... + num [d + 1] + num [n-d + 1] +... + num [n] The second number equals to = num [2] + .... + num [d + 2] + num [n-d + 2] +... + num [n]... ........................................ ................................... 3 because the matrix here is cyclical and homogeneous, we only need to find the first line, and the rest can be introduced according to the previous line. In this way, the complexity of matrix multiplication is reduced to O (n ^ 2) code:

 /************************************************  * By: chenguolin                               *   * Date: 2013-08-31                             *  * Address: http://blog.csdn.net/chenguolinblog *  ************************************************/  #include<cstdio>  #include<cstring>  #include<iostream>  #include<algorithm>  using namespace std;    typedef long long int64;  const int N = 505;     int n , MOD , d , k;  int arr[N];    struct Matrix{      int64 mat[N][N];      Matrix operator*(const Matrix &m)const{          Matrix tmp;          for(int i = 1 ; i <= n ; i++){              tmp.mat[1][i] = 0;              for(int j = 1 ; j <= n ; j++)                  tmp.mat[1][i] += mat[1][j]*m.mat[j][i]%MOD;              tmp.mat[1][i] %= MOD;          }          for(int i = 2 ; i <= n ; i++){              tmp.mat[i][1] = tmp.mat[i-1][n];              for(int j = 2 ; j <= n ; j++)                  tmp.mat[i][j] = tmp.mat[i-1][(j-1+n)%n];          }          return tmp;       }  };    void init(Matrix &m){      memset(m.mat , 0 , sizeof(m.mat));      for(int i = 1 ; i <= d+1 ; i++)          m.mat[1][i] = 1;      for(int i = n-d+1 ; i <= n ; i++)          m.mat[1][i] = 1;      for(int i = 2 ; i <= n ; i++){          m.mat[i][1] = m.mat[i-1][n];          for(int j = 2 ; j <= n ; j++)              m.mat[i][j] = m.mat[i-1][(j-1+n)%n];      }  }    void Pow(Matrix &m){      Matrix ans;      memset(ans.mat , 0 , sizeof(ans.mat));      for(int i = 1 ; i <= n ; i++)          ans.mat[i][i] = 1;      while(k){          if(k&1)              ans = ans*m;          k >>= 1;          m = m*m;      }      for(int i = 1 ; i <= n ; i++){          int64 sum = 0;          for(int j = 1 ; j <= n ; j++)              sum += ans.mat[i][j]*arr[j]%MOD;          if(i > 1) printf(" ");          printf("%lld" , sum%MOD);      }      puts("");  }    int main(){      Matrix m;      while(scanf("%d" , &n) != EOF){          scanf("%d%d%d" , &MOD , &d , &k);          for(int i = 1 ; i <= n ; i++)              scanf("%d" , &arr[i]);          init(m);          Pow(m);      }          return 0;  }  

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.