Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 8516 | Accepted: 3700 | Special Judge |
Description
Input
Output
If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.
Sample Input
5 1 2 3 4 1
Sample Output
2 2 3
Source
题意:给出n个正整数,让从其中找出某些数且这些数的和为n的倍数
因为是任意找一些数,答案可能不是唯一的,所以有以下做法。
参考博客:
http://blog.csdn.net/kenden23/article/details/29608899
http://blog.csdn.net/wmn_wmn/article/details/7829665
作为鸽巢原理,这个题目算是简单的,但是中间的数学原理一般还是不容易想出来的。下面是参考博客中的内容,这里稍作修改:
定理:给定m个数a1,a2,...,an.则必存在整数k和l(0<=k<l<=n))使得a[k+1]+a[k+2]+....+a[l]能被n整除。
证:
有a1,a1+a2,a1+a2+a3,...,a1+a2+..+an.
若上面n个数中有能被n整除的。那么显然存在连续个的数之和(a1+...+ai)能被n整除,那么结果即是a1+...+ai。
否则,对上面每一个数对n模运算,得到n个数,但是我们知道余数只能是1-->n-1,所以由鸟笼原理知道,至少有两个数的余数相同。
且设为k,l(k<l)使得,a1+a2+a3+....+ak与a1+a2+a3+..+al对n取模运算有相同的余数t,
a1+a2+a3+....+ak=x*n+t;
a1+a2+a3+..+al=y*n+t;
相减得a[k+1]+a[k+2]+....+a[l]=(y-x)*n,即能被n整除。
例子:n=5,分别为1,2,3,4,1
1,1+2,1+2+3,1+2+3+4,1+2+3+4+1
1,3,6,10,11
取模后,1,3,1,0,1
先判断有无取模后为0的情况,有所以为1,2,3,4
若无,取模相等的即可
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 10005
int n,a[M],sum[M],pos[M];
bool flag[M];
int main()
{
int i;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
for(i=1;i<=n;i++)
{
int x=sum[i]%n;
if(flag[x]==true)
{
int y=pos[x];
printf("%d\n",i-y);
for(int j=y+1;j<=i;j++)
printf("%d\n",a[j]);
break;
}
if(x==0)
{
printf("%d\n",i);
for(int j=1;j<=i;j++)
printf("%d\n",a[j]);
break;
}
flag[x]=true;
pos[x]=i;
}
return 0;
}
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 9015 | Accepted: 3244 | 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 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
Source
题意:有c个孩子,n个邻居和n个邻居可以给的糖的数量,让选出一些邻居使得从这些邻居中获得的糖的数量之和为c的倍数,
注意这里求的是c的倍数而不是n的倍数,这里可以用鸽笼定理是因为c<=n
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 100005
int c,n,a[M],pos[M];
bool flag[M];
ll sum[M];
int main()
{
int i;
while(scanf("%d%d",&c,&n)!=EOF)
{
if(n==0&&c==0)
break;
memset(pos,-1,sizeof(pos));
memset(flag,false,sizeof(flag));
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
for(i=1;i<=n;i++)
{
int x=sum[i]%c;
if(flag[x])
{
int y=pos[x];
for(int j=y+1;j<=i;j++)
printf("%d ",j);
printf("\n");
break;
}
if(x==0)
{
for(int j=1;j<=i;j++)
printf("%d ",j);
printf("\n");
break;
}
flag[x]=true;
pos[x]=i;
}
}
return 0;
}