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. 渗透日常之 花式实战助你理解CSRF

    本文作者:i春秋签约作家——onls辜釉 最近比较忙,很久没发文章了,Onls本就只是一个安全爱好者,工作也不是安全相关.以往的文章也更像是利用简单漏洞的“即兴把玩”,更多的是偏向趣味性,给大家增加点 ...

  2. [真题] 一道 vsftp 运维题

    一道 vsftp 运维题 一.前言 在 V 站上凑巧看到了好友发的求助帖,五天时间一个理他的都没有.哈哈哈~ 废话不多说,我们来试试. 二.题目 这里我们假设存在这样的场景: 网络内有普通用户 ade ...

  3. jenkins详解(一)

    还是以以下几个问题来学习这个软件: 1.jenkins是什么? 2.为什么要用jenkins? 3.怎么用jenkins? 1.jenkins是什么? Jenkins是一个开源的.提供友好操作界面的持 ...

  4. iOS学习笔记(6)——翻译苹果文档About Windows and Views

    About Windows and Views 关于窗口和视图 In iOS, you use windows and views to present your application’s cont ...

  5. mysql 查询小技巧

    数据字段中存放的是id集,形如  1,2,15,35   也可类推json格式 查询时不用拆分了, 用上 instr.concat搜索和连接字符串 查询fids中包含15的 select * from ...

  6. 静态网页、动态网页、apache和tomcat之间区别和联系

    1.静态网页 静态网页:在网站设计中, 纯粹的HTML(标准通用标志语言下的一个应用)格式的网页通常被称为"静态网页",静态网页是标准的HTML文件,它的拓拓展名是.html或者. ...

  7. Spring注入方式(1)

    Spring支持3种依赖注入方式,分别为属性注入.构造器注入和工厂方法注入(很少使用,不推荐),下面分别对属性注入和构造器注入详细讲解. 1.常量注入 属性注入是通过setter方法注入Bean的属性 ...

  8. DataTables复杂表头

    工作上的需要,要做一个复杂的表头的DataTables thead如下 遇到的问题(详细问题可以浏览官网的答案 链接) 需自定义表头(thead),如果不自定义则会 Cannot read prope ...

  9. 1059 C语言竞赛 (20 分)

    #include <iostream> #include <iomanip> #include <cmath> using namespace std; <& ...

  10. 完美原创:centos7.1 从源码升级安装Python3.5.2

    (原创)完美原创:centos7.1 从源码升级安装Python3.5.2 下载Python3.5.2源码:https://www.python.org/downloads/release/pytho ...