题目传送: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 ;
}

最新文章

  1. angular的跨域(angular百度下拉提示模拟)和angular选项卡
  2. iOS FMDB的使用(增,删,改,查,sqlite存取图片)
  3. nutch1.4 在windows下面提示 java.io.IOException: CreateProcess error=2, ϵͳ&#213;Ҳ&#187;&#181;&#189;ָ&#182;
  4. wpf的毛边窗体效果 前台代码
  5. Unsupervised Classification - Sprawl Classification Algorithm
  6. 响应式架构:消息模式Actor实现与Scala、Akka应用集成
  7. Android 模拟器中sdcard操作
  8. sap判断条件
  9. Strust2的json插件
  10. C语言与管道
  11. RAC Cache Fusion 原理理解
  12. c#编写的基于Socket的异步通信系统
  13. MySQL的&quot;旁门左道&quot;用法总结
  14. 解读经典-《C#高级编程》第七版-Chapter1-.Net体系结构-Page13-20
  15. xe5 android 手机上使用sqlite [转]
  16. 格式化输出python
  17. iPhone系统常用文件夹位置
  18. vip会员统计表 (vip等级是灵活配置的 非写死1是金卡用户 2是什么 等)
  19. shiro学习笔记_0600_自定义realm实现授权
  20. 使用阿里云ECS安装HDFS的小问题

热门文章

  1. Oracle 视图和索引
  2. gleez框架获得时间控件
  3. VMware(虚拟机) 12版安装深度linux系统
  4. 从零搭建和配置OSX开发环境
  5. 【Linux】shell学习之sed
  6. java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
  7. NOIP2012国王游戏(60分题解)
  8. 33 个 2017 年必须了解的 iOS 开源库
  9. Oracle12c版本中未归档隐藏参数
  10. 通过JdbcTemplate编写数据访问(二十八)