思路:将转换关系看做一个矩阵,此处的关系是前一个加上后一个:
1 0 0 0 0 1
1 1 0 0 0 0
0 1 1 0 0 0
0 0 1 1 0 0
0 0 0 1 1 0
0 0 0 0 1 1
把给定的数组看做矩阵[a[0],a[1],a[2],a[3],a[4],a[5]],如果和上面的转换矩阵相乘会得到矩阵[a[0]+a[1], a[1]+a[2], a[2]+a[3], a[3]+a[4], a[4]+a[5], a[5]+a[0]],就是使用一次魔力的结果,使用第二次的话就用刚才得到的矩阵乘以转换矩阵,继续转换即可。
由于矩阵相乘符合结合律,就把后面的k次转换用矩阵快速幂计算,注意在矩阵相乘时要注意对100求余。
AC代码
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 50 + 5;
typedef int state[maxn][maxn];
int w[maxn];
int n, k;
struct node{
int a[maxn][maxn];
};
const int mod = 100;
node mul(node &x, node &y) {
node res;
state &a = x.a, &b = y.a, &c = res.a;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j) {
int sum = 0;
for(int k = 0; k < n; ++k) sum += a[i][k] * b[k][j];
c[i][j] = sum % mod;
}
return res;
}
node power(int n, int k) {
node ans, tmp;
state &a = tmp.a;
//初始化
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j) {
ans.a[i][j] = tmp.a[i][j] = 0;
}
for(int i = 0; i < n; ++i) ans.a[i][i] = 1;
for(int i = 0; i < n; ++i) {
a[i][i] = 1;
a[(i+1)%n][i] = 1;
}
while(k > 0) {
if(k & 1) {
ans = mul(ans, tmp);
}
tmp = mul(tmp, tmp);
k >>= 1;
}
return ans;
}
int main() {
scanf("%d%d", &n, &k);
for(int i = 0; i < n; ++i) {
scanf("%d", &w[i]);
}
node ans = power(n, k);
vector<int>a;
for(int i = 0; i < n; ++i) {
int sum = 0;
for(int j = 0; j < n; ++j) sum += w[j]*ans.a[j][i];
a.push_back(sum%mod);
}
for(int i = 0; i < n; ++i) {
if(i == n-1) printf("%d\n", a[i]);
else printf("%d ", a[i]);
}
return 0;
}
如有不当之处欢迎指出!