[POJ2356]Find a multiple 题解(鸽巢原理)
[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 题解(鸽巢原理)的更多相关文章
- poj 2356 Find a multiple(鸽巢原理)
Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...
- poj Find a multiple【鸽巢原理】
参考:https://www.cnblogs.com/ACShiryu/archive/2011/08/09/poj2356.html 鸽巢原理??? 其实不用map但是习惯了就打的map 以下C-c ...
- poj 2356 Find a multiple【鸽巢原理 模板应用】
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6651 Accepted: 2910 ...
- [POJ2356] Find a multiple 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8776 Accepted: 3791 ...
- poj2356 Find a multiple(抽屉原理|鸽巢原理)
/* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...
- [poj2356]--Find a multiple ——鸽巢原理
题意: 给定n个数,从中选取m个数,使得\(\sum | n\).本题使用Special Judge. 题解: 既然使用special judge,我们可以直接构造答案. 首先构造在mod N剩余系下 ...
- POJ2356 Find a multiple 抽屉原理(鸽巢原理)
题意:给你N个数,从中取出任意个数的数 使得他们的和 是 N的倍数: 在鸽巢原理的介绍里面,有例题介绍:设a1,a2,a3,……am是正整数的序列,试证明至少存在正数k和l,1<=k<=l ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- [POJ3370]&[HDU1808]Halloween treats 题解(鸽巢原理)
[POJ3370]&[HDU1808]Halloween treats Description -Every year there is the same problem at Hallowe ...
随机推荐
- Android Studio- 把项目提交到SVN中操作方法
第一步 下载SVN,下载完成之后,需要吧command line client tools点击修改安装 然后Crash Reporter点击选择取消安装 如果不进行该操作,则可能在C:\Program ...
- appium1.6.3/1.6.4/1.6.5版本下如何支持安卓下ByName定位
1. 换其他定位方式,比如用xpath代替 2. 使用ByAccessibilityId代替,感觉没什么效果 一招修改源码解决问题根源,修改方法如下: 找到你的appium\node_modules\ ...
- 采用FPGA实现UART转SPI
应用笔记 V1.1 2015/2/10 采用FPGA实现UART转SPI 概述 本文提供了实现UART转SPI的Verilog代码的功能描述.这份笔记将介绍UART和SPI的基本知识,代码设计 ...
- POJ3709_K-Anonymous Sequence
题意很简单,给你若干个数字,你需要减去一些数字,使得在数列中的每个数字出现的次数不少于k次. 一开始我们都会想到是用DP,于是很快我们就可以得出状态为搞定前面i个数所需要花费的最小代价用f[i]表示 ...
- 只要实现了annotation这个接口就是注解 同理:只要实现了某个接口就是该类型的实现类
只要实现了annotation这个接口就是注解 同理:只要实现了某个接口就是该类型的实现类
- 【Java】常用POI生成Excel文档设置打印样式
package poi_test; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi ...
- BZOJ3620 似乎在梦中见过的样子(kmp)
不是很懂为什么数据范围要开的这么诡异,想到正解都不敢写.用类似NOI2014动物园的方法,对每个后缀求出类似next的数组即可. #include<iostream> #include&l ...
- Mybatis笔记五:Mybatis的全局配置文件Configuration.xml讲解
从 XML 中构建 SqlSessionFactory 每个基于Mybatis应用都是以一个SqlSessionFactory实例为中心.SqlSessionFactory实例可以由SqlSessio ...
- 使用Unity5.1进行VR开发的配置(最新的未必是最好的!!!)
随着Unity5.1的发布,之前的Oculus Rift和Gear VR 开发流程发生了巨大的变化,这也算是小白鼠们必须付出的代价了~ 那么Unity5.1和Oculus的整合究竟发生了哪些变化,对开 ...
- 如何在vi中设置tab的长度
在使用vi写python时(其实,不管用什么写python程序时,都要注意),首先要将Tab键的长度设为4,因为使用python编程时,我们是通过缩进来实现作用域的,所以要统一Tab键的长度.具体方法 ...