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 ;
}
 
 
 
 

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. jquery源码解析:jQuery数据缓存机制详解2

    上一课主要讲了jQuery中的缓存机制Data构造方法的源码解析,这一课主要讲jQuery是如何利用Data对象实现有关缓存机制的静态方法和实例方法的.我们接下来,来看这几个静态方法和实例方法的源码解 ...

  2. 我从Linux走来,选择了Windows

    我从Linux走来,选择了Windows 几天前就想提笔写下这篇,理解很多人一定会对言论不苟同. 但是我相信您看完一个一年多的Linux用户写完的以后,一定也不会太反对 一.为什么我成为一名 Linu ...

  3. 敏感词过滤的算法原理之DFA算法

    参考文档 http://blog.csdn.net/chenssy/article/details/26961957 敏感词.文字过滤是一个网站必不可少的功能,如何设计一个好的.高效的过滤算法是非常有 ...

  4. [总结帖] 后端MVC V.S. 前端MVVM

    Web编年史: Web1.0 —— 静态页面.简单预处理语言草案:PHP.JSP.ASP Web2.0 —— 企业级架构.一站式解决方案(MVC):J2EE.Spring.Asp.net Web2.5 ...

  5. 聊聊Postgres中的IPC之SI Message Queue

    在 PostgreSQL中,每一个进程都有属于自己的共享缓存(shared cache).例如,同一个系统表在不同的进程中都有对应的Cache来缓存它的元组(对于RelCache来说缓存的是一个Rel ...

  6. 解决无法运行Terminator出现以下问题: File "/usr/bin/terminator"...SyntaxError: invalid syntax

    在安装或者启动Terminator时可能出现这个问题: lin@Dev:~$ terminator File "/usr/bin/terminator", line 123 exc ...

  7. mac安装gdb调试(转载)

    转载自:http://blog.plotcup.com/a/129 最近一直用go写一个项目,本想在mac上用gdb调试一下,但xcode4.6带的gdb版 本还是太低了,不支持go,只好自己安装一个 ...

  8. LINUX云服务器 安装 nginx

    什么是nginx? 是一个高性能的 HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服, 是一个asynchronousservers异步服务器 为什么使用nginx? 因为它的 ...

  9. 课堂练习:ex 4-20

    一.习题要求 • 定义一个复数类Complex. • 有相加,输出,模计算函数. • 模计算要求结果保存在第一个复数中. 二.习题内容 //complex.h # ifndef COMPLEX_H # ...

  10. 网络基础 02_TCP/IP模型

    1 TCP/IP参考模型概述   2 应用层 3 传输层 3.1 传输控制协议(TCP) 面向连接 可靠传输 流控及窗口机制 使用TCP的应用: Web浏览器:电子邮件: 文件传输程序 3.2 用户数 ...