Problem 1692 Key problem

Accept: 103    Submit: 553 Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

Whenever rxw meets Coral, he requires her to give him the laboratory key. Coral does not want to give him the key, so Coral ask him one question. if rxw can solve the problem, she will give him the key, otherwise do not give him. rxw turns to you for help now,can you help him?N children form a circle, numbered 0,1,2, ... ..., n-1,with Clockwise. Initially the ith child has Ai apples. Each round game, the ith child will obtain ( L*A(i+n-1)%n+R*A(i+1)%n ) apples. After m rounds game, Coral would like to know the number of apples each child has. Because the final figure may be very large, so output the number model M.

Input

The first line of input is an integer T representing the number of test cases to follow. Each case consists of two lines of input: the first line contains five integers n,m,L,R and M . the second line contains n integers A0, A1, A2 ... An-1. (0 <= Ai <= 1000,3 <= n <= 100,0 <= L, R <= 1000,1 <= M <= 10 ^ 6,0 <=m < = 10 ^ 9). After m rounds game, output the number model M of apples each child has.

Output

Each case separated by a space. See sample.

Sample Input

1
3 2 3 4 10000
1 2 3

Sample Output

120 133 131

Source

FOJ月赛-2009年3月--- Coral

 
矩阵构造,题意有问题。R,L正好反了。
这一题的矩阵,第一次快速幂超时了,很无奈。原因在于Multiply()是f(n^3),   时间复杂度logm*n^3,
优化的地方,就是在这。
由于构造的矩阵很有规律是同构的。“什么是同构”?
如这一题:
123
312
231
这样的话只要求出第一排,那么其他就很好求了。n^2解决。不会超630ms
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
#define N 102
typedef __int64 LL;
LL n,m,L,R,mod;
LL f[]; struct Matrix
{
LL mat[N][N];
void ini()
{
memset(mat,,sizeof(mat));
}
void init()
{
for(LL i=;i<n;i++)
for(LL j=;j<n;j++)
if(i==j) mat[i][j]=;
else mat[i][j]=;
}
void make_first()
{
for(LL i=;i<n;i++)
{
LL k1=(i+n-)%n;
mat[i][k1]=R;
LL k2=(i+)%n;
mat[i][k2]=L;
}
}
}M_hxl,M_tom; Matrix Multiply(Matrix &x, Matrix &y)
{
LL i, j, k;
Matrix z;
z.ini();
for(k = ; k < n; k ++)
if(x.mat[][k])
{
for(j = ; j < n; j ++)
if(y.mat[k][j])
z.mat[][j] = (z.mat[][j] + (LL)x.mat[][k] * y.mat[k][j]) % mod;
}
for(i = ; i < n; i ++)
{
z.mat[i][] = z.mat[i - ][n - ];
for(j = ; j < n; j ++)
z.mat[i][j] = z.mat[i - ][j - ];
}
return z;
}
void cs()
{
LL i,j;
for(i=;i<n;i++)
{
printf("\n");
for(j=;j<n;j++)
printf("%I64d ",M_hxl.mat[i][j]);
}
printf("\n");
} void power_sum2()
{
M_hxl.init();
M_hxl.make_first();
M_tom.init();
cs();
while(m)
{
if(m&)
{
M_tom=Multiply(M_tom,M_hxl);
}
m=m>>;
M_hxl=Multiply(M_hxl,M_hxl);
}
for(LL i=;i<n;i++)
{
LL sum=;
for(LL j=;j<n;j++)
{
sum=(sum+f[j]*M_tom.mat[i][j])%mod;
}
if(i==)printf("%I64d",sum);
else printf(" %I64d",sum);
}
printf("\n");
} int main()
{
LL T;
while(scanf("%I64d",&T)>)
{
while(T--)
{
scanf("%I64d%I64d%I64d%I64d%I64d",&n,&m,&L,&R,&mod);
for(LL i=;i<n;i++)
scanf("%I64d",&f[i]);
if(m==)
{
for(LL i=;i<n;i++)
{
if(i==)printf("%I64d",f[i]%mod);
else printf(" %I64d",f[i]%mod);
}
printf("\n");
continue;
}
power_sum2();
}
}
return ;
}
 
 
 
 

最新文章

  1. python 环境搭建
  2. .net乱码问题
  3. 聊聊SOA面向服务架构
  4. Best Time to Buy and Sell Stock II [LeetCode]
  5. javascript设计模式-桥接模式
  6. Item2 + zsh
  7. NSLayoutConstraint-代码实现自己主动布局的函数使用方法说明
  8. 切换view的动画
  9. Wpf解决TextBox文件拖入问题、拖放问题
  10. Google推出iOS功能性UI测试框架EarlGrey
  11. 关于http接口开发中json格式数据编码问题处理
  12. Tian Ji -- The Horse Racin
  13. 干了这杯Java之Vector
  14. JavaScript面向对象深入理解原型
  15. Charles SSL
  16. Android(五)——dex文件动态调试
  17. 通过swagger将API业务版本号与Gitlab代码版本号绑定
  18. python基础学习23----IO模型(简)
  19. 自然语言交流系统 phxnet团队 创新实训 项目博客 (六)
  20. RHCE7 管理II-3使用VIM编辑器

热门文章

  1. 记一次优化ansible inventory的小例子
  2. Centos安装Ruby2.2.3
  3. 如何检查 IP是否冲突了
  4. 关闭tomcat端口号
  5. 使用sourceTree需要注意的地方
  6. C#-WebForm-Request、Response、QueryString
  7. jQuery 获取元素当前位置offset()与position()
  8. Mac 10.12安装VirtualBox
  9. 使用NHibernate(6)-- HQL &amp;&amp; ICriteria 简单介绍
  10. C++中的名字重整技术