跳到正文

题解

P5020 [NOIP2018 提高组] 货币系统

Genius_Star约 4 分钟
原文
文章信息与版本

文章信息

发布
2024年12月25日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 4 分钟
题目
P5020

版本

  1. v2当前
  2. v1

思路:

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

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

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

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

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

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

时间复杂度为 O(Tnw)O(Tnw)。

完整代码:

C++
#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;
}
原文