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. git的几个小技巧

    git的几个小技巧 分享git的几个小技巧,后面会根据使用补充.目前包括git撤销本地修改.git回退到前n个版本.git多用户提交冲突解决.git 命令简化.欢迎大家补充^_* 1.git撤销本地修 ...

  2. 基于GitHub Issues的评论系统--gitment

    文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. ![file](https://img2018.cnblogs.com/blog/830272/201909/ ...

  3. 【pymongo.errors】Cursor not found

    pymongo.errors.CursorNotFound: Cursor not found 故事背景:先从数据库中取得所有数据 db['test'].find(),然后对结果进行for循环,但是当 ...

  4. HTML文档简介

    HTML简介 HTML标签 html文档标签: html源代码就好像word文档,有特殊的语法结构定义自己的功能. html文档标签 html标签,其下由两个主要节点标签head.body. head ...

  5. vim编辑python脚本时Tab补全

    所属分类:成长之路 使用Linux写python脚本的时候,初期最痛苦的是什么?当然是各种库的不熟悉,知道了库,里面的方法还要挨个看,挨个记. 所以这时候,很多小伙伴使用了ipython,最强大的功能 ...

  6. Mybatis源码解析,一步一步从浅入深(三):实例化xml配置解析器(XMLConfigBuilder)

    在上一篇文章:Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码 ,中我们看到 代码:XMLConfigBuilder parser = new XMLConfigBuilder(read ...

  7. 2018年蓝桥杯java b组第八题

    标题:日志统计 小明维护着一个程序员论坛.现在他收集了一份"点赞"日志,日志共有N行.其中每一行的格式是: ts id 表示在ts时刻编号id的帖子收到一个"赞" ...

  8. Java 基础篇之编程基础

    基本数据类型 java 是强类型语言,在 java 中存储的数据都是有类型的,而且必须在编译时就确定其类型. 基本数据类型变量存储的是数据本身,而引用类型变量存的是数据的空间地址. 基本类型转换 自动 ...

  9. linux 的vi/vim消除查找到的高亮字符串

    方法如下: 在Vi里面如果要搜索某个关键字,只要键入/xxx就可以了,比如,要搜索一个函数,就键入/snprintf 然后回车,一个文件中,所有出现这个字样的地方都会被高亮显示.按n键,就可以自动把光 ...

  10. 只要听说过电脑的人都能看懂的网上pdf全书获取项目

    作者:周奇 最近我要获取<概统>的教材自学防挂科(线代已死),于是我看到 htt链ps:/链/max链.book接118接.com接/html/2018/0407/160495927.sh ...