逆天
Neutralized · · 个人记录
写官方题解的人是翀多了还是喝高了阿????
你过程基本全是错的,你礼貌吗???
友情链接
在此之前,我希望您可以欣赏一下优雅简洁的官方题解:
- BSOJ8107 锂
抽象 /fn。
能够发现每个连通块一定是一棵树,因为有向环代表偏序关系成环,显然不合法。
那么就相当于数出最终图是一个
对此可以直接写出一个 并且貌似完全不能优化 /ll,可以获得 30 分的高分。
可以把
接下来为了方便,我们设总边数
考虑容斥(二反),钦定
这部分先选出
考虑一条非钦定边
- 如果两个端点都是钦定边的端点,设两条钦定边分别是
e_i, e_j ,那么w_x \lt \min(w_{e_i}, w_{e_j}) = w_{e_{\min(i, j)}} ; - 如果两个端点中一个是钦定端点,设这条边是
e_i ,那么w_x \lt w_{e_i} ; - 否则没有限制。
考虑对于第
设
为什么我们要把下降幂拆成分式来写?好问题:
注意到
丑爆了!但是同时它也确实变得简练了。
带上系数求和,可以开始求答案了,注意是概率所以除以排列数(别忘了二反系数):
只需要预处理阶乘,阶乘逆元,二重阶乘即可做到
注意最后一项需要预处理到
#include <bits/stdc++.h>
using namespace std;
using ull = uint64_t;
using u128 = __uint128_t;
struct FastMod {
ull b, m; FastMod(ull b = 2) : b(b), m(ull((u128(1) << 64) / b)) { }
inline ull operator () (ull x) { ull q = ull(u128(m) * x >> 64), r = x - q * b; return r >= b ? r - b : r; }
} M;
#define ep emplace_back
#define all(x) begin(x), end(x)
using ll = long long;
constexpr int N = 1e7 + 3;
int n, K, p, fac[N], invf[N], fad[N];
inline int neg(int x) { return x ? p - x : 0; }
inline int P(int &x, int y) { return (x += y) >= p && (x -= p), x; }
inline int Pow(int bs, int b, int rs = 1) { for(; b; bs = M(1ull * bs * bs), b >>= 1) if(b & 1) rs = M(1ull * rs * bs); return rs; }
inline int C(int n, int m) {
if(m > n || m < 0) return 0;
return M(M(1ull * fac[n] * invf[n - m]) * invf[m]);
}
main() {
ios :: sync_with_stdio(0), cin.tie(0);
cin >> n >> K >> p, M = FastMod(p);
if(n == 2) cout << (K == 1) << '\n', exit(0);
fad[1] = fac[0] = fac[1] = invf[0] = invf[1] = 1;
for(int i = 2; i <= n; ++i)
fac[i] = M(1ull * fac[i - 1] * i),
fad[i] = M(1ull * fad[i - 1] * ((i << 1) - 1));
invf[n] = Pow(fac[n], p - 2);
for(int i = n; i >= 3; --i)
invf[i - 1] = M(1ull * invf[i] * i);
int Inv = Pow(fad[n - 1], p - 2), res = 0;
for(int t = K; t << 1 <= n; ++t) {
bool sgn = (t & 1) == (K & 1);
int d = M(1ull * C(n, t << 1) * fad[t]);
d = M(M(1ull * d * fad[n - t - 1]) * C(t, K));
P(res, sgn ? d : neg(d));
}
res = M(1ull * res * Inv);
cout << res << '\n';
}