时间限制 5000 ms 内存限制 65536 KB

题目描述

We define function g on an array as:

g([a0,a1,⋯,an−1])=(Σn−1l=0al) mod pn

g([a_0,a_1,\cdots,a_{n-1}]) = \frac{(\Sigma_{l=0}^{n-1} a_l) ~ mod ~ p}{n}

Here, p is a given constant, and n is the length of the array.
You are given p and an array A and a length k.
You need to find the maximum value of g over all contiguous subarrays of A that are of length ≥ k.

输入格式

The first line of input contains T, the number of test cases. Each test case contains two lines. The first line of each test case contains three integers: N, p and k, respectively.
The second line of each test case contains N integers contained in array A.

1≤T≤101 \le T \le 10
1≤N≤5∗1041 \le N \le 5*10^4
1≤K≤min(300,N),1≤P≤1091 \le K \le min(300,N) ,1 \le P \le 10^9
1≤Ai≤1091 \le A_i \le 10^9

输出格式

For each test case, output one line containing two space separated integers: p and q. If the answer is an integer x, then output x and 1 separated by a space.
Otherwise, output p and q if pq is the answer is in its simplest form.

Explanation of example
In the first test case, when length ≥2, the maximum g value is obtained when you take the entire array, so g is 53\frac{5}{3}.
In the second case, all f values are even. Hence, when we take modulo 2, the answer is 0 for any array.

In the third case, p is 1, and the value is 0 for any array.

输入样例

3
3 100 2
2 1 2
5 2 1
2 10 4 6 8
5 1 2
100 213142 3123 123 321

输出样例

5 3
0 1
0 1


题意为,找出给定序列中的一个满足长度≥k的连续子序列,使得这个子序列的序列和模上p再除以n尽可能的大。
比如a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,找出其中长度≥2的一段连续子序列,使得他们相加之后mod p = tmp,tmp / n= tmp2,使得tmp2尽可能的大。
乍看之下又是一道枚举题,但是n的范围最大是5∗1045*10^4,如果从length=k枚举到length=n,O(n2n^2)的复杂度明显就超时了,这时候有两种思路,第一种思路是自己打表出数据,用O(n2n^2)的枚举法测一下答案的连续子序列长度最大是多少,

打表代码如下

#include<bits/stdc++.h>
#define K 5
using namespace std;
int main(){srand(time(NULL));freopen("tsx.txt","w",stdout);int n=50000,k=200,ts=1000,p=1000000000;printf("%d\n",ts);while(ts--){printf("%d %d %d\n",n,p,k);for(int i=0;i<n;i++)printf("%d%c",rand()%p+1,i==n-1?'\n':' ');}return 0;
}

然后用裸的O(n2n^2)的枚举测了几千组自己生成的样例,答案长度最大小于k的1.5倍,比赛时稳妥起见开了k的3倍,时间5s绰绰有余。

第二种思路呢,是用数学思想证明,首先(a+b)%p =(a%p + b%p)%p

令a=Σ2ki=0ai\Sigma_{i=0}^{2k} a_i,b=Σ2k+xi=2k+1ai\Sigma_{i=2k+1}^{2k+x} a_i

(a+b)%p2k+x\frac{(a+b) \%p}{2k+x}=(a%p+b%p)%p2k+x\frac{(a\%p+b\%p) \%p}{2k+x}
≤ a%p2k+x\frac{a\%p}{2k+x}+ b%p2k+x\frac{b\%p}{2k+x}
≤max(a%p2k\frac{a\%p}{2k} , b%px\frac{b\%p}{x} )

即当枚举到长度≥2k但≤4k的时候(更长的情况可以递推),设长度为2k+x,一定可以划分为一个2k和x长度的两个连续子序列,它们的值一定可以代替(大于等于)原始2k+x串的值。

解决代码如下

#include<cstdio>
#include<cmath>
#include<algorithm>
#define MA 50050
using namespace std;
int a[MA],sum[MA],n,k,p;
int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
int main(){int i,j,k,len,t;for(scanf("%d",&t);t--;){scanf("%d%d%d",&n,&p,&k);for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<=n;i++)sum[i]=(sum[i-1]+a[i])%p;int up=0,down=1;int lim=k*3;//根据数学验证,取2k即可for(len=k;len<=lim;len++){for(i=0;i+len<=n;i++){int tmpa=(p+sum[i+len]-sum[i])%p;int tmpb=len;double ta=1.0*up/down;double tb=1.0*tmpa/tmpb;if(ta<tb){up=tmpa;down=tmpb;}}}int gcd_=gcd(up,down);printf("%d %d\n",up/gcd_,down/gcd_);}return 0;
}

