1765 谷歌的恐龙

Problem

把整个游戏简化为,每次生成一个[0,n)的随机数,如果这个随机数和给出的m个数字中的其中一个数字相等,那么就停止生成随机数,否则继续生成,求出所有生成的数的和的期望。

Solution

$ E = sum_{i=0}^{n-1} p_i imes(i+[flag_i] imes E), p_i = frac{1}{n}, 如果i不在m个数中, flag_i = 1, 否则为0。 $

化简为$ E = frac{n imes(n-1)}{2 imes m} $

Code

#include<stdio.h>
#include<set>
#include<deque>
#include<iostream>
#include<stack>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
typedef long long ll;
typedef long double ld;
typedef double db;
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int mod=998244353;
int mo(ll a,int p){
    return a>=p?a%p:a;
}
inline int rd() {
    int x = 0, f = 1;
    char ch;
    while (ch < '0' || ch > '9') {
        if (ch == '-')f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return f * x;
}
ll n,m,x;
int main(){
    //io_opt;
    scanf("%lld%lld",&n,&m);
    
    printf("%.6f
",n*(n-1)/2.0/m);
    return 0;
}
原文地址:https://www.cnblogs.com/sz-wcc/p/12336375.html