Description

The input contains N natural (i.e. positive integer) numbers ( N <=  ). Each of that numbers is not greater than . 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 (  <= 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 . 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


Sample Output


Source

 

题意:有n个数,求是否存在一些数的和是n的倍数。若存在,输出即可。不存在,输出0.

思路:鸽巢原理的题目,组合数学课本上的原题。可以把和求出来,然后对n取余,因为有n个和,对n取余,如果余数中没有出现0,根据鸽巢原理,一定有两个数的余数相同,两个和想减就是n的倍数。如果余数出现0,自然就是n的倍数。也就是说,n个数中一定存在一些数的和是n的倍数。求余输出即可。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 10006
#define inf 1e12
int n;
int sum[N];
int vis[N];
int a[N];
int tmp[N];
int main()
{
while(scanf("%d",&n)==){
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++){
//int x;
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
}
memset(vis,,sizeof(vis));
memset(tmp,,sizeof(tmp));
for(int i=;i<=n;i++){
int x=sum[i]%n;
if(vis[x]){
int y=tmp[x];
printf("%d\n",i-y);
for(int j=y+;j<=i;j++){
printf("%d\n",a[j]);
}
break; }
if(x==){
printf("%d\n",i);
for(int j=;j<=i;j++){
printf("%d\n",a[j]);
}
break;
}
vis[x]=;
tmp[x]=i;
} }
return ;
}

poj 2356 Find a multiple(鸽巢原理)的更多相关文章

  1. POJ 3370 Halloween treats( 鸽巢原理简单题 )

    链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...

  2. [POJ2356] Find a multiple 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8776   Accepted: 3791   ...

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

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

  4. poj 3370 Halloween treats(鸽巢原理)

    Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...

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

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

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

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

  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. [POJ2356]Find a multiple 题解(鸽巢原理)

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

随机推荐

  1. 提高xshell使用效率

    1.快速命令集. 2.鼠标复制粘贴设置. 3.配色方案. 4.esc切换到英文输入. 设置入口:

  2. Direct3D 顶点缓存

    今天我们来学习下Direct3D的顶点和顶点缓存,首先我们需要在场景中绘制一些物体,物体都是由多个三角形组成,每一个三角形由三个顶点组成,我们来看下面一个NPC的模型 左图:正常的模型         ...

  3. mycat实例(1)

    2016二月 22 置原 MyCat - 使用篇(1) 分类:数据库分库分表(Mycat等) (1126) (1) 数据库路由中间件MyCat - 使用篇(1) 基本概念 直接介绍概念太枯燥了,还是拿 ...

  4. java与.net比较学习系列(2) 基础语言要素

    这一篇从最基础的开始对比总结,说起基础语言要素,故名思义,就是学习语言的基础,主要内容包括标识符,关键字和注释.我想从以下几点进行总结,有区别的地方有都使用红色粗体字进行了总结. 1,标识符 2,关键 ...

  5. FlashbackQuery:SCN与timestamp示例

    Flashback QueryFlashback 是ORACLE 自9i 就开始提供的一项特性,在9i 中利用oracle 查询多版本一致的特点,实现从回滚段中读取表一定时间内操作过的数据,可用来进行 ...

  6. dtree基础

    最近用到了dtree来建立树,纠结过好久后,终于有了些门道,下面的总结希望对咪咪们有些帮助: dtree用来建立静态树或者动态树都是很方便的,老外给提供了整个的JS,然后我们只是操心这个树中存放的元素 ...

  7. 真机调试以及“Could not find Developer Disk Image”问题解决方案

    真机测试步骤 1.运行Xcode,Xcode打开后,点左上角菜单'Xcode',点'Preferences'. 2.在打开的窗口中,点'Accounts',切换到账号页,然后点下面的'+'号,在弹出菜 ...

  8. OD调试1--第一个win32程序

    OD调试一:第一个Win32程序的修改 在软件开发的过程中,程序员会使用一些调试工具,以便高效地找出软件中存在的错误.而在逆向分析领域,分析者也会利用相关的调试工具来分析软件的行为并验证分析结果.由于 ...

  9. calc()函数的使用

    calc()函数算是css中的一个另类了,一般来说css都是直接确定的样式,而calc()函数却是可以动态计算,这和css静态的概念有些区别,但这并不妨碍其优秀的性能. 什么是calc()? calc ...

  10. CString常用操作

    ①.CString 类对象的初始化: CString str; CString str1(_T("abc")); CString str2 = _T("defg" ...