POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
|
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 Sample Output 2 Source |
||||||||||
题目大意:
给定$N$个自然数,试找出其中的若干个数,它们的算术和是$N$的整数倍。
基本思路:
1、若存在$sum\%N==0$,则$sum$一定是$N$的倍数(即$sum==kN,\ k\in [1, \infty]$)。
2、若$sum[i]\equiv sum[j]\ mod\ N$,则$(sum[j]-sum[i])\%N==0$,即$sum[j]-sum[i]$是$N$的倍数。
3、(鸽巢原理)将$N$个物体放入$N-1$个盒子里,则一定至少有$1$个盒子放了$2$个以上的物体。这里我们将$N$个自然数分别记为$arr[i],\ i\in [1,\ N]$,令$sum[i]=(\sum\limits^{N}_{i=1}arr[i])\%N,\ i\in [1,\ N]$,而$sum[i]$的取值范围为0 ~ N-1,因此:A.必然存在$i,\ j\in [1,\ N]$使得sum[i]==sum[j];B.可以存在$i\in [1,\ N]$有sum[i]==0。
4、建立一个标记数组$sgn[]$,标记$sum[]$的值,这样可以在读入预处理时找到相等的两个$sum$。同时根据第3点,我们得知答案必然存在,不会出现没有答案输出0的情况。
5、由于存在sum[i]==0的情况,所以要初始化sgn[0]=0。
代码:
#include <stdio.h>
#include <string.h> int N, arr[], sum[], sgn[]; int main() {
int l=, r=-;
memset(sgn, 0xFF, sizeof(sgn)); sgn[]=;
scanf("%d", &N);
for(int i=; i<=N; i++) {
scanf("%d", arr+i);
sum[i]=(sum[i-]+arr[i])%N;
if(!~sgn[sum[i]])
sgn[sum[i]]=i;
else {
l=sgn[sum[i]];
r=i;
}
}
printf("%d\n", r-l);
for(int i=l+; i<=r; i++)
printf("%d\n", arr[i]);
return ;
}
POJ 2356
本文地址:http://www.cnblogs.com/BlackStorm/p/5243156.html
POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理的更多相关文章
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- poj2356 Find a multiple(抽屉原理|鸽巢原理)
/* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...
- [POJ2356]Find a multiple 题解(鸽巢原理)
[POJ2356]Find a multiple Description -The input contains N natural (i.e. positive integer) numbers ( ...
- POJ 2356 Find a multiple 抽屉原理
从POJ 2356来体会抽屉原理的妙用= =! 题意: 给你一个n,然后给你n个数,让你输出一个数或者多个数,让这些数的和能够组成n: 先输出一个数,代表有多少个数的和,然后再输出这些数: 题解: 首 ...
- ACM数论之旅14---抽屉原理,鸽巢原理,球盒原理(叫法不一又有什么关系呢╮(╯▽╰)╭)
这章没有什么算法可言,单纯的你懂了原理后会不会运用(反正我基本没怎么用过 ̄ 3 ̄) 有366人,那么至少有两人同一天出生(好孩子就不要在意闰年啦( ̄▽ ̄")) 有13人,那么至少有两人同一月 ...
- poj 2356 Find a multiple(鸽巢原理)
Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...
- POJ 2356 && POJ 3370 鸽巢原理
POJ 2356: 题目大意: 给定n个数,希望在这n个数中找到一些数的和是n的倍数,输出任意一种数的序列,找不到则输出0 这里首先要确定这道题的解是必然存在的 利用一个 sum[i]保存前 i 个数 ...
- POJ2356 Find a multiple 抽屉原理(鸽巢原理)
题意:给你N个数,从中取出任意个数的数 使得他们的和 是 N的倍数: 在鸽巢原理的介绍里面,有例题介绍:设a1,a2,a3,……am是正整数的序列,试证明至少存在正数k和l,1<=k<=l ...
- poj Find a multiple【鸽巢原理】
参考:https://www.cnblogs.com/ACShiryu/archive/2011/08/09/poj2356.html 鸽巢原理??? 其实不用map但是习惯了就打的map 以下C-c ...
随机推荐
- 在ASP.NET中基于Owin OAuth使用Client Credentials Grant授权发放Token
OAuth真是一个复杂的东东,即使你把OAuth规范倒背如流,在具体实现时也会无从下手.因此,Microsoft.Owin.Security.OAuth应运而生(它的实现代码在Katana项目中),帮 ...
- socket编程为什么需要htons(), ntohl(), ntohs(),htons() 函数
在C/C++写网络程序的时候,往往会遇到字节的网络顺序和主机顺序的问题.这是就可能用到htons(), ntohl(), ntohs(),htons()这4个函数. 网络字节顺序与本地字节顺序之间的转 ...
- Android之自定义View的实现
对于学习Android开发的小童鞋对于自定义View一定不会陌生,相信大家对它是又爱又恨,爱它可以跟随我们的心意设计出漂亮的效果:恨它想要完全流畅掌握,需要一定的功夫.对于初学者来说确实很不容易,网上 ...
- ASP.NET Core的配置(5):配置的同步[ 实例篇]
ConfigurationBuilder在生成以Configuration对象的时候会利用注册其中的ConfigurationProvider加载原始的配置数据,那么一旦配置源中的数据发生变化,应用程 ...
- Linux下的磁盘分割和文件系统
一.各硬件装置在Linux下的文件名 1.IDE硬盘机 在Linux内的文件名: /dev/hd[a-d] (a-d 刚好是四个这个是有原因的具体如下) 解释:以 IDE 接口来说,由于一个 IDE ...
- 父页面操作iframe子页面的安全漏洞及跨域限制问题
一.父子交互的跨域限制 同域情况下,父页面和子页面可以通过iframe.contentDocument或者parent.document来交互(彼此做DOM操作等,如父页面往子页面注入css). 跨域 ...
- 安卓Design包之TabLayout控件的简单使用
Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的Android Design Support Library,在这个supp ...
- C#开机自动启动程序代码
新建一个winform拖一个checkbox进来.. 然后设置它的changed事件. 已经测试过,可以直接复制使用. private void checkBox1_CheckedChanged(ob ...
- SSH中Action的单例与多例
Structs2中的Bean默认的是单例,在整个程序运行期间,每个Bean只有一个实例,只要程序在运行,这个实例就一直存在. 对于Action来说,单例就容易出问题.如果客户端每次提交的参数都是一样的 ...
- Interview website
https://www.interviewcake.com http://www.leetcode.com