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
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cstdlib>
  5. using namespace std;
  6. #define N 102
  7. typedef __int64 LL;
  8. LL n,m,L,R,mod;
  9. LL f[];
  10.  
  11. struct Matrix
  12. {
  13. LL mat[N][N];
  14. void ini()
  15. {
  16. memset(mat,,sizeof(mat));
  17. }
  18. void init()
  19. {
  20. for(LL i=;i<n;i++)
  21. for(LL j=;j<n;j++)
  22. if(i==j) mat[i][j]=;
  23. else mat[i][j]=;
  24. }
  25. void make_first()
  26. {
  27. for(LL i=;i<n;i++)
  28. {
  29. LL k1=(i+n-)%n;
  30. mat[i][k1]=R;
  31. LL k2=(i+)%n;
  32. mat[i][k2]=L;
  33. }
  34. }
  35. }M_hxl,M_tom;
  36.  
  37. Matrix Multiply(Matrix &x, Matrix &y)
  38. {
  39. LL i, j, k;
  40. Matrix z;
  41. z.ini();
  42. for(k = ; k < n; k ++)
  43. if(x.mat[][k])
  44. {
  45. for(j = ; j < n; j ++)
  46. if(y.mat[k][j])
  47. z.mat[][j] = (z.mat[][j] + (LL)x.mat[][k] * y.mat[k][j]) % mod;
  48. }
  49. for(i = ; i < n; i ++)
  50. {
  51. z.mat[i][] = z.mat[i - ][n - ];
  52. for(j = ; j < n; j ++)
  53. z.mat[i][j] = z.mat[i - ][j - ];
  54. }
  55. return z;
  56. }
  57. void cs()
  58. {
  59. LL i,j;
  60. for(i=;i<n;i++)
  61. {
  62. printf("\n");
  63. for(j=;j<n;j++)
  64. printf("%I64d ",M_hxl.mat[i][j]);
  65. }
  66. printf("\n");
  67. }
  68.  
  69. void power_sum2()
  70. {
  71. M_hxl.init();
  72. M_hxl.make_first();
  73. M_tom.init();
  74. cs();
  75. while(m)
  76. {
  77. if(m&)
  78. {
  79. M_tom=Multiply(M_tom,M_hxl);
  80. }
  81. m=m>>;
  82. M_hxl=Multiply(M_hxl,M_hxl);
  83. }
  84. for(LL i=;i<n;i++)
  85. {
  86. LL sum=;
  87. for(LL j=;j<n;j++)
  88. {
  89. sum=(sum+f[j]*M_tom.mat[i][j])%mod;
  90. }
  91. if(i==)printf("%I64d",sum);
  92. else printf(" %I64d",sum);
  93. }
  94. printf("\n");
  95. }
  96.  
  97. int main()
  98. {
  99. LL T;
  100. while(scanf("%I64d",&T)>)
  101. {
  102. while(T--)
  103. {
  104. scanf("%I64d%I64d%I64d%I64d%I64d",&n,&m,&L,&R,&mod);
  105. for(LL i=;i<n;i++)
  106. scanf("%I64d",&f[i]);
  107. if(m==)
  108. {
  109. for(LL i=;i<n;i++)
  110. {
  111. if(i==)printf("%I64d",f[i]%mod);
  112. else printf(" %I64d",f[i]%mod);
  113. }
  114. printf("\n");
  115. continue;
  116. }
  117. power_sum2();
  118. }
  119. }
  120. return ;
  121. }
 
 
 
 

