poj 2356 Find a multiple(鸽巢原理)

时间:2021-03-14 16:27:32

Description

The input contains N natural (i.e. positive integer) numbers ( N <=  ). Each of that numbers is not greater than . 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 (  <= 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 . 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


Sample Output


Source

 

题意:有n个数,求是否存在一些数的和是n的倍数。若存在,输出即可。不存在,输出0.

思路:鸽巢原理的题目,组合数学课本上的原题。可以把和求出来,然后对n取余,因为有n个和,对n取余,如果余数中没有出现0,根据鸽巢原理,一定有两个数的余数相同,两个和想减就是n的倍数。如果余数出现0,自然就是n的倍数。也就是说,n个数中一定存在一些数的和是n的倍数。求余输出即可。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 10006
#define inf 1e12
int n;
int sum[N];
int vis[N];
int a[N];
int tmp[N];
int main()
{
while(scanf("%d",&n)==){
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++){
//int x;
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
}
memset(vis,,sizeof(vis));
memset(tmp,,sizeof(tmp));
for(int i=;i<=n;i++){
int x=sum[i]%n;
if(vis[x]){
int y=tmp[x];
printf("%d\n",i-y);
for(int j=y+;j<=i;j++){
printf("%d\n",a[j]);
}
break; }
if(x==){
printf("%d\n",i);
for(int j=;j<=i;j++){
printf("%d\n",a[j]);
}
break;
}
vis[x]=;
tmp[x]=i;
} }
return ;
}