Question link: Portal
Idea: this is the topic of the concept of pigeon cage... After reading this principle, we can understand it in three ways ..
The number of C representatives, and N represents the number of families ..
[1] First, the prefix and the remainder are required. First, if the remainder is 0, the first N conditions are met, this is a set of feasible solutions...
[2] But what if there is no 0 ?? So let's look at the remainder .. What does it mean if there is cooccurrence at two points ?? It indicates that the sum of the last number after occurrence of the same remainder is a multiple of C, and a set of feasible solutions are obtained ..
[3] If the question says no, "No sweetws" will be output. But what about the situation ??? It is only when [1] [2] is not displayed ??? That is, when n is less than C, it may be impossible, because there may not be the same result, but if n> = C, there will certainly be the same result at two points ..
Then this problem is solved... Question: Halloween treats
| Time limit:2000 ms |
|
Memory limit:65536 K |
| Total submissions:6638 |
|
Accepted:2451 |
|
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
Code:
#include<cstdio>#include<cstring>const int maxn=100000+10;int a[maxn],mod[maxn];int c,n;int main(){ __int64 sum; while(~scanf("%d%d",&c,&n)) { if(c==0&&n==0) return 0; sum=0; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=0;i<c;i++) mod[i]=-1; for(int i=1;i<=n;i++) { sum=(sum+a[i])%c; if(sum==0) { for(int j=1;j<i;j++) printf("%d ",j); printf("%d\n",i); break; } if(mod[sum]!=-1) { for(int j=mod[sum]+1;j<i;j++) printf("%d ",j); printf("%d\n",i); break; } mod[sum]=i; } } return 0;}