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. SpringMvc redirect

    SpringMVC redirect 核心 首先网上百度到的资源基本已经够用, 留作记录. SpringMVC-redirect重定向跳转传值 虽然这哥们也是转的, 但又没有留源地址. 因此 ... ...

  2. AFNetworking 3.1.0 使用中某些知识点讲解

    # POST / GET  请求 /*! 首先要知道,POST请求不能被缓存,只有 GET 请求能被缓存.因为从数学的角度来讲,GET 的结果是 幂等 的,就好像字典里的 key 与 value 就是 ...

  3. uml地址栏参数特殊字符处理

    转义方法: function URLencode(sStr) { return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22') ...

  4. Android Studio如何配置adb以及常用命令

    https://blog.csdn.net/google_huchun/article/details/53314046 用Android Studio一年多了,都没有使用其调试adb,今天就分享ad ...

  5. python连接,操作 InfluxDB

    准备工作 启动服务器 执行如下命令: service influxdb start 示例如下: [root@localhost ~]# service influxdb start Starting ...

  6. Fleury算法

    关于为什么不选桥 因为选桥之后会变成两个联通分支,这时由于可能产生的新联通分支不是孤立顶点,他俩都不联通了,那么也就绝对不可能“一笔画”走下来了 关于为什么可以选除桥之外的任意一条边走 本质原因是因为 ...

  7. TP5 隐藏入口文件 index.php

    找到public下的.htaccess <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine ...

  8. 织梦dede解决“更新数据库archives表时出错"方法

    登陆dedecms网站管理后台,选择执行 sql命令工具,将下列命令执复制进去并执行多行执行,该问题就可以解决. alter table `idea_archives` ADD `voteid` me ...

  9. Mac 10.12原生方法对NTFS分区进行读写的配置

    说明:不一定有效,最简单的方法就是不用NTFS,直接FAT32,对于大文件就用切割. 方法: 1.确定U盘名称 diskutil list ls /Volumes/ 2.比如我找到的U盘名称为Unti ...

  10. @using (Html.BeginForm())和@{Html.BeginForm();}@{Html.EndForm();}对比

    这样写报错 <body>    @using (Html.BeginForm())    {         form主体1    }    @{Html.BeginForm();}    ...