POJ 题目3370 Halloween treats(鸽巢原理)

Halloween treats

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7099   Accepted: 2621   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

Ulm Local 2007

ac代码

#include<stdio.h>
#include<string.h>
int sum[100010],hash[100010];
int main()
{
	int n,m;
	while(scanf("%d%d",&m,&n)!=EOF,n||m)
	{
		int i;
		sum[0]=0;
		for(i=1;i<=n;i++)
		{
			int x;
			scanf("%d",&x);
			sum[i]=(sum[i-1]+x)%m;
		}
		memset(hash,0,sizeof(hash));
		int s,t;
		s=t=1;
		for(i=1;i<=n;i++)
		{
			if(sum[i]==0)
			{
				t=i;
				break;
			}
			else
				if(hash[sum[i]])
				{
					s=hash[sum[i]]+1;
					t=i;
					break;
				}
				else
				{
					hash[sum[i]]=i;
				}
		}
		for(i=s;i<t;i++)
		{
			printf("%d ",i);
		}
		printf("%d\n",t);
	}
}
时间: 2024-09-29 04:33:19

POJ 题目3370 Halloween treats(鸽巢原理)的相关文章

POJ 3370 Halloween treats 鸽巢原理 解题

Halloween treats 和POJ2356差点儿相同. 事实上这种数列能够有非常多,也能够有不连续的,只是利用鸽巢原理就是方便找到了连续的数列.并且有这种数列也必然能够找到. #include <cstdio> #include <cstdlib> #include <xutility> int main() { int c, n; while (scanf("%d %d", &c, &n) && c) { i

POJ 3370 Halloween treats - 鸽巢原理

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 l

poj 2356 Find a multiple 鸽巢原理的简单应用

题目要求任选几个自然数,使得他们的和是n的倍数. 由鸽巢原理如果我们只选连续的数,一定能得到解. 首先预处理前缀和模n下的sum,如果发现sum[i]==sum[j] 那么(sum[j]-sum[i])%n一定为0,直接输出i+1~j就够了. 为什么一定会有解,因为sum从1~n有n个数,而模n下的数只有0~n-1,把n个数放入0~n-1个数里,怎么也会有重复,所以这种构造方法一定没问题. 其实可以O(n)实现,嫌麻烦,就二重循环无脑了. #include <iostream> #includ

POJ 2356 Find a multiple 鸽巢原理

题目来源:POJ 2356 Find a multiple 题意:n个数 选出任意个数 使得这些数的和是n的倍数 思路:肯定有解 并且解是连续的一段数 证明: 假设有m个数 a1,a2,a3...am    s1 s2 s3...sm为前缀和 s1 = a1 s2 = a1+a2 s3 = a1+a2+a3... sm = a1+a2+a3+...+am 1.如果某个前缀和si%m == 0 那么得到解 2.设x1=s1%m x2 = s2%m x3 = s3%m xm = sm%m 因为1不成

POJ 3370 Halloween treats(抽屉原理)

Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6631   Accepted: 2448   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

poj 2356 Find a multiple (鸽巢原理妙用)

题目链接:http://poj.org/problem?id=2356 Description The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more

Poj3370Halloween treats鸽巢原理

和上题差不多一样的搞法. #include<iostream> #include<cstdio> #include<cstring> #include<map> #include<vector> #include<stdlib.h> using namespace std; const int maxn = 111111; int a[maxn]; int b[maxn]; int vis[maxn]; int main() { in

HDU3183(RMQ+鸽巢原理)

题目的意思是对于一个n位数,删除m个位后,得到的最小数是什么,比如12345 2,删除两个位,得到最小的就是123.实际上这题目解法很多,好像有贪心,线段树,RMQ等,因为我最近在学习RMQ,所以就用RMQ了. 这题目用了一个鸽巢原理,得到的m-n位数的第一位,必然出现在1~m-n+1,这个由鸽巢原理就十分明显了(如果1~n-(m-n)+1都没有的话,剩下的m-n-1个位是不可能凑出m-n个位的数的!)这样我们就可以从[1,n-(m-n)+1]中作RMQ取得最小值下标i,之后对于i+1后,m-n

POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798   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