跳到正文

题解

题解:CF107B Basketball Team

ZNYJuno约 2 分钟
原文
文章信息与版本

文章信息

收录
2026年1月30日
更新
2026年1月30日
篇幅
约 2 分钟
题目
CF107B

版本

  1. v1当前

解题思路大概如下: 问题转化:已知第一次抽到h盒球,求后续n-1次中至少再抽到一次h盒球的概率

关键公式: 概率 = 1 - (从其他盒子取n-1个球的概率)

计算步骤:

总球数S = 所有盒子球数之和

其他盒子球数T = S - h盒球数

概率 = 1 - [T×(T-1)×…×(T-n+2)] / [(S-1)×(S-2)×…×(S-n+1)]

特殊情况:

如果S < n:输出-1(球不够)

如果n = 1:输出0(没有后续抽取)

如果n-1 > T:输出1(必须再从h盒取球)

C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n, m, h;
    cin >> n >> m >> h;
    vector<int> s(m);
    int total = 0;

    for (int i = 0; i < m; i++)
    {
        cin >> s[i];
        total += s[i];
    }

    if (total < n)
    {
        cout << -1;
        return 0;
    }

    if (n == 1)
    {
        cout << 0;
        return 0;
    }

    int sh = s[h - 1];
    int t = total - sh;

    if (n - 1 > t)
    {
        cout << 1;
        return 0;
    }

    double p = 1.0;
    for (int i = 0; i < n - 1; i++) p *= (t - i) * 1.0 / (total - 1 - i);

    cout << fixed << setprecision(6) << 1 - p;
    return 0;
}
原文