基本原理:n+1只鸽子飞回n个鸽笼至少有一个鸽笼含有不少于2只的鸽子。

很简单,应用却也很多,很巧妙,看例题:

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 containsn 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 aisweets). 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

Source

题目大意:糖果平分问题。有c个小孩,n个提供糖果的邻居,你可以选择要或不要。现在你只考虑得到的全部糖果能否平分,可能有多种方案,输出一种即可。

上面的case 1: 结果 2 3 4 也行,总和为12. 输出一种即可

#include <stdio.h>
#include <algorithm>
using namespace std; int c,n,neigb[100001];
int S;
struct Remnant
{
int h,r; // 下标和余数
}R[100001]; bool cmp(const Remnant &a, const Remnant & b){ //按余数从小到大排序
if( a.r == b.r)
return a.h < b.h;
return a.r < b.r;
} int main(){
//freopen("in.txt","r",stdin);
while(scanf("%d %d", &c, &n) != EOF){
if(c==0 && n==0) break;
int k=-1,h;
S=0;
for(int i=0; i<n; i++)
{
scanf("%d",&neigb[i]);
S += neigb[i];
R[i].r = S%c; //存储是前i个和 对c的余数
R[i].h = i + 1; //h 为下标
if(k == -1 && R[i].r==0 ) k=i;
} if(k == -1){
sort(R, R+n, cmp);
for(int i=0; i<n-1; i++)
{
if(k == -1 && R[i].r == R[i+1].r)
{
k = R[i].h;
h = R[i+1].h;
break;
}
}
if(k==-1)
printf("no sweets\n");
else{
for(int i=k+1; i<h; i++)
printf("%d ",i);
printf("%d\n",h);
}
}else{
for(int i=0; i<k; i++)
printf("%d ",i+1);
printf("%d\n",k+1);
} }
return 0;
}

鸽巢原理应用-分糖果 POJ 3370 Halloween treats的更多相关文章

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

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

  2. POJ 3370 Halloween treats( 鸽巢原理简单题 )

    链接:传送门 题意:万圣节到了,有 c 个小朋友向 n 个住户要糖果,根据以往的经验,第i个住户会给他们a[ i ]颗糖果,但是为了和谐起见,小朋友们决定要来的糖果要能平分,所以他们只会选择一部分住户 ...

  3. POJ 3370 Halloween treats 鸽巢原理 解题

    Halloween treats 和POJ2356差点儿相同. 事实上这种数列能够有非常多,也能够有不连续的,只是利用鸽巢原理就是方便找到了连续的数列.并且有这种数列也必然能够找到. #include ...

  4. [POJ 3370] Halloween treats

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7143   Accepted: 2641 ...

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

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

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

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

  7. poj 3370 Halloween treats(鸽巢原理)

    Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...

  8. POJ 2356 && POJ 3370 鸽巢原理

    POJ 2356: 题目大意: 给定n个数,希望在这n个数中找到一些数的和是n的倍数,输出任意一种数的序列,找不到则输出0 这里首先要确定这道题的解是必然存在的 利用一个 sum[i]保存前 i 个数 ...

  9. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

随机推荐

  1. Swift - 让StoryBoard设计视图,程序运行时都使用横屏形式

    1,运行时横屏 将项目属性“General”->“DeviceOritentation”的Portrait复选框去掉 2,storyboard设计视图横屏 在storyboard中,单击中间界面 ...

  2. 插件 - 提示窗体(ArtDialog)

    效果: 代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default. ...

  3. 函数alv下的颜色设置

    ABAP中的颜色代码是由4位字都组成的 cxyz    c:color的简写,颜色代码均以C开头 x:标准色代码,SAP中一共有7个标准色    y:反转颜色启用/关闭 1/0 z:增强颜色启用/关闭 ...

  4. svn自动备份

    #!/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH DATE=` ...

  5. KMP 知识点总结

    KMP算法是BF算法的改进,主要是消除了主串指针的回溯,提高算法效率. 先简单介绍一下BF算法: 基本思路: 从目标串s的第一个字符开始和模式串的第一个字符比较,相等逐个比较后续字符,否则从目标串的第 ...

  6. 怎样获取自己的SSL证书

    2.创建证书,注意这里的common name应该填你的server name $ openssl req -new -key key.pem -out request.pem Country Nam ...

  7. 《转》Python多线程学习

    原地址:http://www.cnblogs.com/tqsummer/archive/2011/01/25/1944771.html 一.Python中的线程使用: Python中使用线程有两种方式 ...

  8. 在Ubuntu上录制视频和编辑(很全)

    Linux多媒体三剑客:GIMP,Inkscape,Blender3D Blender基金会制作的开源微电影Sintel:http://www.sintel.org/about电影采用Creative ...

  9. [Android学习笔记]Android Library Project的使用

    RT http://developer.android.com/tools/projects/index.html

  10. C语言实现通讯录

    <span style="font-size:18px;">#include<stdio.h> #include<string.h> #incl ...