POJ 3370 Halloween treats(抽屉原理)
Halloween treats
Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a child will get nothing if it is too late. To avoid conflicts, the children have decided they will put all sweets together and then divide them evenly among themselves. From last year's experience of Halloween they know how many sweets they get from each neighbour. Since they care more about justice than about the number of sweets they get, they want to select a subset of the neighbours to visit, so that in sharing every child receives the same number of sweets. They will not be satisfied if they have any sweets left which cannot be divided.
Your job is to help the children and present a solution.
Input
The input contains several test cases.
The first line of each test case contains two integers c and n (1 ≤ c ≤ n ≤ 100000), the number of children and the number of neighbours, respectively. The next line contains n space separated integers a1 , … , an (1 ≤ ai ≤ 100000 ), where ai represents the number of sweets the children get if they visit neighbour i.
The last test case is followed by two zeros.
Output
For each test case output one line with the indices of the neighbours the children should select (here, index i corresponds to neighbour i who gives a total number of ai sweets). If there is no solution where each child gets at least one sweet print "no sweets" instead. Note that if there are several solutions where each child gets at least one sweet, you may print any of them.
Sample Input
4 5
1 2 3 7 5
3 6
7 11 2 5 13 17
0 0
Sample Output
3 5
2 3 4
题意 万圣节要糖,c个人,n个邻居,应该找那几个邻居要糖可以刚好平分。
题解 不存在没有的情况。抽屉原理,用两个数组存第一个存sum[i]%c,第二个存相同mod的位置,然后输出两个相同取余结果之间的位置。
一开始暴力超时,后来空间换时间。
#include<stdio.h>
#include<cstring>
long long a[100010];
long long sum[100010];
int main()
{
int c,n;
while(scanf("%d%d",&c,&n)!=EOF)
{
if(c==0&&n==0)
break;
else
{
memset(a,0,sizeof(a));
long long sum1=0;
int s;
int flag=0;
int k;
for(int i=1;i<=n;i++)
{
int s;
scanf("%d",&s);
sum1+=s;
sum[i]=sum1%c;
}
int i;
a[0]=1;
for(i=1;i<=n;i++)
{
if(a[sum[i]]) break ;
else
a[sum[i]]=i+1;
}
for(int j=a[sum[i]];j<=i-1;j++)
printf("%d ",j);
printf("%d\n",i);
}
}
return 0;
}
POJ 3370 Halloween treats(抽屉原理)的更多相关文章
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 3370 Halloween treats(抽屉原理)
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6631 Accepted: 2448 ...
- POJ 3370 Halloween treats 鸽巢原理 解题
Halloween treats 和POJ2356差点儿相同. 事实上这种数列能够有非常多,也能够有不连续的,只是利用鸽巢原理就是方便找到了连续的数列.并且有这种数列也必然能够找到. #include ...
- [POJ 3370] Halloween treats
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7143 Accepted: 2641 ...
- uva 11237 - Halloween treats(抽屉原理)
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/37612503 题目链接:uva 11237 ...
- POJ 3370 Halloween treats( 鸽巢原理简单题 )
链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...
- poj 3370 Halloween treats(鸽巢原理)
Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...
- 鸽巢原理应用-分糖果 POJ 3370 Halloween treats
基本原理:n+1只鸽子飞回n个鸽笼至少有一个鸽笼含有不少于2只的鸽子. 很简单,应用却也很多,很巧妙,看例题: Description Every year there is the same pro ...
- UVA 11237 - Halloween treats(鸽笼原理)
11237 - Halloween treats option=com_onlinejudge&Itemid=8&page=show_problem&category=516& ...
随机推荐
- KindEditor编辑器使用
KindEditor使用 1)kindeditor默认模式调用 <link rel="stylesheet" href="./KindEditor/themes/d ...
- ASP编码规范
ASP编码规范(--::) 第一章 ASP编码规范通述 ASP编码分为两大部分,一部分为静态文件编码,一部分为包含服务器端脚本的动态文件编码. 静态文件编码分script编码和HTML编码两部分. 服 ...
- asp.net MVC 中枚举创建下拉列表?
我将尝试使用 Html.DropDownList 扩展方法,但不能找出如何使用它的枚举. 让我们说我有一个这样的枚举: public enum ItemTypes { Movie = 1, Game ...
- ElasticSearch搜索demo
ElasticSearch版本:1.4.1 分词:ik jdk:1.7.67 开发工具:Eclipse 系统:win7 忙活了几天,使用ES做成,就是页面有点丑,demo页面如下: 1.搜索主页 2. ...
- Java爬虫初体验
年关将近,工作上该完成的都差不多了,上午闲着就接触学习了一下爬虫,抽空还把正则表达式复习了,Java的Regex和JS上还是有区别的,JS上的"\w"Java得写成"\\ ...
- 消息中间件之MQ详解及四大MQ比较
一.消息中间件相关知识 1.概述 消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之一.当今市面上有很多主流 ...
- Struts2初级篇(HelloWorld)
Struts2的工作流程: 从一个高水平角度看,Struts2 是一个MVC拉动的(或MVC2)框架,Struts2 的模型-视图-控制器模式是通过以下五个核心部分进行实现的: 操作(Actions) ...
- 【Python】python2 html safe string
import cgi s = '<>&' s += u'哈哈' print type(s) print s r = cgi.escape(s) print type(r) prin ...
- 如何查看win10已激活密钥?查看win10已激活完整密钥的方法!
如何查看win10已激活密钥?查看win10已激活完整密钥的方法! HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/So ...
- JAVA和数据库工具的下载地址(备用)
sqlite数据库驱动下载: http://repo1.maven.org/maven2/org/xerial/sqlite-jdbc/