Dividing HDU-1059 母函数

HDU-1059

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

0 0 0 2 0 0
1 0 0 0 1 2
1 0 0 0 1 1
33 12 54 78 65 11
0 0 0 0 0 0

output

Collection #1:
Can be divided.

Collection #2:
Can’t be divided.

Collection #3:
Can be divided.

Collection #4:
Can be divided.

code

//Siberian Squirrel
//#include<bits/stdc++.h>
#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>

#define ACM_LOCAL

using namespace std;
typedef long long ll;

const double PI = acos(-1);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;
const int MOD = 3221225473;
const int N = 2e5 + 10;
const int UP = 101;

int a[7], limit;
int f1[N], f2[N];

inline void solve(ll res = 0) {
    for (int i = 1; i <= 6; ++i) {
        a[i] %= 60;
        res += a[i] * i;
    }
    if (res & 1) {
        printf("Can't be divided.\n");
    } else {
        memset(f1, 0, sizeof f1);
        f1[0] = 1;
        for (int i = 1; i <= 6; ++i) {
            for (int j = 0; j <= res; ++j) {
                for (int k = 0; k <= a[i] && j + k * i <= res; ++k) {
                    f2[j + k * i] += f1[j];
                }
            }
            for (int j = 0; j <= res; ++j) {
                f1[j] = f2[j];
                f2[j] = 0;
            }
        }
        if (f1[res / 2]) {
            printf("Can be divided.\n");
        } else printf("Can't be divided.\n");
    }
}

int main() {
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
#endif
    int o = 1, cases = 0;
//    scanf("%d", &o);
    while(o --) {
        int f = 0;
        while(scanf("%d%d%d%d%d%d", &a[1], &a[2], &a[3], &a[4], &a[5], &a[6])) {
            f = 0;
            for(int i = 1; i <= 6; ++ i) f += a[i];
            if(f) {
                printf("Collection #%d:\n", ++ cases);
                solve();
                printf("\n");
            } else break;
        }
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值