题目
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105318#problem/B
题目来源:2016寒假第二次高年级训练
简要题意:求长为 n 的绳子,切成
m−1 段后最短的一段的期望长度。
题解
开始看的时候无从下手,看了其他人的代码也无法理解,知道后来看了某题解。
令 Ai 为最短的一段为 i 的概率,
Bi 为最短一段长度至少为 i 的概率。则有:
1⩽i⩽n/m Ai=Bi−Bi+1
An/m+1=Bn/m+1=0
E=∑i=1n/mAi⋅i=∑i=1n/mBi
于是只要求出 Bi 累加就行了。
使用隔板法可知, Bi=Cm−1n−(i−1)m−1/Cm−1n−1=∏j=1m−1n−j−(i−1)mn−j于是两层循环求出结果即可。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
int main()
{
int t, n, m;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &m);
double ans = 0.0;
for (int i = 1; i <= n/m; i++) {
double p = 1.0;
for (int j = 1; j < m; j++) {
p *= double(n-j - m*(i-1)) / (n-j);
}
ans += p;
}
printf("%.5f\n", ans);
}
return 0;
}