poj2356Find a multiple 抽屉原理
来源:互联网 发布:python 杨辉三角 编辑:程序博客网 时间:2024/06/04 19:27
Find a multiple
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5409 Accepted: 2348 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.
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
512341
Sample Output
223
题目大意就是先给出一个数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<iostream>#include<stdio.h>#include<cstring>#include<string>#include<cstdlib>#include<stack>#include<algorithm>using namespace std;const int LEN=10005;int arr[LEN];int sum[LEN], mod[LEN];//int path[LEN][LEN], vis[LEN], dis[LEN];int main(){ int n, i, j; while( scanf("%d", &n)!=EOF ) { scanf("%d", &arr[0]); sum[0]=0; sum[1]=arr[0]; memset(mod, -1, sizeof(mod)); for(i=1; i<n; i++) { scanf("%d", &arr[i]); } for(i=0; i<n; i++) { sum[i+1]=sum[i]+arr[i]; if( sum[i+1]%n==0 ) { cout<<i+1<<endl; for(j=0; j<=i; j++) cout<<arr[j]<<endl; break; } if( mod[sum[i+1]%n]!=-1 ) { cout<<i-mod[sum[i+1]%n]<<endl; for(j=mod[sum[i+1]%n]+1; j<=i; j++) cout<<arr[j]<<endl; break; } mod[sum[i+1]%n]=i; } } return 0;}
0 0
- poj2356Find a multiple 抽屉原理
- poj2356Find a multiple【抽屉原理】
- poj2356Find a multiple(鸽笼原理)
- poj2356Find 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 抽屉原理
- poj 2356 Find a multiple【抽屉原理】
- 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(DFS,抽屉原理)
- 中国设立12月13日为南京大屠杀死难者国家公祭日
- document.domain 跨域
- 判断字符串回文的简单方法
- 微软公布了Visual Studio 2013首个更新
- vs2012加载资源都失败的时候
- poj2356Find a multiple 抽屉原理
- c++ struct union
- 【转】项目面试-如何介绍自己的项目
- 使用remote shell来执行command
- Service Manger的初始化分析
- xcode 常用使用技巧总结
- MYSQL数据库索引类型
- 最长单调递增子序列( O(nlgn) )
- C++:关于构造函数