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 ...
随机推荐
- 字符串转化为Json格式的数据--(记录四)
var pName = $(".productName").html(); var pPrice = $(".productPrice").html(); // ...
- Bagging与随机森林算法原理小结
在集成学习原理小结中,我们讲到了集成学习有两个流派,一个是boosting派系,它的特点是各个弱学习器之间有依赖关系.另一种是bagging流派,它的特点是各个弱学习器之间没有依赖关系,可以并行拟合. ...
- 如何在Zabbix上安装MySQL监控插件PMP
PMP,全称是Percona Monitoring Plugins,是Percona公司为MySQL监控写的插件.支持Nagios,Cacti.从PMP 1.1开始,支持Zabbix. 下面,看看如何 ...
- ASP.NET MVC RouteExistingFiles
遇到这样一个问题:项目是 MVC,但也包含 WebForm 的页面,RouteConfig 中设置了这样一个路由: routes.MapRoute( name: "SubjectIndex& ...
- 基於tiny4412的Linux內核移植--- 中斷和GPIO學習(1)
作者 彭東林 pengdonglin137@163.com 平臺 tiny4412 ADK Linux-4.4.4 u-boot使用的U-Boot 2010.12,是友善自帶的,爲支持設備樹和uIma ...
- Vertica 6.1不完全恢复启动到LGE方法
环境:RHEL6.2 + Vertica 6.1.3-7 确定所有节点的vertica进程都停掉(包括agent和python),如果有运行的,停止它或者杀掉它. 确定所有节点的spread进程都正常 ...
- docker对数据卷进行还原操作
转载请注明出处 数据卷容器备份数据后,备份数据查看 http://www.cnblogs.com/zhuxiaojie/p/5947138.html 我们可能要把这个备份的数据,还原到另一台的do ...
- Python爬虫小白入门(二)requests库
一.前言 为什么要先说Requests库呢,因为这是个功能很强大的网络请求库,可以实现跟浏览器一样发送各种HTTP请求来获取网站的数据.网络上的模块.库.包指的都是同一种东西,所以后文中可能会在不同地 ...
- 『.NET Core CLI工具文档』(七)dotnet-new
说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-new 翻译:dotnet-new 名称 dotnet-new -- 创建一个新的 .NET Core 项 ...
- 脚本化CSS类-HTML5 classList属性
HTML元素可以有多个CSS类名,class属性保存了一个用空格隔开的类名列表.标识符class在JavaScript中是保留字,所以在JavaScript中可以用className. //如下代码设 ...