您的位置:首页 > 其它

uva1386(矩阵快速幂)

2015-07-31 16:35 316 查看
题意:

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

思路:

矩阵快速幂

#include <cstdio>
#include <cstring>
#define ll long long
const int N = 505;

int n, M, D, K;

struct Mat {
ll arr
;
};
Mat ceil, x;

Mat mul (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 (int n) {
Mat ans = x;

while (n) {
if (n&1)
ans = mul(ans, x);
x = mul(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(K-1);
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: