[POJ2356]Find a multiple

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.

Solution

1.题目大意即为求n个数中区间和被n整除的第一个区间[l,r];

2.由鸽巢原理知,n个不同的数会造成n个不同的前缀和,那么至少有两个前缀和关于n是同余的,我们只需找出第一个出现两次的模后前缀和即可;

3.打一个地址标记,记录模值的第一次出现地址,当第二次出现同一模后前缀和时,使l=第一次出现地址,r=第二次出现地址,区间[l,r]即为所求;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
int i,j,l=0,r,n,t[10001]={},a[10001]={},sum[10001]={};
scanf("%d",&n);
memset(t,-1,sizeof(t));
t[0]=0;
for(i=1;i<=n;++i){
scanf("%d",&a[i]);
sum[i]=(sum[i-1]+a[i])%n;
if(t[sum[i]]!=-1){
l=t[sum[i]];
r=i;
break;
}
else t[sum[i]]=i;//zzh大佬的计数方法;
}
printf("%d\n",r-l);
for(i=l+1;i<=r;++i)printf("%d\n",a[i]);
return 0;
}

有关鸽巢原理可以参考我的博客:http://www.cnblogs.com/COLIN-LIGHTNING/p/8439555.html

[POJ2356]Find a multiple 题解(鸽巢原理)的更多相关文章

  1. poj 2356 Find a multiple(鸽巢原理)

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

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

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

  3. poj 2356 Find a multiple【鸽巢原理 模板应用】

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

  4. [POJ2356] Find a multiple 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8776   Accepted: 3791   ...

  5. poj2356 Find a multiple(抽屉原理|鸽巢原理)

    /* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...

  6. [poj2356]--Find a multiple ——鸽巢原理

    题意: 给定n个数,从中选取m个数,使得\(\sum | n\).本题使用Special Judge. 题解: 既然使用special judge,我们可以直接构造答案. 首先构造在mod N剩余系下 ...

  7. POJ2356 Find a multiple 抽屉原理(鸽巢原理)

    题意:给你N个数,从中取出任意个数的数 使得他们的和 是 N的倍数: 在鸽巢原理的介绍里面,有例题介绍:设a1,a2,a3,……am是正整数的序列,试证明至少存在正数k和l,1<=k<=l ...

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

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

  9. [POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)

    [POJ3370]&[HDU1808]Halloween treats Description -Every year there is the same problem at Hallowe ...

随机推荐

  1. tftp 简要使用说明

    yum 安装:tftp    tftp-server (2)启动tftp   CentOS 6 service xinetd restart chkconfig tftp on     CentOS ...

  2. 小程序获取access_token

    <?php //小程序appid $appid = 'wx79d7c348d19f010c'; //小程序 APPSecret 密钥 $appsecret = 'd624aca86d0350ee ...

  3. 分布式架构核心RPC原理

    在应用的迭代演进过程中,随着系统访问量提高,业务复杂度提高,代码复杂度提高,应用逐渐从单体式架构向面向服务的分布式架构转变.RPC(Remote Procedure Call Protocol远程过程 ...

  4. Android Studio 修改 包名

    Android Studio上修改项目(module)的包名(Package Name)   当你对着包名右击,查找Refactor(重构)下的Rename(快捷键:SHIFT+F6)时,弹出来的仅仅 ...

  5. 内存测试——内存泄露工具 LeakCanary

    项目地址 https://github.com/square/leakcanary 接入方法 1. 配置依赖 dependencies { debugCompile 'com.squareup.lea ...

  6. 第80天:jQuery插件使用

    jQuery其他补充+ 4.1 链式编程: end()补充 * 补充五角星 评论案例 * 第一步:鼠标移入,当前五角星和前面的五角星变实体.后面的变空心五角星 * 第二步:鼠标点击的时候,为当前元素添 ...

  7. iOS pch文件的创建

    3.iso pch头文件的创建  输入文件名的时候记得打钩 3.1.在Build Settings 里搜索pref就能找到preflx, 点击设置相对路径 $(SRCROOT) +路径:成功了就会显示 ...

  8. Golang基础(一)

    1. 变量声明与赋值 // var.go package main import "fmt" var a string var b bool var c string = &quo ...

  9. sql语句查询各门课程平均分的最大值

    解法一: select courseno,stuno,avg(score) '平均分最高值'--这里是求平均,后面的条件是过滤最大值的 from tablename group by courseno ...

  10. 【BZOJ4455】小星星(动态规划,容斥)

    [BZOJ4455]小星星(动态规划,容斥) 题面 BZOJ 洛谷 Uoj 题解 题意说简单点就是给定一张\(n\)个点的图和一棵\(n\)个点的树,现在要让图和树之间的点一一对应,并且如果树上存在一 ...