# Poj 3150

slove

$$A = \left[ \begin{matrix} 1 & 1 & 0 & \dots & 0 & 1 \\ 1 & 1 & 1 & 0 & \dots & 0 \\ 0 & 1 & 1 & 1 & 0 & \dots \\ \dots & 0 & 1 & 1 & 1 & 0 \\ 1 & \dots & 0 & 0 & 1 & 1 \\ 1 & 1 & \dots & 0 & 0 & 1 \\ \end{matrix} \right]$$

#include <iostream>
#include <cstdio>
#include <cstring>

const int N = 510;

#define LL long long

LL A[N], B[N], C[N], T[N];
int n, M, d, k;

void Mul(LL *a, LL *b, LL *c) {
memset(T, 0, sizeof T);
for(int i = 0; i < n; i ++)
for(int j = 0; j < n; j ++) {
if(i - j >= 0) T[i] = (T[i] + (a[j] * b[i - j]) % M) % M;
else T[i] = (T[i] + (a[j] * b[i - j + n]) % M) % M;
}
for(int i = 0; i < n; i ++) c[i] = T[i];
}

void Ksm() {
while(k) {
if(k & 1) Mul(B, A, B); // B = B * A;
Mul(A, A, A); // A = A * A;
k >>= 1;
}
}

int main() {
std:: cin >> n >> M >> d >> k;
for(int i = 0; i < n; i ++) std:: cin >> C[i];
for(int i = 0; i <= d; i ++) A[i] = 1;
for(int i = n - 1; i >= n - d; i --) A[i] = 1;
B[0] = 1;
Ksm();
Mul(B, C, C);
for(int i = 0; i < n; i ++) std:: cout << C[i] << " ";
return 0;
}

posted @ 2018-06-25 19:46  qmey  阅读(83)  评论(0编辑  收藏  举报