poj 2356 鸽巢原理
2016-04-19 19:56
232 查看
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
Sample Output
题目大意就是先给出一个数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
;
假设分别是sum[0],sum[1],sum[2],......,sum
如果在某一项存在是N的倍数,则很好解,即可直接从第一项开始直接输出答案
但如果不存在,则sum[i]%N的值必定在[1,N-1]之间,又由于有n项sum,有抽屉原理:
则必定有一对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的值就是答案
然后就利用这个思路就可以直接的解出该题的答案
参考链接:点击打开链接
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
题目大意就是先给出一个数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
;
假设分别是sum[0],sum[1],sum[2],......,sum
如果在某一项存在是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<cstring> #include<cmath> #include<cstdio> #include<queue> #include<map> #include<vector> #include<cstdlib> #include<algorithm> #define inf 0x3f3f3f3f #define LL long long using namespace std; int sum[10010]; int mod[10010]; int a[10010]; int main() { int n; while(cin>>n) { for(int i=1;i<=n;i++) cin>>a[i]; sum[0]=0; memset(mod,-1,sizeof(mod)); for(int i=1;i<=n;i++) { sum[i]+=sum[i-1]+a[i]; if(sum[i]%n==0) { cout<<i<<endl; for(int j=1;j<=i;j++) cout<<a[j]<<endl; break; } if(mod[sum[i]%n]!=-1) { cout<<i-mod[sum[i]%n]<<endl; for(int j=mod[sum[i]%n]+1;j<=i;j++) cout<<a[j]<<endl; break; } mod[sum[i]%n]=i; } } return 0; }
相关文章推荐
- 配置环境变量
- 如何在表单中使用Ajax
- ccc array
- 自动化Jenkins动态获取SVN路径二级目录
- 关于shell脚本source ~/.bash_profile不起作用
- Add Two Numbers
- EL与JSTL标签库
- 数据结构和算法16 之堆排序
- 《JS权威指南》学习笔记(四):对象
- 站立会议02
- HTTP调试 抓包 工具 Fiddle 简介 示例
- 模板类
- 基于Redis实现分布式锁,Redisson使用及源码分析
- Delete Node in a Linked List
- java利用容器来构建表
- zoj2334 Monkey King , 并查集,可并堆,左偏树
- 作业5:需求分析
- 第一冲刺阶段站立会议03
- Android API之android.net.wifi.ScanResult
- leetcode笔记:Integer Break