洛谷P5104 红包发红包

题目链接:

P5104

题目分析:

题目和(n)是没什么关系的,因为是(n)个人抢,其实不一定抢完

其实很显然……就是求一个连续型随机变量的期望
首先设一个随机变量(X),表示第一个人拿到的钱,那么有分布函数(F(x) = frac{x - a}{b - a} = frac{x - 0}{w - 0} = frac{x}{w})
然后对分布函数求一个导,得到密度函数(f(x) = lim_{Delta x o 0} frac{frac{x + Delta x}{w} - frac{x}{w}}{Delta x} = frac{1}{w})
根据连续型随机变量期望的定义,有(E = int_{0}^{w} x f(x) dx),积分一下发现是(frac{w}{2}),之后就差不多了,每次递推的时候把(w)换成(frac{w}{2})

代码:

#include <bits/stdc++.h>
#define int long long 
using namespace std;
inline int read() {
	int cnt = 0, f = 1; char c = getchar();
	while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
	while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + c - '0'; c = getchar();}
	return cnt * f;
}
int w, n, k;
const int p = (1000000000 + 7);
int ksm(int a, int b, int mod) {
	long long res = 1;
	while (b) {
		if (b & 1) res = (res * a) % mod;
		a = (a * a) % mod;
		b >>= 1;
	}
	return res % mod;
}
signed main() {
	w = read(), n = read(), k = read();
	int inv = ksm(2, k, p);
	int ans = w * ksm(inv, p - 2, p) % p;
	printf("%lld", ans % p);
	return 0;
}
原文地址:https://www.cnblogs.com/kma093/p/11345692.html