Find a multiple
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7192   Accepted: 3138   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.

Sample Input

5
1
2
3
4
1

Sample Output

2
2
3

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

另外,POJ3370也是与此题一样的做法,此题题解在这里

——本文原创by BlackStorm,转载请注明出处。

本文地址:http://www.cnblogs.com/BlackStorm/p/5243156.html

POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理的更多相关文章

  1. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  2. poj2356 Find a multiple(抽屉原理|鸽巢原理)

    /* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...

  3. [POJ2356]Find a multiple 题解(鸽巢原理)

    [POJ2356]Find a multiple Description -The input contains N natural (i.e. positive integer) numbers ( ...

  4. POJ 2356 Find a multiple 抽屉原理

    从POJ 2356来体会抽屉原理的妙用= =! 题意: 给你一个n,然后给你n个数,让你输出一个数或者多个数,让这些数的和能够组成n: 先输出一个数,代表有多少个数的和,然后再输出这些数: 题解: 首 ...

  5. ACM数论之旅14---抽屉原理,鸽巢原理,球盒原理(叫法不一又有什么关系呢╮(╯▽╰)╭)

    这章没有什么算法可言,单纯的你懂了原理后会不会运用(反正我基本没怎么用过 ̄ 3 ̄) 有366人,那么至少有两人同一天出生(好孩子就不要在意闰年啦( ̄▽ ̄")) 有13人,那么至少有两人同一月 ...

  6. poj 2356 Find a multiple(鸽巢原理)

    Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...

  7. POJ 2356 && POJ 3370 鸽巢原理

    POJ 2356: 题目大意: 给定n个数,希望在这n个数中找到一些数的和是n的倍数,输出任意一种数的序列,找不到则输出0 这里首先要确定这道题的解是必然存在的 利用一个 sum[i]保存前 i 个数 ...

  8. POJ2356 Find a multiple 抽屉原理(鸽巢原理)

    题意:给你N个数,从中取出任意个数的数 使得他们的和 是 N的倍数: 在鸽巢原理的介绍里面,有例题介绍:设a1,a2,a3,……am是正整数的序列,试证明至少存在正数k和l,1<=k<=l ...

  9. poj Find a multiple【鸽巢原理】

    参考:https://www.cnblogs.com/ACShiryu/archive/2011/08/09/poj2356.html 鸽巢原理??? 其实不用map但是习惯了就打的map 以下C-c ...

随机推荐

  1. .Net中的RealProxy实现AOP

    序言 这个AOP要从我们公司的一个事故说起,前段时间公司的系统突然在乌云中出现,数据被泄露的一览无余,乌云上显示是SQL注入攻击.呵,多么贴近生活的一个露洞,可谓是人尽皆知啊.然而却华丽丽的给拉我们一 ...

  2. CSS float 定位和缩放问题

    今天调试一个看起来很简单的前端问题,但却花了太多的时间,示例代码: <!DOCTYPE html> <html> <head> <title></ ...

  3. 制作自己的MVC框架(二)——启动

    自己写了个框架,取名为“PrimusPHP”,就3个文件夹,log中是日志文件,会自动生成. app中就是控制器和视图,public中是入口文件和静态资源,library中既有框架的核心类,还有工具类 ...

  4. 剖析 HTTP 协议

    HTTP 概述 HTTP 是什么? HTTP(HyperText Transfer Protocol,超文本传输协议)是WWW (World Wide Web)实现数据通信的基石. HTTP是由IET ...

  5. PyQt4入门学习笔记(一)

    PyQt4入门学习笔记(一) 一直没有找到什么好的pyqt4的教程,偶然在google上搜到一篇不错的入门文档,翻译过来,留以后再复习. 原始链接如下: http://zetcode.com/gui/ ...

  6. Linux系统修改PATH环境变量方法

    在Linux安装一些软件通常要添加路径环境变量PATH.PATH环境变量通俗的讲就是把程序的路径"备案"到系统中,这样执行这些程序时就不需要输入完整路径,直接在bash输入程序名就 ...

  7. JSON扩展类——JsonHelper

    1.引用Newtonsoft.Json库(JSON.NET). 2.复制粘贴JsonHelper吧. 源代码: using System; using System.Collections.Gener ...

  8. 在DevExpress程序中使用内置的图标构建美观的界面元素

    在我们一般的程序中,为一般的界面元素添加一定的图标展示,有助于提升界面的整体的美观.结合排版布局,以及固定场景的图标,往往给用户非常好的直观感受:统一.美观.易理解.因此在一般的程序界面中,都尽量在略 ...

  9. .net 一些开源的东东

    来自网络..版权归网络所有..Antlr ----- Website: http://www.antlr.org/ Copyright: Copyright (c) - Terence Parr Li ...

  10. C#语音朗读文本 — TTS的实现

    TTS, Text To Speech的缩写,是使用语音朗读文本的技术.目前,在国内应用较多的是排队叫号系统 Windows 平台的TTS,通常使用的是微软自带的 Speech API. Window ...