Find a multiple POJ - 2356 【鸽巢原理应用】
Problem 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
题意:给出一个含有 n 个数字的序列,要找一个连续的子序列,使他们的和一定是 n 的倍数
思路:抽屉原理经典应用

AC代码:
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm> using namespace std;
#define N 100502
int arr[N];
int vis[N];
int sum[N];
int main(){
int n;
while(~scanf("%d",&n)){
int flag=;
sum[]=;
for(int i=;i<=n;i++){
scanf("%d",&arr[i]);
sum[i]=sum[i-]+arr[i];
if(sum[i]%n==){
flag=i;
}
}
if(flag){
printf("%d\n",flag);
for(int i=;i<=flag;i++){
printf("%d\n",arr[i]);
}
continue;
}
for(int i=;i<=n;i++){
if(vis[sum[i]%n]){
int ans=i-(vis[sum[i]%n]);
printf("%d\n",ans);
for(int j=vis[sum[i]%n]+;j<=i;j++){
printf("%d\n",arr[j]);
}
break;
}
vis[sum[i]%n]=i;
}
} return ;
} /*
5
4 3 4 3
4 7 11 14 */
Find a multiple POJ - 2356 【鸽巢原理应用】的更多相关文章
- POJ 2356 && POJ 3370 鸽巢原理
POJ 2356: 题目大意: 给定n个数,希望在这n个数中找到一些数的和是n的倍数,输出任意一种数的序列,找不到则输出0 这里首先要确定这道题的解是必然存在的 利用一个 sum[i]保存前 i 个数 ...
- [POJ2356]Find a multiple 题解(鸽巢原理)
[POJ2356]Find a multiple Description -The input contains N natural (i.e. positive integer) numbers ( ...
- Find a multiple POJ - 2356 (抽屉原理)
抽屉原理: 形式一:设把n+1个元素划分至n个集合中(A1,A2,…,An),用a1,a2,…,an分别表示这n个集合对应包含的元素个数,则:至少存在某个集合Ai,其包含元素个数值ai大于或等于2. ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- 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 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 3370 Halloween treats( 鸽巢原理简单题 )
链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...
- [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. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...
随机推荐
- Word 查找替换高级玩法系列之 -- 制表符对齐人工目录
自己在Word中输入目录的时候是不是总也对不齐最右边的页码?这就是人工制作目录的不足之处了,但因着它能让我们更自由的发挥,所以还是得到了一些人的偏爱.那么问题来了,到底要怎么对齐这种人工目录呢? 1. ...
- centos7.6编译安装php7.3
刚开始搞环境装过集成,发现不好用,后来自己编译安装一些扩展啊设置的都很容易找到. 以前装过5.6.7.0发现不一样,最近出了7.3是php5速度的三倍,那有必要升级一下列. 由于之前安装过老版本,依赖 ...
- PAT(B) 1006 换个格式输出整数(Java)
题目链接:1006 换个格式输出整数 (15 point(s)) 代码 /** * Score 15 * Run Time 153ms * @author wowpH * @version 1.1 * ...
- Geometers Anonymous Club CodeForces - 1195F (闵可夫斯基和)
大意: 给定$n$个凸多边形, $q$个询问, 求$[l,r]$内闵可夫斯基区间和的顶点数. 要用到一个结论, 闵可夫斯基和凸包上的点等于向量种类数. #include <iostream> ...
- archive_lag_target参数
需求,由于一套生产环境归档日志切换频率过低,建议修改参数,使其间隔一定时间周期自动切换生成归档日志; SQL>; THREAD# SEQUENCE# TO_CHAR(COMPLETION_TIM ...
- RDS for MySQL 逻辑备份文件恢复到自建数据库
使用MySQL自带的 mysqldump 工具可以通过逻辑备份文件恢复数据库,本文将介绍详细的逻辑备份恢复数据库操作步骤. 说明 通过物理备份文件恢复到自建数据库请参见RDS for MySQL 物理 ...
- CI/CD/Jenkins
Continuous Integration, Continuous Delivery & Deployment (CI/CD) 持续集成.持续部署&持续交付. Jenkins就是一个 ...
- hdu 6153 思维+扩展kmp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 扩展kmp不理解的看下:http://www.cnblogs.com/z1141000271/p ...
- (四)Activiti之流程定义部署之ZIP方式和流程定义查询
一.流程定义部署之ZIP方式 上一章节我们使用classpath的方式加载流程定义文件,这里我们使用ZIP的方式. 1.1 用activiti插件生成bpmn和png图片之后,压缩到一个zip格式的压 ...
- (二)Lucene之根据关键字搜索文件
前提:在使用lucene进行搜索的时候,必须先生成索引文件,即必须先进行上一章节的案例,生成索引文件如下: 该索引文件为"segments"开头,如果没有该文件则说明没有索引文件则 ...