题解 抛硬币 期望

题解 抛硬币

题目描述

在这里插入图片描述在这里插入图片描述

具体做法与心路历程

对于这种期望题可以直接设方程,然后递推。一般是不连续的带系数,连续的不带系数。

具体做法

f [ i ] f[i] f[i]表示连续 i i i次正面朝上的期望步数。

f [ i ] = f [ i − 1 ] + 1 + ( 1 − p ) × f [ i ] f[i]=f[i-1]+1+(1-p)\times f[i] f[i]=f[i1]+1+(1p)×f[i]

化简得: f [ i ] = f [ i − 1 ] × 1 p + 1 p f[i]=f[i-1]\times\frac{1}{p}+\frac{1}{p} f[i]=f[i1]×p1+p1

通项公式为:设 i n v = 1 p , f [ n ] = 1 − i n v n + 1 1 − i n v − 1 inv=\frac{1}{p},f[n]=\frac{1-inv^{n+1}}{1-inv}-1 inv=p1,f[n]=1inv1invn+11.

C o d e \mathcal{Code} Code

ps:考试忘记求逆元了,爆0 。。。

/*******************************
Author:galaxy yr
LANG:C++
Created Time:2019年11月04日 星期一 14时24分45秒
*******************************/
#include<cstdio>
#include<algorithm>
#define int long long 

using namespace std;

struct IO{
    template<typename T>
    IO & operator>>(T&res)
    {
        T q=1;char ch;
        while((ch=getchar())<'0' or ch>'9')if(ch=='-')q=-q;
        res=(ch^48);
        while((ch=getchar())>='0' and ch<='9') res=(res<<1)+(res<<3)+(ch^48);
        res*=q;
        return *this;
    }
}cin;

const int mod=998244353;
const int Phi=mod-1;
int p,k,ans;

int ksm(int a,int b)
{
    b%=Phi;
    int res=1;
    while(b)
    {
        if(b&1)
            res=1ll*res*a%mod;
        a=1ll*a*a%mod;
        b>>=1;
    }
    return res;
}

signed main()
{
    //freopen("coin.in","r",stdin);
    //freopen("coin.out","w",stdout);
    cin>>p>>k;
    if(p==1){printf("%lld\n",k); return 0;}
    p=ksm(p,mod-2);
    ans=(1-ksm(p,k+1)+mod)%mod;
    ans=1ll*ans*ksm(1-p+mod,mod-2)%mod;
    ans=(ans-1+mod)%mod;
    printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值