Trick :无 trick,概率 DP

题目链接

E - Random Swaps of Balls (atcoder.jp)

Statements

Problem Statement

There are N − 1 N - 1 N1 white balls and one black ball. These N N N balls are arranged in a row, with the black ball initially at the leftmost position.

Takahashi will perform the following operation exactly K K K times.

  • Choose an integer uniformly at random between 1 1 1 and N N N, inclusive, twice. Let a a a and b b b the chosen integers. If a ≠ b a \neq b a=b, swap the a a a-th and b b b-th balls from the left.

After K K K operations, let the black ball be at the x x x-th position from the left. Find the expected value of x x x, modulo 998244353 998244353 998244353.

Solution

d p i , j dp_{i,j} dpi,j 表示第 i i i 轮黑球位于位置 j j j 的概率

根据期望定义 : r e s = ∑ i = 1 N d p i , j ∗ i res=\sum_{i=1}^N dp_{i,j}*i res=i=1Ndpi,ji

发现 2 ~ N 其实是没有区别的位置,即 :

d p k , 2 = d p k , 3 = ⋯ = d p k , N dp_{k,2}=dp_{k,3}=\cdots=dp_{k,N} dpk,2=dpk,3==dpk,N

所以 :
r e s = d p k , 2 ∗ ∑ i = 2 N i + d p k , 1 res=dp_{k,2}*\sum_{i=2}^N i+dp_{k,1} res=dpk,2i=2Ni+dpk,1

d p k , 2 dp_{k,2} dpk,2 可以递推 :

λ \lambda λ 为黑球保持不变的概率,多少我忘了

这一轮位于 x =上一轮位于 x * 保持 + 上一轮不位于 * 交换

公式我也懒得写了

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long

int const mod = 998244353;

int ksm(int a, int k, int p){
    int res = 1;
    a %= p;
    while(k){
        if(k & 1) res = res * a % p;
        k >>= 1;
        a = a * a % p;
    }
    return res;
}
int inv(int x){
    x %= mod;
    return ksm(x, mod - 2, mod);
}
int n, k;
void solve(){
    cin >> n >> k;
    int p = 2LL * inv(n * n) % mod;
    for(int i = 2; i <= k; i ++){
        int t1 = 2LL * inv(n * n) % mod;
        int t2 = ((n * n % mod - 2 * n) % mod + mod) % mod;
        t2 = t2 * inv(n * n) % mod;
        p = (t1 + t2 * p % mod) % mod;
    }
    int res = 0; 
    int tmp = ((n * (n + 1) % mod * inv(2LL) - 1) % mod + mod) % mod;
    res = tmp * p % mod; 

    (p = (n * n % mod - 2 * n + 2) % mod + mod) %= mod;
    p = p * inv(n * n) % mod;
    for(int i = 2; i <= k; i ++){
        int t1 = 2LL * inv(n * n) % mod;
        int t2 = ((n * n % mod - 2 * n) % mod + mod) % mod;
        t2 = t2 * inv(n * n) % mod;
        p = (t1 + t2 * p % mod) % mod;
    } 
    (res += p) %= mod;
    cout << res << '\n'; 
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0); 
    solve();
    return 0;
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值