Magic Pen 6
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 217 Accepted Submission(s): 108
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)
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.
Source
多校第五场的1006,给一串数,在其中删去连续的最长的一段,使其和模m后保持不变。
看到此题使我想起了POJ 3274,自学Hash时做的一道题,不过这个简单多了,于是很快AC
做法就是维护一个前序和的模,并把这个下标放到Hash表中,然后在Hash表中找一个下标距离最远的删掉即可
依然要注意的是0的初始化问题,在POJ 3274里面有详解
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<vector> 4 5 using namespace std; 6 7 int n,m; 8 int sum[100010]; 9 vector<int> hash[10010]; 10 11 int main() 12 { 13 while(cin>>n>>m) 14 { 15 int temp; 16 17 sum[0]=0; 18 for(int i=0;i<10010;i++) 19 hash[i].clear(); 20 hash[0].push_back(0); 21 22 for(int i=1;i<=n;i++) 23 { 24 scanf("%d",&temp); 25 sum[i]=(sum[i-1]+temp%m+m)%m; 26 hash[sum[i]].push_back(i); 27 } 28 29 int ans=0; 30 for(int i=0;i<10010;i++) 31 if(hash[i].size()>=2) 32 { 33 int t=hash[i][hash[i].size()-1]-hash[i][0]; 34 if(t>ans) 35 ans=t; 36 } 37 38 cout<<ans<<endl; 39 } 40 41 return 0; 42 }