• Cards


    Team Work
    You have a team of N people. For a particular task, you can pick any non-empty subset of people. The cost of having x people for the task is (x^k).

    Output the sum of costs over all non-empty subsets of people.

    Input
    Only line of input contains two integers (N (1 ≤ N ≤ 10^9)) representing total number of people and k (1 ≤ k ≤ 5000).

    Output
    Output the sum of costs for all non empty subsets modulo 109 + 7.

    Examples
    input

    1 1
    

    output

    1
    

    input

    3 2
    

    output

    24
    

    计算(ans=sum_{r=1}^nC_n^rr^k)

    首先,有((1+x)^n=sum_{r=0}^nC_n^rx^r)

    op1:微分再乘以x.
    (nx(1+x)^{n-1}=sum_{r=1}^nC_n^rrx^r)
    重复1:

    (nx(1+x)^{n-1}+n(n-1)x^2(1+x)^{n-2}=sum_{r=1}^nC_n^rr^2x^r)

    (nx(1+x)^{n-1}+(1+2)n(n-1)x^2(1+x)^{n-2}+n(n-1)(n-2)x^3(1+x)^{n-3}=sum_{r=1}^nC_n^rr^3x^r)
    (dotsb)

    (dp[k][b]=x^b(1+x)^{n-b})为k次op1以后的(x^b(1+x)^{n-b})的系数.

    (x*(dp[k][b]*x^b(1+x)^{n-b})')

    (=dp[k][b]*b*x^b(1+x)^{n-b}+dp[k][b]*(n-b)*x^{b+1}(1+x)^{n-(b+1)})

    (=dp[k+1][b]*x^b(1+x)^{n-b}+dp[k+1][b+1]*x^{b+1}(1+x)^{n-(b+1)})
    所以
    (dp[k+1][b]+=dp[k][b]*b)

    (dp[k+1][b+1]+=dp[k][b]*(n-b))

    (=>)

    [dp[k][b]=dp[k-1][b]*b+dp[k-1][b-1]*(n-(b-1)),n<=k ]

    [dp[k][b]=dp[k-1][b]*b,k>n ]

    (dp[i][j]=0, dp[0][0]=1)
    令x=1
    (ans=sum_{i=0}^n dp[k][i]*2^{n-i},kle n)

    (ans=sum_{i=0}^n dp[k][i]*2^{n-i}+sum_{i=n+1}^kdp[k][i],k> n)

    ll dp[5005];
    int main() {
        ll n,k;
        cin>>n>>k;
        dp[0]=1;
        for(int i=1;i<=min(k,n);++i){
            for(int j=i;j;--j)
                dp[j]=(dp[j]*j%mod+dp[j-1]*(n-j+1)%mod)%mod;
            dp[0]=0;
        }
        for(int i=n+1;i<=k;++i){
            for(int j=n;j;--j)
                dp[j]=(dp[j]*j%mod+dp[j-1]*(n-j+1)%mod)%mod;
            for(int j=n+1;j<=i;++j)
                dp[j]=dp[j]*j%mod;
        }
        ll ans=0,z=qpow(2,max(n-k,(ll)0));
        for(int i=min(k,n);i>=0;--i){
            ans=(ans+dp[i]*z%mod)%mod;
            z=z*2%mod;
        }
        for(int i=k;i>n;--i)ans=(ans+dp[i])%mod;
        cout<<ans;
        return 0;
    }
    

    cf-Cards
    Consider the following experiment. You have a deck of m cards, and exactly one card is a joker. n times, you do the following: shuffle the deck, take the top card of the deck, look at it and return it into the deck.

    Let x be the number of times you have taken the joker out of the deck during this experiment. Assuming that every time you shuffle the deck, all m! possible permutations of cards are equiprobable, what is the expected value of xk? Print the answer modulo 998244353.

    Input
    The only line contains three integers n, m and k((1≤n,m<998244353, 1≤k≤5000).)

    Output
    Print one integer — the expected value of xk, taken modulo 998244353 (the answer can always be represented as an irreducible fraction (ab), where b mod 998244353≠0; you have to print (a⋅b^{-1}mod998244353).)

    m张卡牌中只有一张鬼,抽取n次,每次抽到后放回打乱,设n次中抽到x次鬼,(x^k)的期望值是多少?

    一次抽到鬼的概率是(frac{1}{m})

    (ans=sum_{r=1}^nC_n^r(frac{1}{m})^r(1-frac{1}{m})^{n-r}r^k)

    (=frac{1}{m^n}sum_{r=1}^n(m-1)^{n-r}r^k)

    ((x+m-1)^n=sum_{r=0}^n(m-1)^{n-r}x^r)

    k次求导乘x即可.

    ll dp[5005][5005];
    int main() {
        ll n,m,k;
        cin>>n>>m>>k;
        dp[0][0]=1;
        for(int i=1;i<=min(k,n);++i){
            for(int j=1;j<=i;++j)
                dp[i][j]=(dp[i-1][j]*j%mod+dp[i-1][j-1]*(n-j+1)%mod)%mod;
        }
        for(int i=n+1;i<=k;++i){
            for(int j=1;j<=n;++j)
                dp[i][j]=(dp[i-1][j]*j%mod+dp[i-1][j-1]*(n-j+1)%mod)%mod;
            for(int j=n+1;j<=i;++j)
                dp[i][j]=dp[i-1][j]*j%mod;
        }
        ll ans=0;
        for(int j=0;j<=min(k,n);++j)
            ans=(ans+dp[k][j]*qpow(m,n-j)%mod)%mod;
        for(int j=n+1;j<=k;++j)
            ans=(ans+dp[k][j])%mod;
        cout<<ans*qpow(qpow(m,n),mod-2)%mod;
        return 0;
    }
    
    
  • 相关阅读:
    Fix “Could not flush the DNS Resolver Cache: Function failed during execution” When Flushing DNS
    Spring Boot 2.0 教程 | AOP 切面统一打印请求日志
    FTP服务器原理
    SAMBA 服务器原理
    时间服务器:NTP 服务器
    账号控管:NIS服务器
    NFS服务器原理
    DHCP服务器原理
    Linux防火墙
    linux网络完全与防护
  • 原文地址:https://www.cnblogs.com/foursmonth/p/14161916.html
Copyright © 2020-2023  润新知