poj 3370 Halloween treats

来源:互联网 发布:ubuntu 16.04 ati驱动 编辑:程序博客网 时间:2024/05/29 14:12
Halloween treats
Time Limit: 2000MSMemory Limit: 65536KB64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]

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 51 2 3 7 53 67 11 2 5 13 170 0

Sample Output

3 52 3 4

[Submit]   [Go Back]   [Status]

//鸽笼原理 poj3370

/*

详见:http://www.cppblog.com/pcfeng502/archive/2009/10/18/98902.aspx

题意:给定孩子的数目c和邻居的数目n,以及每个邻居拥有的糖块数目ai

由于孩子想要公平,他们想每个人得到的糖块数是一样的,所以他们选择部分邻居,被选到的邻居必须拿出所有糖果。求他们选到的邻居,如果不能保证每个孩子至少拿到一个糖果则输出“no sweets”。可以的话输出任一个满足条件的即可。

即要求选择的糖果数目之和能被c整出:

解法:

本来:想dp一把,但是怎么分析都不行。。会tle

还是数学厉害啊。。

我们在碰到1.sum[i] == 0 或者2.sum[i]的值已经在前面出现过的情况时,就可以知道有解,并且解是从上一个出现该sum[i]数值的后一个位置开始→现在sum[i]的位置,这个貌似可以解出可能的解;但由于加和的顺序随机,所以还不清楚是否可以在该case有解的情况下一定能够解出解。这时候,我们就要用到鸽笼原理了。

*/


#include <iostream>using namespace std;int sweet[100001];int sum[100001];int use[100001];int c,n;bool is;int main(){    int i,j,k;    while(scanf("%d%d",&c,&n))    {        if(!c&&!n)break;        is=false;        memset(use,0,sizeof use);        for(i=0;i<n;i++)        {           scanf("%d",&sweet[i]);        }        sum[0]=sweet[0]%c;        if(sum[0]==0){            printf("1\n");            continue;        }        use[sum[0]]=1;        for(i=1;i<n;i++)        {            sum[i]=(sum[i-1]+sweet[i])%c;            if(sum[i]==0)            {                is=true;                for(k=1;k<=i+1;k++)printf("%d%c",k,k==i+1?'\n':' ');                break;            }            else            if(use[sum[i]])            {                 is=true;                 for(k=use[sum[i]]+1;k<=i+1;k++)printf("%d%c",k,k==i+1?'\n':' ');                 break;            }            use[sum[i]]=i+1;        }        if(!is)printf("no sweets\n");    }    return 0;}


原创粉丝点击