题目传送: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. PythonWEB框架之Flask

    前言: Django:1个重武器,包含了web开发中常用的功能.组件的框架:(ORM.Session.Form.Admin.分页.中间件.信号.缓存.ContenType....): Tornado: ...

  2. 浏览器行为模拟之requests、selenium模块

    requests模块 前言: 通常我们利用Python写一些WEB程序.webAPI部署在服务端,让客户端request,我们作为服务器端response数据: 但也可以反主为客利用Python的re ...

  3. WebSphere概要文件的创建与删除

    一.创建单server服务器 /was/bin/manageprofiles.sh -create -profileName server1 \ -profilePath /was/profiles/ ...

  4. js 正则常用函数 会正则得永生

    正则表达式作为一种匹配处理字符串的利器在很多语言中都得到了广泛实现和应用,web开发本质上是处理字符串(服务端接受请求处理后拼接字符串作为响应,这在早期的CGI编程中最明显,然后客户端解析字符串进行渲 ...

  5. 在Ubuntu 12.04 上为Virtualbox 启用USB 设备支持

    在Ubuntu 12.04 上为Virtualbox 启用USB 设备支持  http://www.cnblogs.com/ericsun/archive/2013/06/10/3130679.htm ...

  6. 文献管理软件zotero的一点使用感受作者: 杨林畅

    作者是我的本科同学叶家鑫 http://www.renren.com/profile.do?id=240875124 文章写于去年12月,我做了一些排版上的修改,括号内的蓝字为我所加 ---- zot ...

  7. 【SQL】glob 和 like 的区别

    LIKE 和 GLOB 都可以用来匹配通配符指定模式的文本值.如果搜索表达式与模式表达式匹配,LIKE 运算符将返回真(true),也就是 1 区别: (1)使用的通配符不一样 like:  百分号( ...

  8. Win10系列:UWP界面布局基础1

    随着技术的不断发展,使用者对应用程序的界面体验提出了更高的要求,为了应对越来越复杂的界面设计需求和有效的简化界面开发过程,微软公司在其应用程序的开发技术当中引入一套新的应用程序界面描述语言,这就是XA ...

  9. os 模块 和 os模块下的path模块

    import os # os 主要用于与操作系统进行交互 #获取当前的工作目录 print(os.getcwd()) #切换工作目录 os .chdir("D:\上海python全栈4期\d ...

  10. JDBC:随机生成车牌号,批量插入数据库

    package InsertTest; /* * 单客户端:批量插入 */ import java.sql.Connection; import java.sql.DriverManager; imp ...