版本比较
题解:P1001 A+B Problem
正文
增加 124 行删除 1 行
32 行未改动333435363737383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916032 行未改动现在只需要用集合幂级数的 $\ln, \exp, +$ 了,这都是简单的,所以就做完了。
集合幂级数的运算的复杂度显然是 $O(n^2 2^n)$,但是我们这里构造的集合幂级数都满足 $n = 1$,因此 $n^2 2^n$ 为常数,所以复杂度为 $O(1)$。
[代码](https://www.luogu.com.cn/paste/d1uadszw)。:::info[代码]
```cpp#include <bits/stdc++.h>using namespace std;constexpr int N = 15;constexpr int M = 1e5;constexpr int mod = 998244353;int n, A, B, a[1 << N], b[1 << N], inv[N + 1], ka, inva, kb, invb;long long ans;inline int add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }int pw(int x, int y) { int ret = 1; for (; y; y >>= 1, x = 1ll * x * x % mod) if (y & 1) ret = 1ll * ret * x % mod; return ret;}struct SPS{ int val[1 << N]; SPS() { memset(val, 0, sizeof val); } SPS(const SPS &another) { memcpy(val, another.val, sizeof val); } SPS &operator = (const SPS &another) { memcpy(val, another.val, sizeof val); return *this; } SPS operator + (const SPS &another) const { SPS ret; for (int S = 0; S < (1 << n); ++S) ret.val[S] = add(val[S], another.val[S]); return ret; } SPS operator * (const int &another) const { SPS ret; for (int S = 0; S < (1 << n); ++S) ret.val[S] = 1ll * val[S] * another % mod; return ret; } SPS operator * (const SPS &another) const { SPS ret; for (int S = 0; S < (1 << n); ++S) ret.val[S] = 1ll * val[S] * another.val[S] % mod; return ret; } void FMT(bool op) { for (int i = 1; i < (1 << n); i <<= 1) for (int j = 0; j < (1 << n); j += (i << 1)) for (int k = 0; k < i; ++k) { if (op == 0) val[i ^ j ^ k] = add(val[i ^ j ^ k], val[j ^ k]); if (op == 1) val[i ^ j ^ k] = add(val[i ^ j ^ k], mod - val[j ^ k]); } }};struct FPS{ SPS val[N + 1]; FPS() {; } FPS(const FPS &another) { for (int i = 0; i <= n; ++i) val[i] = another.val[i]; } FPS &operator = (const FPS &another) { for (int i = 0; i <= n; ++i) val[i] = another.val[i]; return *this; } FPS operator + (const FPS &another) const { FPS ret; for (int i = 0; i <= n; ++i) ret.val[i] = val[i] + another.val[i]; return ret; } FPS operator * (const int &another) const { FPS ret; for (int i = 0; i <= n; ++i) ret.val[i] = val[i] * another; return ret; } FPS operator * (const FPS &another) const { FPS ret; for (int i = 0; i <= n; ++i) for (int j = 0; j <= i; ++j) ret.val[i] = ret.val[i] + val[j] * another.val[i - j]; return ret; } void get(int *a) { for (int S = 0; S < (1 << n); ++S) val[__builtin_popcount(S)].val[S] = a[S]; for (int i = 0; i <= n; ++i) val[i].FMT(0); } void set(int *a) { for (int i = 0; i <= n; ++i) val[i].FMT(1); for (int S = 0; S < (1 << n); ++S) a[S] = val[__builtin_popcount(S)].val[S]; }} f, g;FPS ln(const FPS &f) { FPS g; for (int i = 1; i <= n; ++i) { SPS tmp; for (int j = 1; j < i; ++j) tmp = tmp + g.val[j] * f.val[i - j] * j; g.val[i] = f.val[i] + tmp * (mod - inv[i]); } return g;}FPS exp(const FPS &g) { FPS f; for (int S = 0; S < (1 << n); ++S) f.val[0].val[S] = 1; for (int i = 1; i <= n; ++i) { SPS tmp; for (int j = 1; j <= i; ++j) tmp = tmp + g.val[j] * f.val[i - j] * j; f.val[i] = tmp * inv[i]; } return f;}signed main() { cin.tie(0) -> sync_with_stdio(false); cin >> A >> B, A += 1e9, B += 1e9; n = 1, inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; // 低位 a[0] = A % M + 1, a[1] = B % M, b[0] = b[1] = 1; ka = a[0], inva = pw(ka, mod - 2); for (int S = 0; S < (1 << n); ++S) a[S] = 1ll * a[S] * inva % mod; kb = b[0], invb = pw(kb, mod - 2); for (int S = 0; S < (1 << n); ++S) b[S] = 1ll * b[S] * invb % mod; f.get(a), g.get(b); f = ln(f), g = ln(g), f = f + g, f = exp(f); f.set(a); for (int S = 0; S < (1 << n); ++S) a[S] = 1ll * a[S] * ka % mod * kb % mod; ans += add(a[(1 << n) - 1], mod - 1); // 高位 a[0] = A / M + 1, a[1] = B / M, b[0] = b[1] = 1; ka = a[0], inva = pw(ka, mod - 2); for (int S = 0; S < (1 << n); ++S) a[S] = 1ll * a[S] * inva % mod; kb = b[0], invb = pw(kb, mod - 2); for (int S = 0; S < (1 << n); ++S) b[S] = 1ll * b[S] * invb % mod; f.get(a), g.get(b); f = ln(f), g = ln(g), f = f + g, f = exp(f); f.set(a); for (int S = 0; S < (1 << n); ++S) a[S] = 1ll * a[S] * ka % mod * kb % mod; ans += 1ll * add(a[(1 << n) - 1], mod - 1) * M; ans -= 2e9; cout << ans;}```
:::