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 ...
随机推荐
- JavaScript高级编程 (2) - HTML 与 JavaScript
向HTML 页面中插入JavaScript 的主要方法,就是使用<script>元素.这个元素由Netscape 创造并在Netscape Navigator 2 中首先实现.后来,这个元 ...
- 查看当前数据库正在运行的Session
当数据库运行比较缓慢时,我们需要实时查看当前有什么Session在运行,获得信息越完整,对于分析低性能的原因越有帮助.根据之前调优的经历,简单几步分析如下: 1.通过SQL Server内置的Sp_w ...
- Discuz X3.2 网站快照被劫持的解决方法
附上另一个人的解决方法:http://www.discuz.net/thread-3549930-3-1.html 问题如下: 快照被劫持,无论怎么申诉,怎么更新快照,都无法消除此问题,第一次打开网站 ...
- OSGi 基本原理
定义 OSGi(Open Service Gateway Initiative)技术是面向Java的动态模型系统. 这个框架实现了一个优雅.完整和动态地组价模型.应用程序(称为bundle)无序重新引 ...
- LinqToDB 源码分析——处理表达式树
处理表达式树可以说是所有要实现Linq To SQL的重点,同时他也是难点.笔者看完作者在LinqToDB框架里面对于这一部分的设计之后,心里有一点不知所然.由于很多代码没有文字注解.所以笔者只能接合 ...
- QT 删除文件指定目录
bool deleteDir(const QString &dirName) { QDir directory(dirName); if (!directory.exists()) { ret ...
- StackExchange.Redis帮助类解决方案RedisRepository封装(基础配置)
本文版权归博客园和作者吴双本人共同所有,转载和爬虫,请注明原文地址.http://www.cnblogs.com/tdws/p/5815735.html 写在前面 这不是教程,分享而已,也欢迎园友们多 ...
- StringBuilder的使用
今天用到了StringBuilder来拼接查询语句,发现这个真好用,决定做个小结. 百度一个StringBuilder的定义:String 对象是不可改变的.每次使用 System.String 类中 ...
- 2016 ICPC青岛站---k题 Finding Hotels(K-D树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over ...
- ECharts的简单使用过程
网页中经常要使用图表,以前使用的是highcharts插件,现在发现echarts使用起来和highcharts差不多,但是个人感觉echarts更酷炫,以下是echarts的使用过程,其实highc ...