ACM: 矩阵变形题 数论题 poj 3150

来源:互联网 发布:提花组织软件 编辑:程序博客网 时间:2024/06/06 00:22
Cellular Automaton
Description

A cellular automatonis a collection of cells on a grid of specified shape that evolvesthrough a number of discrete time steps according to a set of rulesthat describe the new state of a cell based on the states ofneighboring cells. The order of the cellular automaton isthe number of cells it contains. Cells of the automaton of ordern are numbered from 1 to n.

The order of the cell is the number of different valuesit may contain. Usually, values of a cell of order m areconsidered to be integer numbers from 0 to m − 1.

One of the most fundamental properties of a cellular automatonis the type of grid on which it is computed. In this problem weexamine the special kind of cellular automaton — circular cellularautomaton of order n with cells of order m. We willdenote such kind of cellular automaton as n,m-automaton.

A distance between cells i and j inn,m-automaton is defined as min(|ij|, n − |ij|). A d-environment ofa cell is the set of cells at a distance not greater thand.

On each d-step values of all cells are simultaneouslyreplaced by new values. The new value of cell i afterd-step is computed as a sum of values of cells belonging tothe d-enviroment of the cell i modulo m.

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

ACM: <wbr>矩阵变形题 <wbr>数论题 <wbr>poj <wbr>3150

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

Input

The first line of the inputfile contains four integer numbers n, m, d,and k (1 ≤ n ≤ 500, 1 ≤ m ≤ 1 000 000, 0 ≤d < n2 , 1 ≤k ≤ 10 000 000). The second line contains n integer numbersfrom 0 to m − 1 — initial values of the automaton’scells.

Output

Output the values of then,m-automaton’s cells after kd-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

题意: Cellular Automaton收集细胞, 现在有n个grid, 每个grid上的值是0~m-1,
      d-steps: 每一次收集当前位置相邻d距离的细胞, 进行d-steps步骤k次.
      求最后每个grid的结果模m
解题思路:
      1. 这题有点怪, 为什么样例输入输出要加"sample input/output #1"这些,
         而输入输出的时候却没有.
      2. 其实题意很明白, 每次操作就是把当前位置的值加上左右下标距离不超过d的值.
      问题分析:
         第一个样例 [1 2 2 1 2]进行一次d-steps操作:
         [(a4+a0+a1) (a0+a1+a2) (a1+a2+a3) (a2+a3+a4) (a3+a4+a0)]
         显然, 我们可以构建一个矩阵来表示一次操作.
         [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]
         思路出来了: 设初始矩阵A, 操作矩阵B. 求: A*B^k;
         B^k可以用二分方法解决.
      3. 问题再出现了: 1 ≤ n ≤ 500 给的范围有点大. 再观察发现
                 
B^1 =           B^2 =           B^3 =
         [1 1 0 0 1]     [3 2 1 1 2]     [7 6 4 4 6]   
         [1 1 1 0 0]     [2 3 2 1 1]     [6 7 6 4 4]  
         [0 1 1 1 0]     [1 2 3 2 1]     [4 6 7 6 4]  
         [0 0 1 1 1]     [1 1 2 3 2]    [4 4 6 7 6] 
         [1 0 0 1 1]     [2 1 1 2 3]     [6 4 4 6 7]
         每一行的操作是往下滚动一位.即: B[i][j] = B[i-1][j-1];
         这样就可以只需开一维数组即可.一个简单的例子即可: B*B
         [1 1 0 0 1]     [1]         [0 1 2 3 4]
         [1 1 1 0 0]     [1]第二个矩阵[4 0 1 2 3]
         [0 1 1 1 0]  [0] 下标表示 [3 4 0 1 2]
         [0 0 1 1 1]     [0] = = = =[2 3 4 0 1]
         [1 0 0 1 1]     [1]         [1 2 3 4 0]
         for(int i = 0; i < n; ++i)
         {
             for(int j = 0; j < n; ++j)
                 c[i] += A[j]*B[ i<=j ? (j-i) : (n-i+j)];
         }
         在网上看到别人的题解是: c[i] += A[j]*B[ i>=j ? (i-j) : (n+i-j)];

代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 501

int n, m, d, K;
long long a[MAX], temp[MAX];
int num;

inline void multiply(long long *A, long long *B, int mod)
{
    long long c[MAX];
    for(int i = 0; i < n; ++i)
    {
        c[i] = 0;
        for(int j = 0; j < n; ++j)
            c[i] += A[j]*B[ i<=j ? (j-i) : (n-i+j)];
    }
    for(int i = 0; i < n; ++i)
        A[i] = c[i]%mod;
}

void pow(long long *A, int n, int mod)
{
    while(n)
    {
        if(n%2 != 0)
            multiply(a,temp,mod);
        multiply(temp,temp,mod);
        n /= 2;
    }
}

int main()
{
//    freopen("input.txt","r",stdin);
    while(scanf("%d %d %d %d",&n, &m, &d, &K) != EOF)
    {
        memset(temp,0,sizeof(temp));
        for(int i = 0; i < n; ++i)
            scanf("%lld",&a[i]);
        temp[0] = 1;
        for(int i = 1; i <= d; ++i)
            temp[i] = temp[n-i] = 1;
        pow(temp,K,m);
        for(int i = 0; i < n; ++i)
        {
            if(i == 0) printf("%lld",a[i]);
            else printf(" %lld",a[i]);
        }
        printf("\n");
    }
    return 0;
}
0 0
原创粉丝点击