『ETOI_ #3』T6 / 水晶商家 / crystal 题解
提高组试题,第三题。
前提知识点
逆元
若 。
则我们将 称作 在模 意义下的逆元,记作 。
费马小定理
对于质数 ,有 。
组合数
从 个不同的物品里选出 个,方案数为 。
思考
观察发现价值为 的水晶数量非常多。
如果我们要购买总价值为 的 价值水晶,其方案数为 ,表示在 个 价值水晶中选 个进行购买。
接下来开始考虑价值非 的特殊水晶怎么处理。
很明显我们可以使用背包计数 DP 进行处理。
定义 表示购买总价值为 的特殊水晶的方案数。
那么答案就是 。
因此我们要实现代码的两个部分:
- 预处理组合数。
- 背包计数 DP。
预处理组合数
如果用二维递推公式,开 的数组:
- 。
那么只能过 的数据点。
这里注意到,都是 的形式, 是一个定值。
而 ,这里只需要模一个模数 即可。
即 。
这里无论 、 还是 都是在 范围内,可以预处理。除法要求逆元。
求逆元基础模板题:【模板】模意义下的乘法逆元。
逆元可以用费马小定理求。
- 是一个质数。
- 对于质数 ,有 。
- 则对于质数 ,有 。
- 就是要求的逆元。因为是质数,总有这样的逆元。
这里则要求 的结果,用快速幂算法即可。
快速幂算法模版题:【模板】快速幂。
为了防止对于每一个测试数据组,预处理一次阶乘以及逆元。可以先读入所有 ,找到其最大值,预处理直接预处理到 即可。
最后封装入一个函数 ,求组合数的函数就做好了。
逆元求组合数模版题:B3717 组合数问题
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const int MOD = 998244353;
const int MAXN = 5000005;
const int MAXV = 10005;
ll fact[MAXN], inv_fact[MAXN];
ll qpow(ll a, ll b) {//快速幂
ll res = 1;
while (b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
void init_comb(int n) {
fact[0] = 1;
for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % MOD;//递推阶乘
inv_fact[n] = qpow(fact[n], MOD - 2);//使用快速幂计算逆元
for (int i = n; i >= 1; i--) inv_fact[i - 1] = inv_fact[i] * i % MOD;//逆推接下来的逆元
}
ll C(int n, int k) {
if (k < 0 || k > n) return 0;//非法情况
return fact[n] * inv_fact[k] % MOD * inv_fact[n - k] % MOD;//使用公式计算
}背包计数 DP
定义 表示购买总价值为 的特殊水晶的方案数。
则有转移公式 ,从 到 枚举 进行转移,然后枚举 最大到 。
注意在枚举 的时候要倒序枚举,防止出现重复计算。
for (int val : v) {
if (val > cap) continue;
for (int s = cap; s >= val; s--) {
dp[s] = (dp[s] + dp[s - val]) % MOD;
}
}统计答案
处理完毕以上两步后。
答案就是 。
注意取模。
ll ans = 0;
for (int s = 0; s <= cap; s++) {
ll need = k - s;
if (need >= 0 && need <= n) {
ans = (ans + dp[s] * C(n, (int)need)) % MOD;
}
}AC代码
时间复杂度
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const int MOD = 998244353;
const int MAXN = 5000005;
const int MAXV = 10005;
ll fact[MAXN], inv_fact[MAXN];
ll qpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
void init_comb(int n) {
fact[0] = 1;
for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % MOD;
inv_fact[n] = qpow(fact[n], MOD - 2);
for (int i = n; i >= 1; i--) inv_fact[i - 1] = inv_fact[i] * i % MOD;
}
ll C(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n] * inv_fact[k] % MOD * inv_fact[n - k] % MOD;
}
struct TestCase {
int n, m;
ll k;
vector<int> v;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int c, t;
cin >> c >> t;
vector<TestCase> tests;
int max_n = 0;
for (int idx = 0; idx < t; idx++) {
int n, m;
ll k;
cin >> n >> m >> k;
vector<int> v(m);
for (int i = 0; i < m; i++) cin >> v[i];
tests.push_back({n, m, k, v});
max_n = max(max_n, n);
}
init_comb(max_n);
for (auto &tc : tests) {
int n = tc.n, m = tc.m;
ll k = tc.k;
vector<int> &v = tc.v;
// 计算特殊水晶总价值 V
int V = 0;
for (int x : v) V += x;
// 如果 k 超过 n + V,答案为 0
if (k > n + V) {
cout << 0 << '\n';
continue;
}
// 背包容量 = min(k, V)
int cap = (int)min<long long>(k, V);
// dp[s] = 用特殊水晶凑出总价值 s 的方案数
vector<ll> dp(cap + 1, 0);
dp[0] = 1;
for (int val : v) {
if (val > cap) continue;
for (int s = cap; s >= val; s--) {
dp[s] = (dp[s] + dp[s - val]) % MOD;
}
}
ll ans = 0;
for (int s = 0; s <= cap; s++) {
ll need = k - s;
if (need >= 0 && need <= n) {
ans = (ans + dp[s] * C(n, (int)need)) % MOD;
}
}
cout << ans << '\n';
}
return 0;
}好啦,这就是『ETOI_ #3』的最后一题!我们下一次比赛再见ヾ( ̄▽ ̄)Bye~ Bye~!