poj 2356 Find a multiple(鸽巢原理)
Description
The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that numbers is not greater than . 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 ( <= 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 . 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
Sample Output
Source
题意:有n个数,求是否存在一些数的和是n的倍数。若存在,输出即可。不存在,输出0.
思路:鸽巢原理的题目,组合数学课本上的原题。可以把和求出来,然后对n取余,因为有n个和,对n取余,如果余数中没有出现0,根据鸽巢原理,一定有两个数的余数相同,两个和想减就是n的倍数。如果余数出现0,自然就是n的倍数。也就是说,n个数中一定存在一些数的和是n的倍数。求余输出即可。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 10006
#define inf 1e12
int n;
int sum[N];
int vis[N];
int a[N];
int tmp[N];
int main()
{
while(scanf("%d",&n)==){
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++){
//int x;
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
}
memset(vis,,sizeof(vis));
memset(tmp,,sizeof(tmp));
for(int i=;i<=n;i++){
int x=sum[i]%n;
if(vis[x]){
int y=tmp[x];
printf("%d\n",i-y);
for(int j=y+;j<=i;j++){
printf("%d\n",a[j]);
}
break; }
if(x==){
printf("%d\n",i);
for(int j=;j<=i;j++){
printf("%d\n",a[j]);
}
break;
}
vis[x]=;
tmp[x]=i;
} }
return ;
}
poj 2356 Find a multiple(鸽巢原理)的更多相关文章
- 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个数,使得\(\sum | n\).本题使用Special Judge. 题解: 既然使用special judge,我们可以直接构造答案. 首先构造在mod N剩余系下 ...
- poj 3370 Halloween treats(鸽巢原理)
Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2356 && POJ 3370 鸽巢原理
POJ 2356: 题目大意: 给定n个数,希望在这n个数中找到一些数的和是n的倍数,输出任意一种数的序列,找不到则输出0 这里首先要确定这道题的解是必然存在的 利用一个 sum[i]保存前 i 个数 ...
- 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 ...
- [POJ2356]Find a multiple 题解(鸽巢原理)
[POJ2356]Find a multiple Description -The input contains N natural (i.e. positive integer) numbers ( ...
随机推荐
- qt model/view 架构自定义模型之QFileSystemModel
# -*- coding: utf-8 -*- # python:2.x #QFileSystemModel """ Qt 内置了两种模型:QStandardItemM ...
- 深入理解Scala的隐式转换系统
摘要: 通过隐式转换,程序员可以在编写Scala程序时故意漏掉一些信息,让编译器去尝试在编译期间自动推导出这些信息来,这种特性可以极大的减少代码量,忽略那些冗长,过于细节的代码. 使用方式: 1. ...
- Dave(正方形能围成的最大点数)
Dave Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submis ...
- Cocos2d-x 3.0rc0版本号项目的创建和部署
<1>执行setup.py 依照提示.加入你须要加入的环境变量 <2>创建项目批处理文件Create-Cocos2d-x 3.0-Project.bat 内容例如以下: @ec ...
- 初次使用SVN心得
进入实验室, 一个项目往往需要多天多人次共同维护,所以版本控制也显得尤为关键.下面是我第一次使用SVN工具的心得体会. 首先是安装,服务器搭配方面应该是之前完成的,这里就不多讲了. 下载地址:http ...
- Sprite Kit编程指南(1)-深入Sprite Kit
深入Sprite Kit 学习Sprite Kit最好的方法是在实践中观察它.此示例创建一对场景和各自的动画内容.通过这个例子,你将学习使用Sprite Kit内容的一些基础技术,包括: · ...
- 【jquery ,ajax,php】加载更多实例
jquery $(function() { //初始化 getData(0); var index = 1; $("#more").click(function() { getDa ...
- ASP.NET MVC:自定义 Route 生成小写 Url(转)
先给出本文中测试用的 controller: public class PersonsController : Controller { public ActionResult Query(strin ...
- 关于object在使用上的问题
关于object在使用上的问题 1.直接调用标签<object>中的单击事件 <object>是含有onclick和ondblclick两个事件的.按照以往方式,直接在里面调用 ...
- 【SQL语句】 - Ctrl+3 查询表属性的存储过程 [sp_select_talberowName]
/**-- ============================================= Author: xftCteateDate: 2013-10-11Description:查看表 ...