标签:母函数经典题
Dividing
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18835 Accepted Submission(s): 5260
Problem Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could
just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so
that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets
of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described
by the input-line ``1 0 1 2 0 0‘‘. The maximum total number of marbles will be 20000.
The last line of the input file will be ``0 0 0 0 0 0‘‘; do not process this line.
Output
For each colletcion, output ``Collection #k:‘‘, where k is the number of the test case, and then either ``Can be divided.‘‘ or ``Can‘t be divided.‘‘.
Output a blank line after each test case.
Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Sample Output
Collection #1:
Can‘t be divided.
Collection #2:
Can be divided.
Source
考查知识点:限制性的母函数,与hdu 2082 2079 类似。
//突然感觉跟 2082很像 只可惜无奈超时。。。。参考了别人代码 才知道不管多重背包还是母函数好像都要取模,但是为什么不知道。。。-_-||
//大坑:想不到取模。。。-_-||
#include<stdio.h>
#include<string.h>
int c1[1000100],c2[1000100];
int a[1000100];
int main()
{
int i,k,j;
int count=1;
while(~scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]),a[1]+a[2]+a[3]+a[4]+a[5]+a[6])
{
for(i=1;i<=6;++i)
a[i]%=80;
int sum=a[1]+2*a[2]+3*a[3]+4*a[4]+5*a[5]+6*a[6];
if(sum&1)
{
printf("Collection #%d:\nCan't be divided.\n\n",count++);
continue;
}
int ave=sum/2;
memset(c2,0,sizeof(c2));
memset(c1,0,sizeof(c1));
// memset(a,0,sizeof(a));
for(i=0;i<=a[1];++i)
{
c1[i]=1;
}
for(i=2;i<=6;++i)
{
for(j=0;j<=ave;++j)
{
for(k=0;k+j<=ave&&k<=a[i]*i;k+=i)
c2[j+k]+=c1[j];
}
for(j=0;j<=ave;++j)
{
c1[j]=c2[j];
c2[j]=0;
}
}
if(c1[ave])
{
printf("Collection #%d:\nCan be divided.\n\n",count++);
}
else
printf("Collection #%d:\nCan't be divided.\n\n",count++);
}
return 0;
}
(母函数经典题 与2082类似)hdu 1059 Dividing
标签:母函数经典题
原文地址:http://blog.csdn.net/ice_alone/article/details/44201743