`
阿尔萨斯
  • 浏览: 4181240 次
社区版块
存档分类
最新评论

uva 1386 - Cellular Automaton(循环矩阵+矩阵快速幂)

 
阅读更多

题目链接:uva 1386 - Cellular Automaton

题目大意:一个细胞自动机,有n个格子,每个格子的取值为0~m-1。给定距离d,每次操作后每个格子的值将变为到它距离不超过d的所有格子在操作之前的值之和除以m的余数。

解题思路:矩阵快速幂,因为矩阵是循环矩阵,所以记录并处理第一行即可。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 505;

int N, M, D, K;

struct Mat {
    ll arr[maxn];
};
Mat ceil, x;

Mat mul_mat (const Mat& a, const Mat& b) {
    Mat ans;

    memset(ans.arr, 0, sizeof(ans.arr));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)
            ans.arr[i] = (ans.arr[i] + a.arr[j] * b.arr[(i-j+N)%N]) % M;
    }
    return ans;
}

void pow_mat (int n) {
    Mat ans = x;

    while (n) {
        if (n&1)
            ans = mul_mat(ans, x);
        x = mul_mat(x, x);
        n >>= 1;
    }
    x = ans;
}

int main () {
    while (scanf("%d%d%d%d", &N, &M, &D, &K) == 4) {
        for (int i = 0; i < N; i++)
            scanf("%lld", &ceil.arr[i]);

        memset(x.arr, 0, sizeof(x.arr));
        for (int i = -D; i <= D; i++)
            x.arr[(i+N)%N] = 1;

        pow_mat(K-1);

        /*
        for (int i = 0; i < N; i++)
            printf("%lld ", x.arr[i]);
        printf("\n");
        */

        for (int i = 0; i < N; i++) {
            if (i)
                printf(" ");

            ll ret = 0;
            for (int j = 0; j < N; j++)
                ret = (ret + ceil.arr[j] * x.arr[(j-i+N)%N]) % M;
            printf("%lld", ret);
        }
        printf("\n");
    }
    return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics