Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 7879 | Accepted: 2866 | Special Judge |
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 andn (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), whereai represents the number of sweets the children get if they visit neighbouri.
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, indexi corresponds to neighbouri who gives a total number ofai 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 5 1 2 3 7 5 3 6 7 11 2 5 13 17 0 0
Sample Output
3 5 2 3 4
Source
题目大意:
一共有n个数,从其中取一些数使它们的和膜c等于0。
解题思路:
由抽屉原理可得,此题一定有解,只要我们计算出前缀和%c的值,当两个点的前缀和%c相等使,中间的值之和%c一定等于0。(注意,需要补充一个一个元素都没有的情况)。
附AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#define LL long long
using namespace std;
const int maxn=100000+5;
int a[maxn],sum[maxn],c,n;
int vis[maxn];
int main()
{
while(~scanf("%d%d",&c,&n)&&(c||n))
{
memset(vis,-1,sizeof vis);//初始化
int l=0,r=0;
bool flag=false;
vis[0]=0;//补充0
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
if(i==1)
sum[i]=a[i]%c;
else sum[i]=(sum[i-1]+a[i])%c;//计算前i项和%c的值
if(!flag&&vis[sum[i]]==-1)//如果没出现过则存下下标
vis[sum[i]]=i;
else if(!flag&&vis[sum[i]]!=-1)//这个数前面出现过,则这次出现和上次出现之间的数的和%c==0
{
l=vis[sum[i]];
r=i;
flag=true;
}
}
for(int i=l+1;i<=r;++i)//将其中的下标全部输出
{
printf("%d",i);
if(i<r)
putchar(' ');
}
putchar('\n');
}
return 0;
}