Find a multiple
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6651   Accepted: 2910   Special Judge

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 分析:当不存在从下标0开始的某一段数字对n取余等于0的时候,需要找一个yu[i]和yu[j]相等,采用类似哈希的方式。

代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define N 10000+100 using namespace std; int a[N];
int sum[N];
int yu[N]; struct node
{
bool k;
int pos; }q[N]; int main()
{
int n;
int i, j;
int left, right; while(~scanf("%d", &n))
{
bool flag1=false;
bool flag2=false; for(i=0; i<n; i++){
scanf("%d", &a[i] );
if(i==0) sum[i]=a[i];
else sum[i]=sum[i-1]+a[i];
}
left=0;
memset(q, 0, sizeof(q)); for(i=0; i<n; i++){
yu[i]=sum[i]%n;
if(yu[i]==0){
flag1=true; right=i; break;
}
else{
if(q[yu[i]].k ){
flag2=true;
left=q[yu[i]].pos; right=i; break;
}else{
q[yu[i]].k=true; q[yu[i]].pos=i;
}
}
}
if(flag1){
printf("%d\n", right+1 );
for(i=0; i<=right; i++){
printf("%d\n", a[i] );
}
}
else if(flag2){
printf("%d\n", right-left );
for(i=left+1; i<=right; i++){
printf("%d\n", a[i] );
}
}
}
return 0;
}

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 Find a multiple(鸽巢原理)

    Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...

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

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

  8. poj Find a multiple【鸽巢原理】

    参考:https://www.cnblogs.com/ACShiryu/archive/2011/08/09/poj2356.html 鸽巢原理??? 其实不用map但是习惯了就打的map 以下C-c ...

  9. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

随机推荐

  1. SpringSecurity学习一----------最简单的权限控制系统

    © 版权声明:本文为博主原创文章,转载请注明出处 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  2. objective-C中的扩展方法与partial class

     在c#中要扩展一个现有类非常easy,比方这样: ? 1 2 3 4 5 6 7 public static class Utils {     public static void PrintTo ...

  3. Sphinx初探之安装

    在Centos or redhat 安装Sphinx .首先安装依赖包 $ yum install postgresql-libs unixODBC .安装软件 $ rpm -Uhv sphinx-- ...

  4. 深入了解Erlang 垃圾回收机制以及其重要性(转)

    声明:本片文章是由Hackernews上的[Erlang Garbage Collection Details and Why ItMatters][1]编译而来,本着学习和研究的态度,进行的编译,转 ...

  5. VS使用WEB DEPLOY发布

    背景是这样的,公司有两台服务器,平时一台备用,另一台做为主生产机器.当有大量补丁或者安装什么东西需要重启的时候,交其中一台直接关掉IIS,然后重启即可,此时另一台负责处理用户请求. 之前一台服务器一个 ...

  6. HDU 5901 Count primes (2016 acm 沈阳网络赛)

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5901 题意:输入n,输出n以内质数个数 模板题,模板我看不懂,只是存代码用. 官方题解链接:https ...

  7. mybatis 视频总结

    [说明]mabatis卡住了,理解的不深,配置文件的格式太多看不懂(除了连接数据库的部分),听说还可以和log4j集成,怎么个方法 一:今日完成 1)一些语言细节和操作细节 比如在servlet里面操 ...

  8. python 深复制与浅复制------copy模块

    模块解读: 浅复制: x = copy.copy(y)深复制: x = copy.deepcopy(y)(注:模块特有的异常,copy.Error) 深copy与浅copy的差别主要体现在当有混合对象 ...

  9. SecureCRT的Home+End+Del键映射

    在securecrt界面:工具 → 键映射编辑器,在弹出的键盘中: 1.点击“home”,会弹出一个窗口,在“发送字符串”中输入:\033[1~ 2.点击“end”,会弹出一个窗口,在“发送字符串”中 ...

  10. 大数据学习系列(1)-- linux之文件系统结构介绍

    1./ 根目录 --------- 所有目录挂在其下 2./boot --------- 存放Ubuntu内核和系统启动文件.系统启动时这些文件先被装载. 3./etc --------- 系统的配置 ...