Magic Pen 6
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 2565 Accepted Submission(s): 913
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 0
Hint
The magic pen can be used only once to scratch out consecutive students.
思路:大致是
https://www.2cto.com/kf/201308/234446.html这位博主的思路,先把前缀和求出来,再枚举0~M-1的模数,a[i~j]=sum[j]-sum[i], 求出最大区间长度.
见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int sum[100010];
int Hash1[10010];
int Hash2[10010];
int main()
{
int n,m,t;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(sum,0,sizeof(sum));
memset(Hash1,-1,sizeof(Hash1));
memset(Hash2,-1,sizeof(Hash2));
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
sum[i]=(sum[i-1]+t)%m;
if(sum[i]<0)
sum[i]=(sum[i]+m)%m;
Hash1[0]=0;
if(Hash1[sum[i]]==-1)
{
Hash1[sum[i]]=i;
}
else
{
Hash2[sum[i]]=i;
}
}
int len=0;
for(int i=0;i<=m-1;i++)
{
if(Hash2[i]==-1)
continue;
else
{
len=max(Hash2[i]-Hash1[i],len);
}
}
printf("%d\n",len);
}
return 0;
}