【2017 Multi-University Training Contest

Link:

Description

给你a数组的n个数的所有2^n个子集的2^n个子集元素的和;
子集元素的和最大为m;
告诉你各个子集元素的和出现的次数;

1 2
则0出现1次,1出现1次,2出现一次,3出现一次;
分别对应{},{1},{2},{1,2};
问你能不能复原出原数组;
输出最小字典序的数组;

Solution

给你一个b数组;
实际上和背包方案数问题的f数组是一样的;
f[i]表示物品组成的体积为i时的方案数

for (int i = 1;i <= n;i++)
    for (int j = m ; j >= w[i];j--)
        if (f[j-w[i]])
            f[j] += f[j-w[i]];


我们可以保证,最小的出现过的正数,一定是a[1];
(假设a数组是从小到大的顺序给出);
那么,我们把这个a[1]取走,让其只剩下n-1个物品,使得b数组里面不包含这个a[1]物品的信息;这样的话,就能继续按照”取最小的出现过的正数”这个原则找a[2]了;
使得b数组里面不包含a[1]的信息
逆序做一遍背包就可以了;
按照上面的更新顺序,逆着写一下即可.

NumberOf WA

2

Reviw

一开始忘记是f[j]+f[j-w[i]]了

Code

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x+1)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;

LL n,m;

int main(){
    //Open();
    //Close();
    int T;
    ri(T);
    while (T--){
        rl(n),rl(m);
        LL ans;
        //special?
        if ( m <= n - 1){
            ans =  (m*(m-1)*2 + m*2) + (n*(n-1) -m*(m-1) - m*2 )*n;
        }else{
            LL temp = m-(n-1);
            LL temp1 = (n-1)*(n-2)/2;
            if (temp >= temp1){
                ans = n*(n-1);
            }else{
                LL delta = temp1-temp;
                ans = (n-1)*2 + delta*2*2 + temp*2;
            }
        }
        ol(ans);puts("");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7626131.html