Barbells

Your local gym has n barbells and m plates. In order to prepare a weight for lifting, you mustchoose a single barbell, which has two sides. You then load each side with a (possibly empty) setof plates. For safety reasons, the plates on each side must sum to the same weight. What weightsare available for lifting?

For example, suppose that there are two barbells weighing 100100 and 110110 grams, and five platesweighting 1,4,5,5,1, 4, 5, 5, and 66 grams, respectively. Then, there are six possible weights available forlifting. The table below shows one way to attain the different weights:

Barbell Left side Right side Total
100 0 0 100
100 5 5 110
100 1 + 5 6 112
110 5 5 120
110 1 + 5 6 122
110 5 + 5 4 + 6 130

Input

The first line of input contains the space-separated integers nn and mm (1n,m14)(1 ≤ n, m ≤ 14).The second line of input contains nn space-separated integers b1,...,bnb_1, ..., b_n (1bi108)(1 ≤ b_i ≤ 10^8), denoting theweights of the barbells in grams.The third line of input contains m space-separated integers p1,...,pm (1pi108)p_1, ..., p_m \ (1 ≤ p_i ≤ 10^8), denoting theweights of the plates in grams.

Output

Print a sorted list of all possible distinct weights in grams, one per line.

樣例輸入
2 5
100 110
5 5 1 4 6
樣例輸出
100
110
112
120
122
130

nnmm的數量較少,最多隻有1414個,因此暴搜+剪枝即可。

#include<bits/stdc++.h>
#include<string.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 20;
bool v[N];
vector<ll> ans;
int a[N], b[N], n, m;
ll sum;

void dfs2(int u, ll sum1, ll sum2) {
    if (u == n + 1) {
        if (sum1 == sum2)repi(i, 1, m)ans.pb(sum1 * 2 + b[i]);
        return;
    }
    if (sum2 > sum1)return;
    if (!v[u]) {
        v[u] = true;
        dfs2(u + 1, sum1, sum2 + a[u]);
        v[u] = false;
    }
    dfs2(u + 1, sum1, sum2);
}

void dfs1(int u, ll sum1) {
    if (sum1 > sum)return;
    if (u == n + 1) {
        dfs2(1, sum1, 0);
        return;
    }
    v[u] = true;
    dfs1(u + 1, sum1 + a[u]);
    v[u] = false;
    dfs1(u + 1, sum1);
}

int main() {
    m = qr(), n = qr();
    repi(i, 1, m)b[i] = qr();
    repi(i, 1, n)a[i] = qr(), sum += a[i];
    sum >>= 1;
    dfs1(1, 0);
    sort(ans.begin(), ans.end());
    ans.erase(unique(ans.begin(), ans.end()), ans.end());
    for (auto it:ans)pl(it);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章