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. [ActionScript 3.0] 记录几个ByteArray 十六进制 String等相互转换的方法

    /** * 通过hax数据返回ByteArray * @param hax 格式 "AA5A000100FF" */ private function getHax(hax:Str ...

  2. Python-Django编程问题汇总

    OS:Windows10 64 IDE:JetBrain Python Community Edition 2017.3.4 Python:python-3.6.4 Django:V2.0.3 问题一 ...

  3. css元素垂直居中的8中方法

    1. 通过vertical-align:middle实现CSS垂直居中 通过vertical-align:middle实现CSS垂直居中是最常使用的方法,但是有一点需要格外注意,vertical生效的 ...

  4. c#开发Android初学(一)

    我也是最近开始学习用c#开发android,最近找在好心网友那里下到了四个android开发安卓的视频(传智的)有想要的朋友可以留下邮箱 废话不多收,分享我最近一个星期的成果. 先看最主要的一个获取数 ...

  5. Thread.Sleep(1000) 、Task.Delay(1000).Wait() 区别

    public static Task Delay(int millisecondsDelay, CancellationToken cancellationToken){    if (millise ...

  6. 关于Matlab串口发送HEX格式字符

    终于想起来更新一下关于使用Matlab串口发送HEX格式字符.这个用法主要来自于我使用Matlab对机器人进行实时轨迹跟踪的绘制,由于底层限制,自己又不想在中间增加转换模块,就需要直接发送HEX格式指 ...

  7. win8.1中向IIS注册asp.net

    以前都是用aspnet_regiis -I 命令向IIS注册asp.net ,今天换了win8.1后竟然发现这招不好使了. 根据提示,需要用dism.exe来完成注册. 折腾一会儿后,问题最终解决:  ...

  8. EasyMock set方法报错: java.lang.AssertionError

    有返回值的方法没问题, 直接andReturn就行了. EasyMock.expect(info.getWebTitle()).andReturn(StringUtils.EMPTY).anyTime ...

  9. 【Qt开发】QDate类

    QDate为开发者提供日期的类,函数也很丰富 常用方法介绍 1.QDate addDays(qint64 ndays) const 当前日期添加n天,n可以为负 2.QDate addMonths(i ...

  10. 配置MySQL接受远程登录连接

    一 开放mysql mysql的配置文件在/etc/mysql/my.cnf文件内,里面有一行bind-address = 127.0.0.1表示只允许本地访问,将这行注释即可 # bind-addr ...