题意:
题目链接:https://www.51nod.com/onlineJudge/submitDetail.html#!judgeId=223971
每次生成一个[0,n)的随机数,如果这个随机数和给出的m个数字中的其中一个数字相等,那么就停止生成随机数,否则继续生成,求出所有生成的数的和的期望。
思路:
求期望的一种套路,可以列出一个关于期望E的方程,然后求解方程得出E的表达式。
对这道题来说,可以列出方程:
其中pi就是数字i被选中的概率,flag只有两个值0和1,如果是0表示i
是属于m个数之中,否则为1。
解方程就可以得到E的表达式:
所以,这道题要求的期望其实和m个数是多少没有关系。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
int main() {
LL n, m;
scanf("%lld%lld", &n, &m);
printf("%.6f\n", n * 1.0 * (n - 1) / 2 / m);
return 0;
}