poj 2356 Find a multiple(鸽巢原理)

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

Source

Ural Collegiate Programming Contest 1999

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

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

 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<math.h>
 7 #include<algorithm>
 8 #include<queue>
 9 #include<set>
10 #include<bitset>
11 #include<map>
12 #include<vector>
13 #include<stdlib.h>
14 using namespace std;
15 #define max(a,b) (a) > (b) ? (a) : (b)
16 #define min(a,b) (a) < (b) ? (a) : (b)
17 #define ll long long
18 #define eps 1e-10
19 #define MOD 1000000007
20 #define N 10006
21 #define inf 1e12
22 int n;
23 int sum[N];
24 int vis[N];
25 int a[N];
26 int tmp[N];
27 int main()
28 {
29     while(scanf("%d",&n)==1){
30         memset(sum,0,sizeof(sum));
31         for(int i=1;i<=n;i++){
32             //int x;
33             scanf("%d",&a[i]);
34             sum[i]=sum[i-1]+a[i];
35         }
36         memset(vis,0,sizeof(vis));
37         memset(tmp,0,sizeof(tmp));
38         for(int i=1;i<=n;i++){
39             int x=sum[i]%n;
40             if(vis[x]){
41                 int y=tmp[x];
42                 printf("%d\n",i-y);
43                 for(int j=y+1;j<=i;j++){
44                     printf("%d\n",a[j]);
45                 }
46                 break;
47
48             }
49             if(x==0){
50                 printf("%d\n",i);
51                 for(int j=1;j<=i;j++){
52                     printf("%d\n",a[j]);
53                 }
54                 break;
55             }
56             vis[x]=1;
57             tmp[x]=i;
58         }
59
60     }
61     return 0;
62 }

时间: 2024-12-25 16:27:20

poj 2356 Find a multiple(鸽巢原理)的相关文章

poj 2356 Find a multiple 鸽巢原理的简单应用

题目要求任选几个自然数,使得他们的和是n的倍数. 由鸽巢原理如果我们只选连续的数,一定能得到解. 首先预处理前缀和模n下的sum,如果发现sum[i]==sum[j] 那么(sum[j]-sum[i])%n一定为0,直接输出i+1~j就够了. 为什么一定会有解,因为sum从1~n有n个数,而模n下的数只有0~n-1,把n个数放入0~n-1个数里,怎么也会有重复,所以这种构造方法一定没问题. 其实可以O(n)实现,嫌麻烦,就二重循环无脑了. #include <iostream> #includ

POJ 2356 Find a multiple 鸽巢原理

题目来源:POJ 2356 Find a multiple 题意:n个数 选出任意个数 使得这些数的和是n的倍数 思路:肯定有解 并且解是连续的一段数 证明: 假设有m个数 a1,a2,a3...am    s1 s2 s3...sm为前缀和 s1 = a1 s2 = a1+a2 s3 = a1+a2+a3... sm = a1+a2+a3+...+am 1.如果某个前缀和si%m == 0 那么得到解 2.设x1=s1%m x2 = s2%m x3 = s3%m xm = sm%m 因为1不成

poj 2356 Find a multiple (鸽巢原理妙用)

题目链接:http://poj.org/problem?id=2356 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

POJ 2356 find multiple 鸽巢原理

我们在浏览一些网站,尤其是一些小说网站的时候,都会有修改页面背景颜色的地方,这个功能使用jquery很容易实现. 效果图: show you code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>jquery test</title> <script src="jquery-1.11.1.min.js">&

POJ 2356 Find a multiple (dp + 鸽笼原理)

OJ题目:click here~~ 题目分析:n个数,从中取若干个数,和为n的倍数.给出一种取法. 因为只要给出其中一种方案就行,鸽笼原理可以求出取出的数为连续的方案. 关于鸽笼原理,点这里~ 直接贴过来: 有n+1件或n+1件以上的物品要放到n个抽屉中,那么至少有一个抽屉里有两个或两个以上物品. 如果你知道这个结论: a1,a2,a3...am是正整数序列,至少存在整数k和r,1<=k<r<=m,使得ak+a(k+1)+...+a(r)是m的倍数. 证明比较简单: Sk表示前k个数之和

[POJ2356] Find a multiple 鸽巢原理

Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8776   Accepted: 3791   Special Judge Description The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000

poj 3370 Halloween treats(鸽巢原理)

Description Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a child will get nothing if it is too l

Poj2356Find a multiple鸽巢原理

一定存在连续的k个数,使得他们的和能被n整除.设a[i]为前缀和 a[1]%n  ,a[2]%n,...,a[n]%n的值的范围<n,所以有n个数小与n,肯定会出现两个一样的数,表明了,第二个数比第一个数多出来的一部分一定能被n整除. 要注意处理 前缀和中出现0的情况. #include<iostream> #include<cstdio> #include<cstring> #include<map> #include<vector> #

POJ 2356. Find a multiple 抽屉/鸽巢原理

Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   Special Judge Description The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000