"Poj 3150" Cellular automaton matrix

Source: Internet
Author: User
Tags integer numbers mul

title : http://poj.org/problem?id=3150

Cellular Automaton matrix multiplication + dichotomy

Cellular Automaton

Time limit:12000ms Memory limit:65536k
Total submissions:3544 accepted:1428
Case Time Limit:2000ms

Description

A cellular automaton is a collection of cells in a grid of specified shape that evolves through a number of discrete time Steps according to a set of rules this describe the new state of a cell based on the states of neighboring cells. The order of the cellular automaton is the number of cells it contains. Cells of the automaton of order n is numbered from 1 to N.

The order of the cell is the number of different values it may contain. Usually, values of a cell of order m is considered to being integer numbers from 0 to M? 1.

One of the most fundamental properties of a cellular automaton are the type of grid on which it is computed. In this problem we examine the special kind of cellular automaton-circular cellular automaton of order n with cells of O Rder m. We'll denote such kind of cellular automaton as N,m-automaton.

A distance between cells I and J in N,m-automaton are defined as min (|i? j|, n |i? j|). A d-environment of a cell is the set of cells at a distance not greater than D.

On each d-step values of all cells is simultaneously replaced by new values. The new value of cell I after D-step was computed as a sum of values of cells belonging to the d-enviroment of the cell i m Odulo m.

The following picture shows 1-step of the 5,3-automaton.

The problem is to calculate the state of the N,m-automaton after K d-steps.

Input

The first line of the input file contains four integer numbers n, m, D, and K (1≤n≤500, 1≤m≤1 000 000, 0≤d < N?2, 1≤k≤10 000 000). The second line contains n integer numbers from 0 to M? 1-initial values of the automaton ' s cells.

Output

Output the values of the N,m-automaton ' cells after K d-steps.

Sample Input
Sample Input #1
5 3 1 1
1 2 2) 1 2

Sample Input #2
5 3 1 10
1 2 2) 1 2

Sample Output
Sample Output #1
2 2 2) 2 1

Sample Output #2
2 0 0) 2 2

Test Instructions :
There is a number of n on a ring, which defines an operation that is less than the number of D plus and re-modulo m. Refreshes all numbers for each operation. What number will it be after asking K times?

Ideas :
It's easy to think of constructing a 01 matrix, KSM solving, n too big a bit of optimization: Discovering that the matrix is regular

1100001
1110000
0111000
.。。。。
Just need to save the first row, the multiplication is calculated as follows

fori=0;i<n;i++)      forj=0;j<n;j++)      c[i]+=a[j]*b[i>=j?(i-j):(n+i-j)];

Code :

#include <iostream>#include <stdio.h>using namespace STD;intN,m,d,k;Long Longinit[505],tmp[505];voidMulLong LongA[],Long LongB[]) {Long Longc[505]; for(intI=0; i<n;i++) c[i]=0; for(intI=0; i<n;i++) for(intj=0; j<n;j++) C[i]+=a[j]*b[i>=j? ( I-J):(n+i-j)]; for(intI=0; i<n;i++) b[i]=c[i]%m; }intMain () {intI,j;scanf("%d%d%d%d", &n,&m,&d,&k); for(i=0; i<n;++i)scanf("%lld", &init[i]); for(tmp[0]=i=1; i<=d;++i) tmp[i]=tmp[n-i]=1; while(k) {if(k&1) Mul (Tmp,init); k>>=1;    Mul (TMP,TMP); } for(i=0; i<n;i++)printf("%lld", Init[i]);printf("\ n");}

"Poj 3150" Cellular automaton matrix

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.