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. docker启动ubuntu的桌面环境

    一.概述 由于最近一段时间在家办公,国内服务器在阿里云,国外站点在aws.家里的移动宽带比较差,无法访问aws. 所以尝试在阿里云启动docker,找到一个lxde桌面环境的ubuntu镜像. 二.启 ...

  2. SpringBoot(六):SpringBoot中如何使用Servlet?

    第一种方法: 1.使用Servlet3的注解方式编写一个Servlet 2.在main方法的主类上添加注解: @ServletComponentScan(basePackages = "co ...

  3. MySQL深入研究--学习总结(3)

    前言 接上文,继续学习后续章节.细心的同学已经发现,我整理的并不一定是作者讲的内容,更多是结合自己的理解,加以阐述,所以建议结合原文一起理解. 第九章<普通索引和唯一索引,如何选择> 从查 ...

  4. Linux Kernel 0.12 启动简介,调试记录(Ubuntu1804, Bochs, gdb)

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文作为本人csdn blog的主站的备份.(Bl ...

  5. Redis工具收费后新的开源已出现

    作者:三十三重天 博客: zhouhuibo.club 引言 Redis工具哪家强,中国山东找蓝翔.哎呀,串台了. 众所周知,开源的最终还是收费. Reids Desktop 秉承了这一理念,苦逼的程 ...

  6. U盘重装系统:手把手教你怎么使用U盘重装系统、清除登录密码

    前言 之前讲过<不懂电脑也能自己重装系统,可视化傻瓜式一键重装系统不求人!!!>,这是针对可以正常开机的情况下直接使用浏览器功能重装系统, 那不能正常开机或者忘记密码的怎么办呢? 不慌,今 ...

  7. 计算异质性H值(运用arcgis和Python进行区域分析)

    最近需要对ecognition分割结果进行统计分析,以此来进一步判断其分割结果中的欠分割和过分割对象,在看了一篇论文后,发现了可以用一个参数H来判断每个切割对象的异质性,由于此方法需要用到arcgis ...

  8. 在C#中使用 CancellationToken 处理异步任务

    在 .NET Core 中使用异步编程已经很普遍了, 你在项目中随处可见 async 和 await,它简化了异步操作,允许开发人员,使用同步的方式编写异步代码,你会发现在大部分的异步方法中,都提供了 ...

  9. ch1_5_1统计最大最小元素的平均比较次数

    public class ch1_5_1统计最大最小元素的平均比较次数 { public static void main(String[] args) { // TODO Auto-generate ...

  10. 【Azure 应用服务】App Service 在使用GIt本地部署,上传代码的路径为/home/site/repository,而不是站点的根目录/home/site/wwwroot。 这个是因为什么?

    问题描述 App Service 在使用GIt本地部署,上传代码的路径为/home/site/repository,而不是站点的根目录/home/site/wwwroot. 这个是因为什么? 并且通过 ...