poj3370(鸽巢原理-与2356一样)

来源:互联网 发布:js调用身份证读取器 编辑:程序博客网 时间:2024/05/20 18:18

http://poj.org/problem?id=3370

Halloweentreats
Time Limit: 2000MSMemory Limit: 65536KTotal Submissions: 4911Accepted: 1851Special Judge

Description

Every year there is the same problem at Halloween: Eachneighbour is only willing to give a certain total number of sweetson that day, no matter how many children call on him, so it mayhappen that a child will get nothing if it is too late. To avoidconflicts, the children have decided they will put all sweetstogether and then divide them evenly among themselves. From lastyear's experience of Halloween they know how many sweets they getfrom each neighbour. Since they care more about justice than aboutthe number of sweets they get, they want to select a subset of theneighbours to visit, so that in sharing every child receives thesame number of sweets. They will not be satisfied if they have anysweets 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 integersc and n (1 ≤ c ≤n ≤ 100000), the number of children and the number ofneighbours, respectively. The next line containsn space separated integersa1 , ... ,an (1 ≤ ai ≤100000 ), where airepresents the number of sweets the children get if they visitneighbour i.

The last test case is followed by two zeros.

Output

For each test case output one line with the indices of theneighbours the children should select (here, indexi corresponds to neighbouri who gives a total number ofai sweets). If there is nosolution where each child gets at least one sweet print "no sweets"instead. Note that if there are several solutions where each childgets 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

UlmLocal 2007
与2356一样,具体详解参看上一篇文章。。
注意这题仅仅是输出他的下标,上一题输出的是他他下标对应的数。。。
#include
#include
#include
using namespace std;
#define maxn 100010
int a[maxn];
int b[maxn];
int main()
{
    intn,c;
   while(scanf("%d%d",&c,&n)!=EOF)
    {
       int i,k=0,p;
       for(i=0;i
       {
           scanf("%d",&a[i]);
       }
       memset(b,-1,sizeof(b));
       b[0]=0;
       for(i=0;i
       {
           k=(k+a[i])%c;
           if(k==0)
           {
               //printf("%d\n",i+1);
               for(p=0;p<=i;p++)
               {
                   printf("%d ",p+1);
               }
               printf("\n");
               break;
           }
           else if(b[k]==-1)
           {
               b[k]=i;
           }
           else
           {
               //printf("%d\n",i-b[k]);
               for(p=b[k]+1;p<=i;p++)
               {
                   printf("%d ",p+1);
               }
               printf("\n");
               break;
               //return 0;
           }
       }
    }
    return0;
}
原创粉丝点击