『ETOI_ #3』T6 / 水晶商家 / crystal 题解
『ETOI_ #3』T6 / 水晶商家 / crystal 题解
提高组试题,第三题。
前提知识点
逆元
若
则我们将
费马小定理
对于质数
组合数
从
思考
观察发现价值为
如果我们要购买总价值为
接下来开始考虑价值非
很明显我们可以使用背包计数 DP 进行处理。
定义
那么答案就是
因此我们要实现代码的两个部分:
- 预处理组合数。
- 背包计数 DP。
预处理组合数
如果用二维递推公式,开
那么只能过
这里注意到,都是
而
即
这里无论
求逆元基础模板题:【模板】模意义下的乘法逆元。
逆元可以用费马小定理求。
-
- 对于质数
p ,有a^{p-1}\equiv 1 (\mod p) 。 - 则对于质数
p ,有a^{p-2}\equiv a^{-1} (\mod p) 。 -
这里则要求
快速幂算法模版题:【模板】快速幂。
为了防止对于每一个测试数据组,预处理一次阶乘以及逆元。可以先读入所有
最后封装入一个函数
逆元求组合数模版题: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~!