题解:AT_abc360_e [ABC360E] Random Swaps of Balls
[ABC360E] Random Swaps of Balls 题解
题意
有
高桥正好要执行下面的操作
- 在
1 和N 之间均匀随机地选择一个整数,包括两次。设a 和b 为所选整数。如果是a \neq b ,从左边开始交换a -th 和b -th 两个球。
经过
思路
记
因为若黑球不在最左边,在其他点的概率都相同。
求出
设
记
记
注意每次操作所选的
状态转移:
即保持在最左边的概率加上从其他点回到最左边的概率。
时间复杂度
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
const int mod = 998244353;
int qpow(int a, int x) {
int ret = 1;
for (; x; x >>= 1, a = a * a % mod)
if (x & 1) ret = ret * a % mod;
return ret;
}
int inv(int x) {return qpow(x, mod - 2);}
int n, k, p, q, e, ans, dp[N];
signed main() {
cin >> n >> k;
p = 2 * (n - 1) % mod * inv(n * n % mod) % mod;
q = 2 * inv(n * n % mod) % mod;
dp[0] = 1;
for (int i = 1; i <= k; i ++)
dp[i] = (1 - p + mod) % mod * dp[i - 1] % mod + q % mod * (1 - dp[i - 1]) % mod,
dp[i] += mod, dp[i] %= mod;
ans += dp[k];
e = (1 - dp[k] + mod) % mod * inv(n - 1) % mod;
e *= (n - 1) % mod * (n + 2) % mod * inv(2) % mod;
e %= mod;
ans += e, ans %= mod;
cout << ans << endl;
return 0;
}