首页 > ACM题库 > HDU-杭电 > HDU 1808 Halloween treats-组合数学-[解题报告] C++
2013
12-23

HDU 1808 Halloween treats-组合数学-[解题报告] C++

Halloween treats

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

4 5
1 2 3 7 5
3 6
7 11 2 5 13 17
0 0

样例输出:

3 5
2 3 4

组合数学的知识,偶然碰到的题,于是把几道简单的都刷了。。

不知为什么网上的题解都把Poj2356写成2365……这就是以讹传讹么……

P.S. 2356也做了,是道计算几何水题,看别人的解题报告还学到了一个函数:

hypot

#include <cstdio>
#include <cstring>

int data[100005];
int remainder[100005];
int c,n;

int main ()
{
    while (scanf("%d%d",&c,&n) && (c||n))
	{
		int i,s,e;
		memset(remainder,0,sizeof(remainder));
		for (i=1;i<=n;i++)
			scanf("%d",&data[i]);
		int sum=0;
		for (i=1;i<=n;i++)
		{
			sum=(sum+data[i])%c;
			if (sum == 0)
			{
				s=1;
				e=i;
				break;
			}
			else if (remainder[sum])
			{
				s=remainder[sum]+1;
				e=i;
				break;
			}
			else
				remainder[sum]=i;
		}
		for (i=s;i<=e;i++)
			printf(i==e?"%d\n":"%d ",i);
    }
    return 0;   
}
一开始没多想,就想了隔一个放一个,过了之后搜解题报告发现这题还挺深奥……
参见 http://ip96cns.blog.163.com/blog/static/17009519220111217501402/
#include <cstdio>

int main ()
{
	int T;
	scanf ("%d",&T);
	while (T--)
	{
		int n,temp,maxn;
		__int64 sum=0;
		scanf("%d",&n);
		for (int i=0;i<n;i++)
		{
			scanf("%d",&temp);
			if (temp>maxn)
				maxn=temp;
			sum+=temp;
		}
		if (sum-maxn+1>=maxn)
			puts("Yes");
		else
			puts("No");
	}
	return 0;
}

解题报告转自:http://blog.csdn.net/whyorwhnt/article/details/8770010


  1. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。

  2. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥

  3. 第二块代码if(it != mp.end())应改为if(it != mp.end() && (i+1)!=(it->second +1));因为第二种解法如果数组有重复元素 就不正确