[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. QJsonDocument实现Qt下JSON文档读写

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QJsonDocument实现Qt下JSON文档读写     本文地址:http://tech ...

  2. PAT 甲级 1144 The Missing Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343463260160 Given N integers, you ...

  3. 开源人脸识别face_recognition

    环境:python36 1.安装dlib.face_recognition windows版 下载dlib,cp后面是py版本 下载地址:https://pypi.org/simple/dlib/ 提 ...

  4. py下载网络图片

    很简单,做下记录 import urllib import os url = "图片路径" dir = "d:\\pyimgtest\\G0000001\\" ...

  5. BZOJ 1237 配对(DP)

    给出两个长度为n的序列.这两个序列的数字可以连边当且仅当它们不同,权值为它们的绝对值,求出这个二分图的最小权值完全匹配.没有输出-1. n<=1e5.用KM会TLE+MLE. 如果连边没有限制的 ...

  6. 认识User-Agent

    Windows NT 10 对应操作系统 windows 10 Windows NT 6.3 对应操作系统 windows 8.1 Windows NT 6.2 对应操作系统 windows 8 Wi ...

  7. 关于在VWmare下安装Redhat

    本文是小白博主自行摸索安装的过程,下文主要是给出几篇很有用的文章出处和自己遇到的问题. 一.关于安装包 对于安装包,个人建议是去官网下载(个人安装的是rhel 7.4),可能初接触Linux时,是不是 ...

  8. redis2.4.conf配置文件中文释意

    # Redis示例配置文件 # 注意单位问题:当需要设置内存大小的时候,可以使用类似1k.5GB.4M这样的常见格式: # # 1k => 1000 bytes # 1kb => 1024 ...

  9. poj 1185 状态压缩

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27926   Accepted: 10805 Descriptio ...

  10. git 还原某个文件到特定版本

    1.先使用 git log 查看需要还原的版本号 2.git checkout <版本号> <文件相对路径> 3.git commit -m "xxx"