Find a multiple(抽屉原理)
来源:互联网 发布:广发大数据 编辑:程序博客网 时间:2024/06/04 23:34
Find a multiple
Time Limit: 1000MS Memory Limit: 65536K
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
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8335 Accepted: 3628 Special Judge
DescriptionThe 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
题目分析:
题目大意就是先给出一个数N,接着再给出N个数,要你从这N个数中任意选择1个或多个数,使得其和是N的倍数如果找不到这样的答案 则输出0。
答案可能有多个,但智勇任意输出一个解就行。
输出的第一行是选择元素的个数M,接着M行分别是选择的元素的值
刚开始的时候并不同为什么这一题回事抽屉原理,分析后才明白,昨晚后更有体会
实际上此题一定有解,不存在输出0的结果。
证明如下:
我们可以依次求出a[0],a[0]+a[1],a[0]+a[1]+a[2],......,a[0]+a[1]+a[2]...+a[n];
假设分别是sum[0],sum[1],sum[2],......,sum[n];
如果在某一项存在是N的倍数,则很好解,即可直接从第一项开始直接输出答案。
但如果不存在,则sum[i]%N的值必定在[1,N-1]之间,又由于有n项sum。
有抽屉原理:把多于n个的物体放到n个抽屉里,则至少有一个抽屉里有2个或2个以上的物体。
则必定有一对i,j,使得sum[i]=sum[j],其中i!=j,不妨设j>i
则(sum[j]-sum[i])%N=0,故sum[j]-sum[i]是N的倍数
则只要输出从i+1~j的所有的a的值就是答案。
代码如下:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;int mp[10000];int sum[10000];int mod[10000];int main(){ int n,i,j,num; while(scanf("%d",&n)!=EOF) { num=0; memset(mod,-1,sizeof(mod)); memset(sum,0,sizeof(sum)); for(i=0; i<n; i++) { scanf("%d",&mp[i]); num+=mp[i]; sum[i]=num; } for(i=0; i<n; i++) { if(sum[i]%n==0) { printf("%d\n",i+1); for(j=0; j<=i; j++) { printf("%d\n",mp[j]); } break; } ///这里的mod数组用来记录sum%n余数是否已经存在。 if(mod[sum[i]%n]!=-1) { printf("%d\n",i-mod[sum[i]%n]); for(j=mod[sum[i]%n]+1;j<=i;j++) printf("%d\n",mp[j]); break; } ///有新的余数时,保存此时的下标。 mod[sum[i]%n]=i; } } return 0;}
阅读全文
1 1
- Find a multiple(抽屉原理)
- POJ2356 Find a multiple 抽屉原理(鸽巢原理)
- POJ 2355 Find a multiple(组合数学-抽屉原理)
- POJ 2356 Find a multiple(组合数学-抽屉原理)
- POJ 2356 Find a multiple (抽屉原理)
- 【POJ】2356 - Find a multiple(抽屉原理 & STL)
- 【POJ】-2356-Find a multiple(抽屉原理&STL)
- poj2356——Find a multiple(抽屉原理)
- POJ 2356 Find a multiple (抽屉原理)
- POJ 2356 Find a multiple(抽屉原理)
- 【POJ 2356】Find a multiple(抽屉原理-好题)
- POJ 2356 Find a multiple [抽屉原理]
- POJ-2356 Find a multiple ----抽屉原理
- [POJ 2356]Find a multiple(抽屉原理)
- poj 2356 Find a multiple 抽屉原理
- POJ 3256 Find a multiple (抽屉原理)
- POJ 2356 Find a multiple <抽屉原理>
- poj2356 Find a multiple 抽屉原理
- Qt QProcess备份数据库,目前文件与空格
- 遇到问题----connect() failed (111: Connection refused) while connecting to upstream解决
- 深入分析Java的序列化(Serialization)
- Java 时间与日期处理
- Servlet 发生异常: Cannot call sendRedirect() after the response has been committed!
- Find a multiple(抽屉原理)
- 文章标题
- JAVA 泛型
- 【荐】Angular如何防御XSS攻击
- 支持向量机与SMO优化算法
- 怎样用最简单的html+css代码制作一颗跳动的心?
- Swift
- Redis2.8.13安装配置
- 浅谈 STL 中的空间配置器一点小小心得。