Http://poj.org/problem? Id = 3150
The bare matrix of this question is easy to see. Suppose d = 1, n = 5, then the matrix is like this.
1 1 0 0 1
1 1 1 0 0
0 1 1 1 0
0 0 1 1 1
1 0 0 1 1
This is n ^ 3, but n <= 500, apparently TLE
We observe the n × n matrix and find that no row is obtained from the top row to the right.
According to CIJ = AIK × bkj, we can find that bkj = akj = AI (J-K)
Then we can reduce the value of two dimensions to one dimension. Each time, we only need to calculate the first line, that is, cj = ak * BJ-K.
#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << #x << " = " << x << endl#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }inline const long long getint() { long long r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }typedef long long matrix[505];void mul(matrix a, matrix b, matrix c, int lb, int lc, long long md) {matrix t;rep(j, lc) {t[j]=0;rep(k, lb)if(j-k>=0) t[j]=(t[j]+a[k]*b[j-k])%md;else t[j]=(t[j]+a[k]*b[lb+j-k])%md;}rep(j, lc) c[j]=t[j];}matrix a, b, c;int main() {long long n, m, d, k;cin >> n >> m >> d >> k;rep(i, n) read(c[i]);a[0]=b[0]=1;rep(j, d+1) a[j]=1;for2(j, n-d, n) a[j]=1;while(k) {if(k&1) mul(a, b, b, n, n, m);mul(a, a, a, n, n, m);k>>=1;}mul(c, b, c, n, n, m);rep(i, n) printf("%lld ", c[i]);return 0;}
Description
ACellular AutomatonIs 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. theOrder of the cellular automatonIs the number of cells it contains. Cells of the automaton of orderNAre numbered from 1N.
TheOrder of the cellIs the number of different values it may contain. Usually, values of a cell of orderMAre considered to be integer numbers from 0M−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 orderNWith cells of orderM. We will denote such kind of cellular automatonN, m-Automaton.
A distance between cellsIAndJInN,M-Automaton is defined as Min (|I−J|,N− |I−J|).D-environment of a cellIs the set of cells at a distance not greaterD.
On eachD-stepValues of all cells are simultaneously replaced by new values. The new value of CellIAfterD-Step is computed as a sum of values of cells belonging toD-Enviroment of the cellIModuloM.
The following picture shows 1-step of the 5, 3-automaton.
The problem is to calculate the state ofN,M-Automaton afterK D-Steps.
Input
The first line of the input file contains four integer numbersN,M,D, AndK(1 ≤N≤ 500, 1 ≤M≤ 1 000 000, 0 ≤D<NLimit 2, 1 ≤K≤ 10 000 000). The second line contains N integer numbers from 0M−1-initial values of the automaton's cells.
Output
Output the values ofN,M-Automaton's cells afterK D-Steps.
Sample Input
sample input #15 3 1 11 2 2 1 2sample input #25 3 1 101 2 2 1 2
Sample output
sample output #12 2 2 2 1sample output #22 0 0 2 2
Source
Northeastern Europe 2006.
[Poj] 3150 cellular automaton (matrix multiplication + special techniques)