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

 
 

 
题解:
 
我们可以求出每个数的前缀和,如果有一项mod n等于0,那么直接输出它之前的所有数;
如果不存在,那么qzh[i]%n的值一定落在[1,n-1]之间,根据鸽巢原理,n个数落在n-1个地方,必定有一个地方重复,即qzh[i] % n = qzh[j] % n;
所以qzh[i]%n - qzh[j]%n = 0, 即i 到 j 之间的所有数加起来就是n的倍数;
所以直接暴力判断ok;
 

 
Code:
#include <iostream>
#include <cstdio>
#include <map>
using namespace std; int n;
int a[];
int qzh[];
map <int, int> mp; int main()
{
scanf("%d", &n);
for (register int i = ; i <= n ; i ++) scanf("%d", a + i);
for (register int i = ; i <= n ; i ++)
{
qzh[i] = qzh[i-] + a[i];
if (qzh[i] % n == )
{
cout << i << endl;
for (register int j = ; j <= i ; j ++) printf("%d\n", a[j]);
return ;
}
if (mp[qzh[i]%n]!= )
{
cout << i - mp[qzh[i]%n] << endl;
for (register int j = mp[qzh[i]%n] + ; j <= i ; j ++)
printf("%d\n", a[j]);
break;
}
mp[qzh[i]%n] = i;
}
return ;
}

[POJ2356] Find a multiple 鸽巢原理的更多相关文章

  1. [poj2356]--Find a multiple ——鸽巢原理

    题意: 给定n个数,从中选取m个数,使得\(\sum | n\).本题使用Special Judge. 题解: 既然使用special judge,我们可以直接构造答案. 首先构造在mod N剩余系下 ...

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

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

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

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

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

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

  5. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

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

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

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

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

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

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

  9. HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. WeakMap 本身释放,而 keyObject 没有释放的情况下,value 会释放吗?

    const keyObject = ['keyObject']; new WeakMap().set(keyObject, ['value']); 问题:现在 ['value'] 会被释放吗? 听说W ...

  2. charles Web界面设置

    本文参考:charles Web界面设置 Web Inerface Web界面可以让您使用Web浏览器控制查询,您可以访问 http://control.charles 的Web界面,当查询运行时,您 ...

  3. 005:CSS三大重点之三:定位

    目录 1:定位模式和边偏移 2:定位模式 静态定位 相对定位:移动位置:脱标.占位置 绝对定位:脱标.占有位置. 拼爹型 子绝父相 固定定位:脱标.占有位置. 3:定位模式转换 3:z-index 前 ...

  4. [Leetcode][动态规划] 第931题 下降路径最小和

    一.题目描述 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示 ...

  5. 心动吗?正大光明的免费使用IntelliJ IDEA商业版

    IntelliJ IDEA是广受Java开发者喜爱的工具,其商业版的价格十分昂贵,如下图: 现在有机会免费获取IntelliJ IDEA的正版License,您是否心动呢?我把自己成功申请Licens ...

  6. 基于Coravel定时任务之物联网设备数量统计

    目录 基于Coravel定时任务之物联网设备数量统计 1 应用背景 2 对比各家定时库 2.1 TaskScheduler 2.2 Fluent Scheduler 2.3 Quartz.net 2. ...

  7. 阿里云安装RocketMQ

    说明: 我的阿里云是centos 6.9 jdk 1.8.0_192-b12(安装教程参照:https://www.cnblogs.com/kingsonfu/p/9801556.html) mave ...

  8. linux 修改文件权限指令chmod

    chmod 修改一下bin目录下的.sh权限就可以了. chmod u+x *.sh 这里的u 这里指文件所有者,+x 添加可执行权限,*.sh表示所有的sh文件.

  9. Python攻破淘宝网各类反爬手段,采集淘宝网ZDB(女用)的销量!

    声明: 由于某些原因,我这里会用手机代替,其实是一样的! 环境: windows python3.6.5 模块: time selenium re 环境与模块介绍完毕后,就可以来实行我们的操作了. 第 ...

  10. .NET进阶篇-语言章-1-Generic泛型深入

    内容目录 一.概述二.泛型的好处三.泛型使用1.泛型方法2.泛型类.泛型接口四.泛型的功能1.泛型中的默认值2.约束3.协变逆变5.泛型委托4.泛型缓存五.总结 一.概述 泛型我们一定都用过,最常见的 ...