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的倍数

思路:

n个数余数分别为 1 ~ n-1 ,相当于有n-1个抽屉,n个物品

分别计算a[1] + a[2] + …… + a[k] 的和然后取余如果为零则直接输出前k个数,否则寻找余数相同的两个数,假设为i, j (i < j),则a[i+1] + . . . . + a[j] 的和一定能被n整除(原理还没想清楚)

AC代码

 1 #include<iostream>
2 #include<stdio.h>
3 #include<string.h>
4 using namespace std;
5 int a[10005];
6 int mod[10005];
7 int mark[10005];
8
9 int main()
10 {
11 int n;
12 bool flag = false;
13 cin >> n;
14 memset(mod, 0, sizeof(mod));
15 memset(mark, 0, sizeof(mark));
16 for(int i = 1; i <= n; i++)
17 {
18 cin >> a[i];
19 mod[i] = (mod[i-1] + a[i]) % n;
20 }
21
22 for(int i = 1; i <= n; i++)
23 {
24 if(mod[i] == 0)
25 {
26 flag = true;
27 cout << i << endl;
28 for(int j = 1; j <= i; j++)
29 cout << a[j] << endl;
30 break;
31 }
32 }
33
34 if(!flag)
35 {
36 for(int i = 1; i <= n; i++)
37 {
38 if(mark[mod[i]] == 0)
39 mark[mod[i]] = i;
40 else
41 {
42 cout << i -mark[mod[i]] << endl;
43 for(int j = mark[mod[i]]+1; j <= i; j++)
44 cout << a[j] << endl;
45
46 break;
47 }
48 }
49 }
50
51 return 0;
52 }

B - 抽屉 POJ - 2356 (容斥原理)的更多相关文章

  1. Find a multiple POJ - 2356 容斥原理(鸠巢原理)

    1 /* 2 这道题用到了鸠巢原理又名容斥原理,我的参考链接:https://blog.csdn.net/guoyangfan_/article/details/102559097 3 4 题意: 5 ...

  2. C - 抽屉 POJ - 3370 (容斥原理)

    Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain ...

  3. POJ 2356 Find a multiple 抽屉原理

    从POJ 2356来体会抽屉原理的妙用= =! 题意: 给你一个n,然后给你n个数,让你输出一个数或者多个数,让这些数的和能够组成n: 先输出一个数,代表有多少个数的和,然后再输出这些数: 题解: 首 ...

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

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

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

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

  6. poj 2356 (抽屉原理)

    题目链接:http://poj.org/problem?id=2356 题目大意:给你n个数,要你从n个数选出若干个数,要求这若干个数的和是n的倍数,输出选择数的个数,以及相应的数. 解题思路: 以下 ...

  7. Find a multiple POJ - 2356 (抽屉原理)

    抽屉原理: 形式一:设把n+1个元素划分至n个集合中(A1,A2,…,An),用a1,a2,…,an分别表示这n个集合对应包含的元素个数,则:至少存在某个集合Ai,其包含元素个数值ai大于或等于2. ...

  8. poj 2356 抽屉原理

    基本原理: n+1个鸽子放到n个笼子里,至少有一个笼子里有两只及其以上的鸽子.若有n个笼子,kn+1个鸽子,至少有一个笼子里面有k+1个鸽子: 题意:给定N个数,挑出一些数,他们和和是n的整数倍: 分 ...

  9. poj 2773(容斥原理)

    容斥原理入门题吧. Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9798   Accepted: 3 ...

随机推荐

  1. HDOJ-6681(离散化+线段树)

    Rikka With Cake HDOJ-6681 最终的答案为射线的交点数加一.当然,我们也可以证明.证明需要用到欧拉公式 V−E+F=2 V-E+F=2V−E+F=2 .设射线的交点共 c cc ...

  2. 让人头疼的AI bug (随想)

    虽然概念上,人工智能和机器学习不等同.但是本文提及的AI,指的是基于机器学习的AI.   一个软件产品,出了错误叫bug,bug需要修.那一个机器学习的模型,准确率在那摆着呢,大伙心知肚明是有一定的犯 ...

  3. sublime text3里 修改TAB键为缩进为四个空格

    1. 菜单栏里点击 Preferences-> Setting里面,右侧小窗口User 2. 在弹出来的文本里,添加如下两行:{ "tab_size": 4, "t ...

  4. 我与FreeBSD 的故事之二

    那些人的丑恶嘴脸使我发笑,我愈发远离所谓的社区与论坛.电视剧<武林外传>说的好:有人的地方就有江湖,江湖从未走远,从未改变.社区中的冲突很少是技术层面的,按照老话说睿智的人很少发表自己的见 ...

  5. Apache配置 10. 访问控制-禁止解析PHP

    (1)简述 对于使用PHP语言编写的网站,有一些目录是有需求上传文件的.如果网站代码有漏洞,让黑客上传了一个用PHP写的木马,由于网站可以执行PHP程序,最终会让黑客拿到服务器权限. 为了避免这种情况 ...

  6. 解析分布式应用框架Ray架构源码

    摘要:Ray的定位是分布式应用框架,主要目标是使能分布式应用的开发和运行. Ray是UC Berkeley大学 RISE lab(前AMP lab) 2017年12月 开源的新一代分布式应用框架(刚发 ...

  7. unbutu的dpkg被中断的解决办法

    直接sudo apt update进行重新配置就行

  8. 2018年11月16日SQL Server实验内容(触发器实验)

    --注意:先把studentmanager数据库中的所有表用select into命令复制一份, --然后用复制后新表完成下面的实验,同时,对每个触发器都要进行验证. select *into dep ...

  9. APK瘦身属性——android:extractNativeLibs

    先描述一下结论: android:extractNativeLibs = true时,gradle打包时会对工程中的so库进行压缩,最终生成apk包的体积会减小. 但用户在手机端进行apk安装时,系统 ...

  10. Fork/Join 框架

    本文部分摘自<Java 并发编程的艺术> Fork/Join 框架概述 Fork/Join 框架是 Java7 提供的一个用于并行执行任务的框架,是把一个大任务分割成若干个小任务,最终汇总 ...