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. Winforn中使用代码动态生成控件

    场景 有时候需要根据配置文件在窗体中使用代码动态生成控件. 比如读取xml配置文件中的节点数量,然后在窗体中生成指定数量的RadioGroup控件. 实现 新建一个窗体,在窗体的加载完之后的事件中 p ...

  2. eclipse下mybatis-generator-config插件

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...

  3. nodejs实现聊天机器人

    技术栈 服务端: koa.koa-route.koa-websocket.request. 客户端: html.css.js.websocket. 远程聊天API: http://api.qingyu ...

  4. IO流的工具类

    1.需要先导入jar包: FilenameUtils import org.apache.commons.io.FilenameUtils; public class FilenameUtilesDe ...

  5. 即时聊天APP(四) - 联系人和会话

    联系人和会话界面使用的是RecyclerView进行滑动显示,并将好友列表存储至数据库,以供下次登录时使用,RecyclerView在后面我会详细介绍,这里略过. 联系人初始化时读取数据库并展示: / ...

  6. 想研究BERT模型?先看看这篇文章吧!

    最近,笔者想研究BERT模型,然而发现想弄懂BERT模型,还得先了解Transformer. 本文尽量贴合Transformer的原论文,但考虑到要易于理解,所以并非逐句翻译,而是根据笔者的个人理解进 ...

  7. MySQL之增删改查之

    MySQL之增删改查   前言:以下是MySQL最基本的增删改查语句,很多IT工作者都必须要会的命令,也是IT行业面试最常考的知识点,由于是入门级基础命令,所有所有操作都建立在单表上,未涉及多表操作. ...

  8. 品Spring:注解之王@Configuration和它的一众“小弟们”

    其实对Spring的了解达到一定程度后,你就会发现,无论是使用Spring框架开发的应用,还是Spring框架本身的开发都是围绕着注解构建起来的. 空口无凭,那就说个最普通的例子吧. 在Spring中 ...

  9. Fresco添加HTTP请求头

    项目中用Fresco来管理图片由于服务器图片有不同的版本需要根据客户端的屏幕密度来选择不同的图片共享一份用OkHttp下载图片并添加HTTP头代码. public class OkHttpNetwor ...

  10. Nginx+PHP7.3.9 Docker镜像制作

    最近因项目需要制作了多个版本的php docker镜像,制作过程可谓是一波三折,因基于yum的方式安装php的方式在安装扩展插件时很不方便,不容易找到插件对应的yum源,所以PHP在docker镜像中 ...