爬树的甲壳虫

这道题主要考察:数学期望费马小定理逆元

期望性质

费马小定理求逆元

有了以上知识点做铺垫,我们就可以进行如下推导啦!

推导:

根据期望的定义,我们需要先定义两个变量来方便进行数学公式的推导

期望定义:离散型随机变量,期望值就等于将离散变量的每个可能值乘以其概率,然后将所有乘积相加

因为我们这里是离散型的数据,所以可以用上述定义。

爬树的甲壳虫推导1

爬树的甲壳虫推导2

付上我的CODE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
const int MOD = 998244353;

int n, res;

int qmi(int a, int b) //逆元
{
int res = 1;
while(b)
{
if(b & 1) res = (LL)res * a % MOD;
a = (LL)a * a % MOD;
b >>= 1;
}
return res;
}

int main()
{
scanf("%d", &n);

while(n -- )
{
int a, b;
scanf("%d%d", &a, &b);

res = (res + 1LL) * b % MOD * qmi(b - a, MOD - 2) % MOD; //推导公式
}

cout << res;
}