#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64
LL mod,n;
LL mul(LL a[],LL b[])//循环矩阵的乘法
{
LL i,j,c[101];
for(i=0;i<n;i++)
{
c[i]=0;
for(j=0;j<n;j++)
c[i]=(a[j]*b[(i-j+n)%n]+c[i])%mod;
}
memcpy(a,c,sizeof(c));
}
void pow_mod(LL a[],LL b)
{
LL s[101];
LL i,j;
memset(s,0,sizeof(s));
s[0]=1;
while(b)
{
if(b&1)
mul(s,a);
mul(a,a);
b=b>>1;
}
memcpy(a,s,sizeof(s));
}
int main()
{
LL T;
cin>>T;
while(T--)
{
LL m,l,r,i,j,k,a[101],e[101];
cin>>n>>m>>l>>r>>mod;
for(i=0;i<n;i++)
cin>>a[i];
memset(e,0,sizeof(e));
e[0]=1;e[1]=r;e[n-1]=l;
pow_mod(e,m);
LL ans[101];
for(i=0;i<n;i++)
{
ans[i]=0;
for(j=0;j<n;j++)
ans[i]=(ans[i]+a[j]*e[(i-j+n)%n])%mod;
}
cout<<ans[0];
for(i=1;i<n;i++)
cout<<" "<<ans[i];
cout<<endl;
}
return 0;
}
/*
循环矩阵:
|a0 a1 a2 a3|*|1 r 0 l|^m=|b0 b1 b2 b3|
|l 1 r 0|
|0 l 1 r|
|r 0 l 1|
循环矩阵:每一层都等于上一层右移一格,循环矩阵相乘还是循环矩阵。所以可以用一维数组表示。
时间复杂度降为O(n^2logm)
*/
fzu 1692 Key problem 循环矩阵
最新推荐文章于 2019-01-08 08:27:05 发布