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. Rocket broker启动失败?

    安装 Rocket 时, 执行 nohup sh bin/mqbroker -n localhost:9876 & 启动 broker 失败 更改其内存试试 在下面目录下 : cd distr ...

  2. 后端程序员之路 4、一种monitor的做法

    record_t包含_sum._count._time_stamp._max._min最基础的一条记录,可以用来记录最大值.最小值.计数.总和metric_t含有RECORD_NUM(6)份recor ...

  3. SpringMVC-02 第一个SpringMVC程序

    SpringMVC-02 第一个SpringMVC程序 第一个SpringMVC程序 配置版 新建一个Moudle , springmvc-02-hello,确定依赖导入进去了 1.配置web.xml ...

  4. 字符串匹配-BF算法和KMP算法

    声明:图片及内容基于https://www.bilibili.com/video/av95949609 BF算法 原理分析 Brute Force 暴力算法 用来在主串中查找模式串是否存以及出现位置 ...

  5. 题解 洛谷P1990 覆盖墙壁

    DP康复训练题 原题:洛谷P1990 核心:递推/DP 题源应该是铺地砖,所以采用一摸一样的思路,只是有两种不同的方块 我们先用最最简单的方式尝试一下枚举当最后一行被填满的情况: 1.如果我们只用第一 ...

  6. 深入理解Java并发框架AQS系列(一):线程

    深入理解Java并发框架AQS系列(一):线程 深入理解Java并发框架AQS系列(二):AQS框架简介及锁概念 一.概述 1.1.前言 重剑无锋,大巧不工 读j.u.c包下的源码,永远无法绕开的经典 ...

  7. java实现回溯算法

    最近有在leetcode上面做算法题,已经遇到了两道回溯算法的题目,感觉一点思路都没有,现决定将java如何实现回溯算法做一次总结. 什么叫做回溯算法 (摘抄于百度百科) 回溯算法实际上一个类似枚举的 ...

  8. IPFS挖矿的成本有哪些?

    IPFS作为区块链新贵,近来风头一时无量.截止3月9日,Filecoin以257亿的流通市值超越门罗币,稳居区块链流通排行榜. 无论什么投资,其门槛一定在成本.今天就和大家细说投资市面上常见实体矿机的 ...

  9. P1223_排队接水(JAVA语言)

    思路 根据短作业优先平均等待时间最短的常识(默默感叹一句操作系统没白学),将Ti从小到大排序后,计算平均等待时间输出 //水题 题目描述 有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请 ...

  10. springboot源码解析-管中窥豹系列之BeanDefine如何加载(十三)

    一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...