天天看點

HDU-1059 Dividing(DP)Dividing http://acm.hdu.edu.cn/showproblem.php?pid=1059

Dividing

http://acm.hdu.edu.cn/showproblem.php?pid=1059

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.
        

初學DP,隻知道01背包,沒看過多重背包,隻是感覺應該這樣做,逾時n次,網上看到别人的解法和分析證明收獲頗多

http://www.cnblogs.com/walker01/archive/2010/02/06/1665033.html這個解法還是看不懂,待以後懂的更多了再來看

#include <cstdio>
#include <cstring>
#include <algorithm>
#define LOCAL
using namespace std;

const int MAXN=60005;
int m,maxv,n[9];
int dp[MAXN];
int mod[7]={1,42,42,14,84,210,42};

int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
#endif // LOCAL
    int kase=0;
    while(scanf("%d%d%d%d%d%d",&n[1],&n[2],&n[3],&n[4],&n[5],&n[6]),(n[1]||n[2]||n[3]||n[4]||n[5]||n[6])) {
        printf("Collection #%d:\n",++kase);
        //優化法一:			http://poj.org/bbs?problem_id=1014
        //POJ上給出的一種優化,不明白為什麼
        /*
        for(int i=1;i<=6;++i)
            if(n[i]>60) {
                if(1==(1&n[i]))
                    n[i]=61;
                else
                    n[i]=60;
            }
        */

        //優化法二:自己苦想半天想出來的取模優化,
        //          看了			http://blog.csdn.net/gg_gogoing/article/details/38453273
        //          才知道取模原來都是錯的...
        //          同時又看到:取模前不為0,取模結果為0時,重新賦為模可避免問題(不知道能不能避免全部錯誤的發生)

        //1+2+3+4+5+6=21,則對i和21的最小偶公倍數取模,剛好能消去21*2n(n=0,1,2……),每一堆都能分得21*n
        for(int i=1;i<=6;++i) {
            bool flag=true;
            if(0==n[i])
                flag=false;
            n[i]%=mod[i];
            if(0==n[i]&&flag)
                n[i]=mod[i];
        }
        int mini=n[1];
        for(int i=2;i<=6;++i)
            if(n[i]<mini)
                mini=n[i];
        if(1==(mini&1))
            --mini;
        for(int i=1;i<=6;++i)
            n[i]-=mini;

        maxv=0;
        for(int i=1;i<=6;++i)
            maxv+=n[i]*i;
        if(1==(maxv&1)) {
            printf("Can't be divided.\n\n");
            continue;
        }
        m=maxv/2;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=6;--n[i]) {
            if(n[i]==0) {
                ++i;
                ++n[i];
                continue;
            }
            for(int j=m;j>=i;--j) {
                dp[j]=max(dp[j-i]+i,dp[j]);
            }
        }
        if(m!=dp[m])
            printf("Can't be divided.\n\n");
        else
            printf("Can be divided.\n\n");
    }
    return 0;
}