hdu 1059 Dividing 多重背包 超级水题 ★

来源:互联网 发布:linux获取文件行数 编辑:程序博客网 时间:2024/05/24 06:39

Dividing

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21390    Accepted Submission(s): 6028


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 01 0 0 0 1 10 0 0 0 0 0
 

Sample Output
Collection #1:Can't be divided.Collection #2:Can be divided.
 

Source
Mid-Central European Regional Contest 1999
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  2191 1024 1081 1074 1078 
 

这个dp[v]的意义就是体积为v的背包能否填满。



#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI (4.0*atan(1.0))#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   ind<<1,le,mid#define rson    ind<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk    make_pair#define _f     first#define _s     secondusing namespace std;//const int INF=    ;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxV=20000*6+20    ;const int maxn=10    ;//const int maxm=    ;int num[maxn];bool dp[maxV];int V;int main(){    int kase=0;    while(1)    {        V=0;        for(int i=1;i<=6;i++)        {            scanf("%d",&num[i]);            V+=i*num[i];        }        if(!V)  break;        printf("Collection #%d:\n",++kase);        if(V%2)  {puts("Can't be divided.\n");continue;}        memset(dp,0,sizeof dp);        dp[0]=1;        for(int i=1;i<=6;i++)        {            int cnt;            int tot=num[i];            for(cnt=1;cnt<=tot   ;tot-=cnt,cnt*=2)            {                int val=i*cnt;                for(int v=V;v>=val;v-- )                {                    dp[v]=dp[v]|dp[v-val];                }            }            if(!tot)  continue;            int val=i*tot;              for(int v=V;v>=val;v-- )            {                    dp[v]=dp[v]|dp[v-val];            }        }        if(dp[V/2])  puts("Can be divided.\n");        else  puts("Can't be divided.\n");    }    return 0;}


0 0
原创粉丝点击