POJ 2356-Find a multiple (drawer principle)

Find a multiple

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10270 Accepted: 4379 Special Judge

Description

The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).

Input

The first line of the input contains the single number N. Each of next N lines contains one number from the given set.

Output

In case your program decides that the target set of numbers can not be found it should print to the output the single number 0. Otherwise it should print the number of the chosen numbers in the first line followed by the chosen numbers themselves (on a separate line each) in arbitrary order.

If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.

Sample Input

5
1
2
3
4
1

Sample Output

2
2
. 3
input contains natural number N (i.e., a positive integer) (N <= 10000). Each number is not greater than 15,000. This figure is not necessarily different from (and therefore, there may be two or more equal). Your task is to select some of the given number (1 <= few <= N), so that the selected digital sum is a multiple of N (i.e., N * k (= the sum of the selected numbers) for some natural number k).
Input value
of the first line of input contains a single number N. Each row of the next N rows comprising a number given to a collection.
Output
If you can not find a program to determine the target figure set, it should print a single digit 0 in the output. Otherwise, the selected number of digits should be printed in the first row, and then the selected digital printing itself (in a separate location) of each row) in any order.

If a set of numbers has the required properties of the above, it should be only one (preferably your favorite) prints to the output.
Sample input
. 5
1
2
. 3
. 4
1
Sample Output
2
2
. 3

Title effect: a given number n, there are n number of the next, the number n is determined is not a series of numbers and is a multiple of n, if the number of the selected digital output, and then outputs the selected following number, if the set condition is not satisfied, 0 is output.

Problem-solving ideas: I did not think the outset, this question is a drawer principle, namely: n + 1 th n items into the drawer, the drawer has at least one item of not less than two, this question is how use it, we set up a prefix and an array of 1 + 2 + 3 ... + n, if 1 + ... + value to an item happens to be a multiple of n, the direct output can be. If not, then for each modulo, if there are two identical die, in accordance with the remainder theorem, their difference must be a multiple of n, the answer is required among them consecutive numbers, if said subject there is no output 0, in fact, after analysis we can see, ** this question is that there must be a solution! ! ** If not answer and the first n items, the answer in the first term and the n-1 consecutive numbers wherein n is equivalent to n-1 th items into the drawer, the drawer must have a lot of in two articles, namely, there must be two numbers% n are equal, it means that the two numbers are congruent, that answer must exist. AC Code:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int _max=2e5;
int mod[_max],sum[_max]={0},a[_max];
int main()
{
	int n;
	cin>>n;
	memset(mod,-1,sizeof mod);//存放余数的位置
	for(int i=1;i<=n;i++)
	  cin>>a[i];
	for(int i=1;i<=n;i++)
	{
		sum[i]=sum[i-1]+a[i];
		if(sum[i]%n==0)
		{
			cout<<i<<endl;
			for(int j=1;j<=i;j++)
			  cout<<a[j]<<endl;
			break;  
		}
		if(mod[sum[i]%n]!=-1)//如果位置不是-1则说明这个数之前出现过了。
		{
      	cout<<i-mod[sum[i]%n]<<endl;
      	for (int j=mod[sum[i]%n]+1;j<=i;j++)
         	cout<<a[j]<<endl;
         break;
        }
      mod[sum[i]%n]=i;
	}
	return 0;
}
Published 38 original articles · won praise 1 · views 1174

Guess you like

Origin blog.csdn.net/aezakmias/article/details/104800250