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

题目大意是: 输入n个数,从中找出数字,它们加起来之和是n的倍数,并将它们输出。 分析:之和是n的倍数,就是对n求余是0。 通常大家会想把这些数随机组合,得到的和存放在一个数组里,但遇到很多数,这就不好办了。 由于问题只要找出一组即可,所以我们就是要找到一组。 所以我建立个抽屉模型,
为什么可以用抽屉模型?有n个数,这些数的连续和分别对n取余得到的余数范围是0~n-1, 不相同的余数个数不超过n,当个数为n时, 那其中肯定有一组数的和满足条件,若小于n,这当中至少有两组数的余数相同,举个
例子,1,3,5,9,2 ,5个数, 我把它们连续相加变成, 1,4, 9, 18, 20, 仍然是5个数。 使他们一一对5取余,得到 1, 4, 4, 3, 0。 我把余数当作抽屉,和作为物体, 这里有5个物体,4个抽屉,
第一组数是1,3,第二组数是1,3,5。 用后者减前者便是答案。
#include <stdio.h>
#define M 10001
int a[M], mod[M];
int main()
{
int n, i, sum, begin, end;
while(scanf("%d", &n) != EOF){ sum = ;
for(i = ; i <= n; i++){ scanf("%d", &a[i]); }
for(i = ; i <= n; i++){ sum = (sum + a[i]) % n;
if(!sum)
{
begin = ;
end = i;
break;
}
else if(mod[sum] > )
{
begin = mod[sum] + ;
end = i;
break;
}
mod[sum] = i; }
printf("%d\n", end - begin + );
for(i = begin; i <= end; i++){ printf("%d\n", a[i]); }
putchar('\n'); }
return ;
}

find a multiple的更多相关文章

  1. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

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

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

  3. [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用

    The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...

  4. SharePoint "System.Data.SqlClient.SqlException (0x80131904): Parameter '@someColumn' was supplied multiple times.“

    最近在处理SharePoint Office365的相关开发的时候发现了这样一个奇怪的现象: 无法通过API更新Editor field,只要已更新就会throw Exception,由于是Offic ...

  5. 2012Chhengdu K - Yet Another Multiple Problem

    K - Yet Another Multiple Problem Time Limit:20000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  6. JAVA实现AES 解密报错Input length must be multiple of 16 when decrypting with padded cipher

    加密代码 /**解密 * @param content 待解密内容 * @param password 解密密钥 * @return */ public static byte[] decrypt(b ...

  7. scala - multiple overloaded alternatives of method bar define default arguments

    同名同位置默认参数不能overload def bar(i:Int,s:String="a"){} def bar(i:String,s:String="b") ...

  8. 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?

    复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...

  9. 多文档上传(upload multiple documents)功能不能使用怎么办?

    问题描述: 在SharePoint 2010的文档库里选择documents标签,然后选择upload document下拉菜单,你会发现upload multiple documents那个按钮是灰 ...

  10. OPEN CASCADE Multiple Variable Function

    OPEN CASCADE Multiple Variable Function eryar@163.com Abstract. Multiple variable function with grad ...

随机推荐

  1. LoadRunner 常用C语言函数使用

    strlen:获取字符串的长度 char str[20]="容我想想"; int len; len = strlen(str); lr_output_message("s ...

  2. C语言补码作用

    补码主要是为了cpu运算器在进行减法运算时避免借位而设立的. 在早期,cpu中的运算器部分,只要实现一个加法器就可以完成四由算术运算. 因为计算机中的数值编码是有限位数的,所以减法实际上相当于加上减数 ...

  3. iOS 关于iOS开发中的delegate

    有A.B两个对象,A要完成某件事,想让B帮它做. 这时候,A中就要实例化一个B的对象b,A还要在头文件中声明协议,然后在B中实现协议中对应的方法. 这时候再把A的delegate设置为b,在需要的地方 ...

  4. ACdream1157 Segments(CDQ分治 + 线段树)

    题目这么说的: 进行如下3种类型操作:1)D L R(1 <= L <= R <= 1000000000) 增加一条线段[L,R]2)C i (1-base) 删除第i条增加的线段, ...

  5. React的第一步

    首先了解React中所牵扯到的几个重要的概念 什么是React? 是Facebook的开发团队开发出来的一个用于构建用户界面个js库,最近才开源出来公布于世,它的初衷是用于创建“独立的视图组件”,一个 ...

  6. ural 1251. Cemetery Manager

    1251. Cemetery Manager Time limit: 1.0 secondMemory limit: 64 MB There is a tradition at the USU cha ...

  7. BZOJ3615 : MSS

    将所有点按横坐标排序,那么最长上升子序列和最长下降子序列里必有一个长度不小于$\sqrt{n}$. 因为如果最长上升子序列很短,那么根据Dilworth定理,它的反链长度与它成反比. 因此可以将$n$ ...

  8. HDU5823 : color II

    每种颜色的点集肯定是独立集,因此可以通过$O(2^n)$枚举每个集合判断出每个集合是否只需要一种颜色即可染色. 设$f[i][S]$表示$i$种颜色覆盖$S$这个集合的方案数,假定两个集合可以相交,那 ...

  9. 如何终止java线程

    http://blog.csdn.net/anhuidelinger/article/details/11746365 终止线程的三种方法 有三种方法可以使终止线程. 1.  使用退出标志,使线程正常 ...

  10. Coder-Strike 2014 - Finals (online edition, Div. 2) A. Pasha and Hamsters

    水题 #include <iostream> #include <vector> #include <algorithm> using namespace std; ...