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 5 1 2 3 7 5 3 6 7 11 2 5 13 17 0 0
Sample Output
3 5 2 3 4
分析:
例题2:任取5个整数,必然能够从中选出三个,使它们的和能够被3整除.
证明:任意给一个整数,它被3除,余数可能为0,1,2,我们把被3除余数为0,1,2的整数各归入类r0,r1,r2.至少有一类包含所给5个数中的至少两个.因此可能出现两种情况:1°.某一类至少包含三个数;2°.某两类各含两个数,第三类包含一个数.
若是第一种情况,就在至少包含三个数的那一类中任取三数,其和一定能被3整除;若是第二种情况,在三类中各取一个数,其和也能被3整除..综上所述,原命题正确.
代码1: //(会长所写) #include<cstdio>
#include<vector>
#include<algorithm>
#define MAX 100000
using namespace std;
int sum[MAX+5];
vector<int> d[MAX+5];
int main()
{
int mod,n;
while (~scanf ("%d%d",&mod,&n) && (mod!=0 && n!=0))
{
for (int i = 0 ; i < mod ; i++)
{
d[i].clear();
}
for (int i = 1 ; i <= n ; i++)
{
int t;
scanf ("%d",&t);
sum[i] = sum[i-1] + t;
sum[i] %= mod;
d[sum[i]].push_back(i);
}
int st,endd;
if (d[0].size() > 0)
{
st = 1;
endd = d[0][0];
}
else
{
for (int i = 1 ; i < mod ; i++)
{
if (d[i].size() >= 2)
{
st = d[i][0]+1;
endd = d[i][1];
break;
}
}
}
while (st <= endd)
{
printf ("%d%c",st,st == endd ? '\n' : ' ');
st++;
}
}
return 0;
}
代码2:
#include<stdio.h>
int a[1000005];
int main()
{int n,m;
while(~scanf("%d%d",&n,&m)&&n+m)
{int flag=1,x,y=0,l,r;
for(int i=1;i<=m;i++)
a[i]=0;
for(int i=1;i<=m;i++)
{
scanf("%d",&x);
y=(y+x)%n;
if(flag)
{
if(y==0)
{
l=1;r=i;flag=0;continue;
}
else if(a[y])
{
l=a[y]+1;r=i;flag=0;continue;
}
a[y]=i;
}
}
for(int i=l;i<=r;i++)
if(i<=r-1)
printf("%d ",i);
else printf("%d\n",i);
}
return 0;
}