北邮OJ 884. 16校赛-Average Modulo相关推荐

  1. 北邮OJ 1027. 16校赛-Archer in Archery

    时间限制 1000 ms 内存限制 65536 KB 题目描述 Archer(Emiya), also known as the red A, is famous for his talented s ...

  2. 北邮OJ 1022. 16校赛-Saber's Board

    时间限制 5000 ms 内存限制 65536 KB 题目描述 In a parallel universe, Saber has won the champion of every kind of ...

  3. 北邮OJ 1021. 16校赛-Stone Game

    时间限制 4000 ms 内存限制 65536 KB 题目描述 Alice and Bob are old friends in game theory. This afternoon they me ...

  4. 北邮OJ 1010. 16校赛-Binary Strings

    时间限制 5000 ms 内存限制 65536 KB 题目描述 One day, the teacher asked Weishen to judge whether a binary string ...

  5. 北邮OJ 1005. 16校赛-Hawei Learning C

    时间限制 1000 ms 内存限制 65536 KB 题目描述 Hawei is learning C programming language recently, but he is so naiv ...

  6. 北邮OJ 981. 16校赛-Saber's Number Game

    时间限制 1000 ms 内存限制 65536 KB 题目描述 Saber is fond of number games of various kinds, she particularly lik ...

  7. 北邮OJ 980. 16校赛-R_clover's Challenge

    时间限制 2000 ms 内存限制 65536 KB 题目描述 R_clover wants to challenge Mengmengda_wsw's math,so he give her a f ...

  8. 北邮oj题库刷题计划(更新ing)

    北邮oj题库刷题计划(更新ing) 83. A + B Problem 84 Single Number 85. Three Points On A Line 120 日期 121 最值问题 122 ...

  9. 北邮OJ 141 虚数

    北邮OJ 虚数 #include <bits/stdc++.h> using namespace std; typedef struct fushu{int x; //实部 int y; ...

最新文章

  1. python语言的数据类型有哪些_Python语言有哪些数据类型
  2. Transform机制(1)
  3. RPC理论以及Dubbo的使用介绍
  4. 因为此网站使用了 hsts_HSTS原理及实践
  5. 300字简单区分线程问题
  6. 微信小程序代码最大限制2M的解决方案
  7. 最终幻想OL(FF14)分析 - 基本数据
  8. 矩阵乘法np.dot()及np.multipy()区别
  9. QTTabBar+Office Tab+Quicker 助力高效使用Windows办公
  10. 解决MySQL报错[Err] 1093 - You can't specify target...
  11. 大鹏教你python数据分析
  12. 表示颜色的英语单词(图)
  13. 敏友的【敏捷个人】有感(4): 发表下个人感言,指导自己的人生
  14. 教你如何微信公众号图文中怎么下载封面图
  15. 浅谈单片机低功耗处理
  16. 史上最硬核全套Java视频教程(学习路线+视频+配套资料)
  17. 阿里钉钉Android实习面试也太太太太难了吧,对算法的要求堪比字节
  18. Android 如何选择城市-CityPicker
  19. SQLServer连接服务器维护,SQLServer远程连接服务器详细配置(sp_addlinkedserver)
  20. 【Apollo】支持@ConfigurationProperties动态刷新

热门文章

  1. CCF-百度松果基金闭门研讨会成功举办,百度飞桨提供基金平台支持
  2. 知识库问答中的关系识别研究回顾
  3. 周志华教授签名新书免费送!豆瓣满分森林书破解AI实践难题
  4. 【Java】基于注解开发初探
  5. but was actually of type 'com.sun.proxy.$Proxy**'的两种解决方法
  6. 机器人“铁手”可以保护工厂工人免受伤害
  7. 十大经典排序算法之选择排序及其优化
  8. webflux切面拦截权限,webflux整合aop,webflux获取request
  9. (二):集成日志框架:springboot使用logback日志框架
  10. Git——git push 错误[error: src refspec master does not match any]解决方案