poj 2356 Find a multiple(鸽巢原理)

本文介绍了一道经典的组合数学题目,利用鸽巢原理解决求一组数中是否存在某些数的和为给定数的倍数的问题。通过计算累积和并对给定数取余,可以高效找出满足条件的数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

题意:有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 }
View Code

 

转载于:https://www.cnblogs.com/UniqueColor/p/4811449.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值