zoukankan      html  css  js  c++  java
  • CF1392H ZS Shuffles Cards

    Description

    zscoder has a deck of $n+m$ custom-made cards, which consists of $n$ cards labelled from $1$ to $n$ and $m$ jokers. Since zscoder is lonely, he wants to play a game with himself using those cards.

    Initially, the deck is shuffled uniformly randomly and placed on the table. zscoder has a set $S$ which is initially empty.

    Every second, zscoder draws the top card from the deck.

    If the card has a number $x$ written on it, zscoder removes the card and adds $x$ to the set $S$ .
    If the card drawn is a joker, zscoder places all the cards back into the deck and reshuffles (uniformly randomly) the $n+m$ cards to form a new deck (hence the new deck now contains all cards from $1$ to $n$ and the $m$ jokers). Then, if $S$ currently contains all the elements from $1$ to $n$ , the game ends. Shuffling the deck doesn't take time at all.
    What is the expected number of seconds before the game ends? We can show that the answer can be written in the form $frac{P}{Q}$ where $P,Q$ are relatively prime integers and $Q eq 0 mod 998244353$
    Output the value of $(P cdot Q^{-1})$

    Solution

    考虑先计算每一轮摸牌多少次,再考虑期望抽到Joker次数(轮数)

    每一张数字牌在所有Joker前的概率为$frac {1}{m+1}$,所以期望每一轮抽到的数字牌张数为$frac{n}{m+1}+1$

    设$f_k$为还剩$k$个数没有进入集合时的期望步数,有

    $$f_k=frac{m}{m+k}(f_k+1)+frac{k}{m+k}f_{k-1}$$

    解得

    $$f_k=f_{k-1}+frac{m}{k}$$

    $f_1$的值为$m+1$,则$f_n=1+msum _{i=1}^nfrac{1}{i} $

    最后两个数相乘

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int n,m;
    long long inv[2000005]={0,1},ans;
    const long long mod=998244353;
    inline int read(){
        int f=1,w=0;
        char ch=0;
        while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9') w=(w<<1)+(w<<3)+ch-'0',ch=getchar();
        return f*w;
    }
    int main(){
        for(int i=2;i<=2000001;i++)inv[i]=(mod-mod/i)*inv[mod%i]%mod;
        n=read(),m=read();+
        for(int i=1;i<=n;i++)(ans+=inv[i])%=mod;
        printf("%lld
    ",(ans*m%mod+1)*(n*inv[m+1]%mod+1)%mod);
        return 0;
    }
    ZS Shuffles Cards
  • 相关阅读:
    商城----项目宏观(1)
    Java动态代理-JDK自带实现
    git提交出现remote rejected master -> XX changes closed
    openstack认证实践
    转一篇Git代码回滚技巧
    线段树解决leetcode307. Range Sum Query
    The Skyline Problem leetcode 详解
    编程容易犯错的几个地方
    c++中小项堆声明和使用
    C++中int转为char 以及int 转为string和string 转int和字符串的split
  • 原文地址:https://www.cnblogs.com/JDFZ-ZZ/p/14310312.html
Copyright © 2011-2022 走看看