fuzhou 1692 Key problem ***的更多相关文章

  1. FZU 1692 Key problem( 循环矩阵优化 + 矩阵快速幂)

    链接:传送门 题意: n个小朋友围成一个环( 2 <= n <= 100 )然后进行m次的游戏. 一开始,第 i 个小朋友有 Ai 个苹果. 定义游戏的规则为:每一次游戏处于 i 位置的小 ...

  2. 【BZOJ】【1640】【USACO2007 Nov】/【1692】【USACO2007 Dec】队列变换

    后缀数组/贪心 每次从等待序列的头或尾拿出一个放到答案序列的末尾,那么每次贪心比较头和尾的字典序大小即可…… TAT贪心很好想,但是我一开始没想到是可以直接比较字符串大小……而是一位一位判的,WA了… ...

  3. Complexity and Tractability (3.44) - The Traveling Salesman Problem

    Copied From:http://csfieldguide.org.nz/en/curriculum-guides/ncea/level-3/complexity-tractability-TSP ...

  4. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  5. Spring MVC 学习 -- 创建过程

    Spring MVC 学习 -- 创建过程 Spring MVC我们使用的时候会在web.xml中配置 <servlet> <servlet-name>SpringMVC< ...

  6. SpringMVC源码剖析(三)- DispatcherServlet的初始化流程

    在我们第一次学Servlet编程,学Java Web的时候,还没有那么多框架.我们开发一个简单的功能要做的事情很简单,就是继承HttpServlet,根据需要重写一下doGet,doPost方法,跳转 ...

  7. Spring MVC 学习总结(一)——MVC概要与环境配置

    一.MVC概要 MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范,用一种将业务逻辑.数据.显示分离的方法组织代码,MVC主要作用是降低了视图与业务 ...

  8. SpringMVC解析3-DispatcherServlet组件初始化

    在spring中,ContextLoaderListener只是辅助功能,用于创建WebApplicationContext类型实例,而真正的逻辑实现其实是在DispatcherServlet中进行的 ...

  9. Oracle常见的几种等待事件

    1. CPU time CPU time其实不是真正的等待事件.是衡量CPU是否瓶颈的一个重要指标.一般来讲,一个良好的系统,CPU TIME 应该排在TOP 5 TIME Event的最前面. 当然 ...

随机推荐

  1. 《快学Scala》第四章 映射与元组

  2. 0基础浅谈反射型xss(2)

    0x1:回顾前文疑惑“先闭合,在构造” 上一篇,我们说到了xss的一个触发精髓,“先闭合,在构造”,对于前面的先闭合,我们来简单的解释一下:   首先说,为什么要闭合? 因为HTML标签都是成对出现的 ...

  3. 白帽hacker酷炫小技能大盘点!

    白帽子是谁? 他们与一行行代码打交道,在“0”和“1”的世界中寻找风险,在IT产业软硬件核心技术和代码等自主研发能力不足.安全防护手段滞后.地下黑客业务已形成产业链的背景下,我国网络安全正面临日益严峻 ...

  4. HTML5语义化标签总结

    1.语义化标签总结 基础布局标签 <header></header> <nav></nav> <main></main> < ...

  5. 网络请求及各类错误代码含义总结(包含AFN错误码大全)

    碰见一个很奇葩的问题, 某些手机在设置了不知什么后, 某些 APP 死活 HTTPS 请求失败, 例如以 UMeng 统计HTTP 请求失败为例, Log如下: UMLOG: (Error   App ...

  6. keycloak ssl-required报错问题处理

       两台主机,网段不同,第一台129.30.108.179/24    第二台172.16.160.92/24 都安装keycloak :    docker run -d --name keycl ...

  7. QuantLib 金融计算——基本组件之 Index 类

    目录 QuantLib 金融计算--基本组件之 Index 类 QuantLib 金融计算--基本组件之 Index 类 Index 类用于表示已知的指数或者收益率,例如 Libor 或 Shibor ...

  8. Collections.singletonList方法的使用

    方法注释 /** * Returns an immutable list containing only the specified object. * The returned list is se ...

  9. Python使用浏览器模拟访问页面之使用ip代理

    最近需要使用浏览器模拟访问页面,同时需要使用不同的ip访问,这个时候就考虑到在使用浏览器的同时加上ip代理. 本篇工作环境为win10,python3.6. Chorme 使用Chrome浏览器模拟访 ...

  10. Flask 数据库迁移

    在开发过程中,需要修改数据库模型,而且还要在修改之后更新数据库.最直接的方式就是删除旧表,但这样会丢失数据. 更好的解决办法是使用数据库迁移框架,它可以追踪数据库模式的变化,然后把变动应用到数据库中. ...