Magic Pen 6
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 964 Accepted Submission(s): 351
Problem Description
In HIT, many people have a magic pen. Lilu0355 has a magic pen, darkgt has a magic pen, discover has a magic pen. Recently, Timer also got a magic pen from seniors.
At the end of this term, teacher gives Timer a job to deliver the list of N students who fail the course to dean's office. Most of these students are Timer's friends, and Timer doesn't want to see them fail the course. So, Timer decides to use his magic pen to scratch out consecutive names as much as possible. However, teacher has already calculated the sum of all students' scores module M. Then in order not to let the teacher find anything strange, Timer should keep the sum of the rest of students' scores module M the same.
Plans can never keep pace with changes, Timer is too busy to do this job. Therefore, he turns to you. He needs you to program to "save" these students as much as possible.
At the end of this term, teacher gives Timer a job to deliver the list of N students who fail the course to dean's office. Most of these students are Timer's friends, and Timer doesn't want to see them fail the course. So, Timer decides to use his magic pen to scratch out consecutive names as much as possible. However, teacher has already calculated the sum of all students' scores module M. Then in order not to let the teacher find anything strange, Timer should keep the sum of the rest of students' scores module M the same.
Plans can never keep pace with changes, Timer is too busy to do this job. Therefore, he turns to you. He needs you to program to "save" these students as much as possible.
Input
There are multiple test cases.
The first line of each case contains two integer N and M, (0< N <= 100000, 0 < M < 10000),then followed by a line consists of N integers a 1,a 2,...a n (-100000000 <= a 1,a 2,...a n <= 100000000) denoting the score of each student.(Strange score? Yes, in great HIT, everything is possible)
The first line of each case contains two integer N and M, (0< N <= 100000, 0 < M < 10000),then followed by a line consists of N integers a 1,a 2,...a n (-100000000 <= a 1,a 2,...a n <= 100000000) denoting the score of each student.(Strange score? Yes, in great HIT, everything is possible)
Output
For each test case, output the largest number of students you can scratch out.
Sample Input
2 3 1 6 3 3 2 3 6 2 5 1 3
Sample Output
1 2 0HintThe magic pen can be used only once to scratch out consecutive students.
Source
Recommend
zhuyuanchen520
这个题就是要求最多可以一次性划掉多少学生保证剩下的学生成绩和取M得模与原来相同,开始我以为只要用后缀数组记录一下然后再暴力求解就好,可那明显是会超时的,后面同学给我说了为什么不直接记录他们取模的值就好,诶我明显是死脑筋了,对啊,干嘛不直接记录取模,每次记住第一次出现的模制的位置,后面再次出现之需要用当前坐标减去第一次坐标就好,不过这里面有个比较坑的地方就是,0至始至终的最初坐标就是0,这里是WA好几次的地方。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max(a,b)(a>b?a:b)
int a[100005];
int f[100005];
int p[10005];
int vis[10005];
int main(){
int n,m,pos,dis;
while(scanf("%d%d",&n,&m)==2){
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
f[i]=(f[i-1]+a[i]);
f[i]=(f[i]%m+m)%m;
}
dis=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++){
if(!vis[f[i]]){
p[f[i]]=i;
vis[f[i]]=1;
}
if(vis[f[i]]){
p[0]=0;
dis=max(dis,i-p[f[i]]);
}
}
printf("%d\n",dis);
}
return 0;
}