P5020 [NOIP2018 提高组] 货币系统

· · 题解

思路:

直接求肯定不好求,考虑减去一些面值。

首先考虑按照面值从大到小排序。

若 a_i 可以被前面的 i - 1 个面值表示出来,则这张面值是不需要的;可以完全背包做一遍即可。

现在求出的 b 是 a 的子集中满足 (m, b) 与 (n, a) 等价且元素数量最少。

我们需要证明不存在一个不是 a 子集中的 b' 比 b 更优。

对于所有 x \in b', x \not\in a,x 肯定能被 b 中的表示出来;对于 b' 中 \in a 的部分 c,是表示不出来上面的 x 的;故 x 的数量应该是不少于 m - |c| 的。

时间复杂度为 O(Tnw)。

完整代码:

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define Add(x, y) (x + y >= mod) ? (x + y - mod) : (x + y)
#define lowbit(x) x & (-x)
#define pi pair<ll, ll>
#define pii pair<ll, pair<ll, ll>>
#define iip pair<pair<ll, ll>, ll>
#define ppii pair<pair<ll, ll>, pair<ll, ll>>
#define ls(k) k << 1
#define rs(k) k << 1 | 1
#define fi first
#define se second
#define full(l, r, x) for(auto it = l; it != r; ++it) (*it) = x
#define Full(a) memset(a, 0, sizeof(a))
#define open(s1, s2) freopen(s1, "r", stdin), freopen(s2, "w", stdout);
#define For(i, l, r) for(register int i = l; i <= r; ++i)
#define _For(i, l, r) for(register int i = r; i >= l; --i)
using namespace std;
using namespace __gnu_pbds;
typedef __int128 __;
typedef long double lb;
typedef double db;
typedef unsigned long long ull;
typedef long long ll;
bool Begin;
const int N = 50005, M = 105; 
inline ll read(){
    ll x = 0, f = 1;
    char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-')
          f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9'){
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return x * f;
}
inline void write(ll x){
    if(x < 0){
        putchar('-');
        x = -x;
    }
    if(x > 9)
      write(x / 10);
    putchar(x % 10 + '0');
}
int T, n, x, ans;
int a[M];
bitset<N> dp;
inline void solve(){
    for(int i = 1; i < N; ++i)
      dp[i] = 0;
    n = ans = read();
    dp[0] = 1;
    for(int i = 1; i <= n; ++i)
      a[i] = read();
    sort(a + 1, a + n + 1);
    for(int i = 1; i <= n; ++i){
        x = a[i];
        if(dp[x]){
            --ans;
            continue;
        }
        for(int k = 0; x * (1 << k) <= a[n]; ++k)
          dp |= (dp << (x * (1 << k)));
    }
    write(ans);
    putchar('\n');
}
bool End;
int main(){
    T = read();
    while(T--)
      solve();
    cerr << '\n' << abs(&Begin - &End) / 1048576 << "MB";
    return 0;
}