题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1808

Problem Description
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
 
启发题解:http://blog.csdn.net/liwen_7/article/details/8047273
抽屉原理:http://baike.baidu.com/link?url=qugmIxXpH8G9HJzDYsKlnWOtkPRvzgvLZ_Qjfi7bKG5gyorL8C5Wh5f3dJsp_PCU4MQlBEF7o7KH9URyhOBMX9yYu6_aYR4xG4Wp_Y4ktZca3WODwQ2alV-SDRVJJ-ES
 

 题意: 已知有n户人,每户会给小孩们一定数量的糖果(会给的数量假设小孩都已知),求小孩挑选哪几户人家,所得糖果总数能够使每个小孩得到相同数量的糖果,即是小孩数目的倍数?

 思路: 设a1、a2……am是正整数的序列,则至少存在整数k和l,(1<=k<l<=m),使得和a(k+1) + a(k+2) + ... ... +al是m的倍数。

 证明: x%m的余数有(m-1)中可能,即设有(m-1)个鸽巢,设sn代表(a1+a2+...+an)则m个sn产生m个余数,根据鸽巢原理,一定至少有两个s的余数相等,将这连个s想减,中间a(k+1) + a(k+2) + ... ... +al一定是m的倍数。

 
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string.h>
#include<cmath>
using namespace std;
#define N 100005 struct node
{
int num,r;
}k[N]; bool cmp(node a,node b)
{
if(a.r==b.r)
return a.num<b.num;
return a.r<b.r;
} int main()
{
long long c,a,sum;
int n;
bool flag;
while(~scanf("%lld%d",&c,&n))
{
if(c==&&n==)
break;
flag=false;
sum=;
k[].num=;
for(int i=;i<=n;i++)
{
scanf("%lld",&a);
sum+=a;
k[i].r=sum%c;
k[i].num=i;
if(k[i].r==&&flag==false)
{
flag=true;
printf("");
for(int j=;j<=i;j++)
printf(" %d",j);
printf("\n");
}
}
if(!flag)
{
sort(k+,k++n,cmp);
for(int i=;i<=n;i++)
{
if(k[i].r==k[i-].r)
{
printf("%d",k[i-].num+);
for(int j=k[i-].num+;j<=k[i].num;j++)
printf(" %d",j);
printf("\n");
flag=true;
break;
}
}
}
if(!flag)
printf("no sweets\n");
}
return ;
}

HDU 1808 Halloween treats(抽屉原理)的更多相关文章

  1. uva 11237 - Halloween treats(抽屉原理)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/37612503 题目链接:uva 11237 ...

  2. POJ 3370 Halloween treats(抽屉原理)

    Halloween treats Every year there is the same problem at Halloween: Each neighbour is only willing t ...

  3. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  4. HDU 1808 Halloween treats

    Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain ...

  5. UVA 11237 - Halloween treats(鸽笼原理)

    11237 - Halloween treats option=com_onlinejudge&Itemid=8&page=show_problem&category=516& ...

  6. HDU 5776 sum(抽屉原理)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=5776 Problem Description Given a sequence, you're ask ...

  7. hdu 1205 吃糖果 (抽屉原理<鸽笼原理>)

    吃糖果Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submissi ...

  8. Halloween treats HDU 1808 鸽巢(抽屉)原理

    Halloween treats Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. POJ 3370 Halloween treats(抽屉原理)

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6631   Accepted: 2448 ...

随机推荐

  1. mysql 全文搜索(转载http://blog.csdn.net/manbujingxin/article/details/6656992)

    前提:mysql只支持英文内容的全文索引,所以只考虑英文的全文搜索.假定数据表名为post,有三列:id.title.content.id是自增长序号,title是varchar,content是te ...

  2. idea中查看java类继承图

    复习java io,看了书里的这张图,太模糊了,想弄个出来放大看,一开始试了idea发现只能看父类,后来折腾了一会试了其他工具才发现还是idea好用. :) 一.打开type hierachy 光标放 ...

  3. 使用iText快速更新书签

    一.介绍 pdfbox基于Apache协议,商用无需开放源代码. iText基于APGL协议,打包和修改需发布源码,除非花钱买断. 二.用途 下载的电子书,有的书签是FitHeight,也就是缩放后整 ...

  4. Session重点整理

    首先明确几个概念 (1)JSessionID:通过tomcat运行的Java项目,为新用户生成的随机字符串.(应该是tomcat设置的,我没试过别的服务器,如有错误请指正) (2)Session请求( ...

  5. echarts-------饼形图

    首先echarts是一个可以提供给用户体验效果更好的一个图形界面, Canvas 类库 ZRender. 1.下载echarts的js,可以在官方网址进行下载echarts.min.js 2.将下载下 ...

  6. vijos 清点人数

    背景 NK中学组织同学们去五云山寨参加社会实践活动,按惯例要乘坐火车去.由于NK中学的学生很多,在火车开之前必须清点好人数. 描述 初始时,火车上没有学生:当同学们开始上火车时,年级主任从第一节车厢出 ...

  7. Miniconda安装scrapy教程

    一.背景说明 前两天想重新研究下Scrapy,当时的环境是PyCharm社区版+Python 3.7.使用pip安装一直报错 “distutils.errors.DistutilsPlatformEr ...

  8. python(2)之列表

    列表的使用 names=["zhangyang","liming",["sese","popo"],"xiao ...

  9. php composer 使用 以及 psr0和psr4的真正区别

    composer 使用 项目和库之间唯一的区别是,你的项目是一个没有名字的包 包名不区分大小写,但惯例是使用小写字母,并用连字符作为单词的分隔 入门比较好资源: 官网适合读一遍  https://do ...

  10. 逆袭之旅DAY20.XIA.循环结构

    2018-07-16 19:53:47 while循环 do do...while循环 for 循环