POJ 3370 Halloween treats (Drawer principle), poj3370

Source: Internet
Author: User

POJ 3370 Halloween treats (Drawer principle), poj3370

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

Description

Every year there is the same problem at Halloween: Each neighbor 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 neighbor. since they care more about justice than about the number of sweets they get, they want to select a subset of the neighbors to visit, so that in sharing every child would es 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 integersCAndN(1 ≤ c ≤ n ≤ 100000), The number of children and the number of neighbors, respectively. The next line containsNSpace separated integersA1 ,...,AN(1 ≤ ai ≤ 100000), WhereAIRepresents the number of sweets the children get if they visit neighborI.

The last test case is followed by two zeros.

Output

For each test case output one line with the indices of the neighbors the children shocould select (here, indexICorresponds to neighborIWho gives a total numberAISweets ). 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

Source

Ulm Local 2007


Question: Give c and n, next n numbers, calculate the sum of any number and a multiple of c, and output any group of answers (note that it is arbitrary)


Drawer principle: Put 10 apple to nine drawers, at least one drawer has more than 1 Apple


Why is this question a drawer principle? You calculate the sum of the First n numbers (n in total) and mod c. Since n is greater than c, you can guess how many remainder there will be,

There can be n at most, that is, 0 ~ N-1, while 0 meets the condition. In other words, either 0 or at least two of the n remainder have the same remainder,

Now let's take a look at the situation where the two integers are the same. For example, if sum [1] % c = sum [n] % c, then a [2] + a [3] + .. + a [n] is a multiple of c. That's all,

Check the Code:






#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 100005int a[N];int vis[N];int c,n;int main(){    int i;    while(~scanf("%d%d",&c,&n))    {        memset(vis,-1,sizeof(vis));        for(i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        int temp=0,j;        for(i=1;i<=n;i++)        {            temp+=a[i];            temp%=c;            if(temp==0)            {                for(j=1;j<=i;j++)                    if(j==1)                       printf("%d",j);                    else                       printf(" %d",j);               printf("\n");                break;            }            if(vis[temp]!=-1)            {                for(j=vis[temp]+1;j<=i;j++)                  if(i==j)                      printf("%d",j);                   else                      printf("%d ",j);                      printf("\n");              break;            }            vis[temp]=i;        }    }    return 0;}








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.