hdoj 4648 Magic Pen 6



Magic Pen 6

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2045    Accepted Submission(s): 701


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.
 

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.
 
题意:找出最长连续子序列使它们之和能被m整除。

无语  ,以为是抽屉原理的应用,但一直没过。。。 最后暴力过了,醉了醉了。。。

#include 
#include 
#include 
#define LL long long
#define MAX 100000+10
using namespace std;
LL sum[MAX]; 
int main()
{
	int n, m, a;
	int i, j;
	int ans;//记录最大删去人数 
	int exist; 
	while(scanf("%d%d", &n, &m) != EOF)
	{
		sum[0] = 0;
		for(i = 1; i <= n; i++)
		{
			sum[i] = 0;
			scanf("%d", &a);
			sum[i] = sum[i-1] + a;
		}
		ans = 0; exist = 0;//标记是否已找到序列之和能被m整除 
		for(i = n; i >= 1; i--)//遍历长度 
		{
			for(j = 1; j+i-1 <= n; j++)//子序列j -> j+i-1 长度为i 
			{
				if((sum[j+i-1]-sum[j-1]) % m == 0)
				{
					ans = i;
					exist = 1;
					break;
				} 
			} 
			if(exist)
			break;
		}
		printf("%d\n", ans);
	}
	return 0;
}

你可能感兴趣的:(数学)