HDU - 1059 Dividing (母函数二进制优化)/求哪些数能被组合出来

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. 
InputEach 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. 
OutputFor 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.

题意 : 有 价值为 1,2,3,4,5,6,价值 的弹珠w[i]个,问是否能平分为2份 ,让两份价值相同

解:显然用普通的母函数数据太大会超时,我们用v数组来标记是否能组成该数字

我们可以用sum统计为所以弹珠的总价值,如果总价值为奇,显示不能平分

否则我们sum=sum/2,我们显然要看最后v[sum]=1,那么能平分

什么是二进制优化: 比如我们有100个价值为3的弹珠 

那么我们可以把它分为   1+2+4+8+16+32+37   因为这7个数能组成100以内任何数,你可以表示你要取几个价值为3的弹珠

而不用一个一个加

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
int v[3511330];
int w[110];
int main()
{
    int yy=1;
 while(~scanf("%d%d%d%d%d%d",&w[1],&w[2],&w[3],&w[4],&w[5],&w[6])&&w[1]+w[2]+w[3]+w[4]+w[5]+w[6])
 {
     int summ=0;
     for(int i=1;i<=6;i++)
         summ+=w[i]*i;
     printf("Collection #%d:\n",yy++);
     if(summ%2==1)//如果是奇数,那么不能平分
     {
         printf("Can't be divided.\n\n");
         continue;
     }
     int sum=summ/2;
     memset(v,0,sizeof(v));
     v[0]=1;//显然0是一个人什么都不拿的情况
    for(int i=1;i<=6;i++)//枚举6种弹珠
    {
        for(int j=1;w[i];j=j<<1)//j 不断扩大两倍 
        {
            int u=i*(j<=w[i]?j:w[i]); // i*的意思是乘以弹珠价值 ,括号里面表示如果剩余的数量小于 j 那么直接取剩余数量就好 (如 100  ,我们取的是 1,2,4,8,16,32,37)
            for(int k=sum;k>=u;k--) //最大的范围到sum就好,最小范围到u ,因为我们要求k-u>0
            {
                if(v[k-u]) v[k]=1; //这句话的意思是,你把所有(已经存在的价值+ u )的情况标记为1
                if(v[sum]) break; //如果v[sum]已经存在,那么可以平分
            }
            if(v[sum]) break;
            if(j>w[i]) break; //如果 j>w[i] ,说明已经全找完了
            w[i]-=j;//剩余数量减少
        }
        if(v[sum]) break;
    }
    if(v[sum])
    printf("Can be divided.\n");
    else
      printf("Can't be divided.\n");
    printf("\n");
 }
}




猜你喜欢

转载自blog.csdn.net/dsaghjkye/article/details/79991792