Fleecing the Raffle(NCPC 2016 暴力求解)

题目:

A tremendously exciting raffle is being held, with some tremendously exciting prizes being given out. All you have to do to have a chance of being a winner is to put a piece of paper with your name on it in the raffle box. The lucky winners of the p prizes are decided by drawing p names from the box. When a piece of paper with a name has been drawn it is not put back into the box – each person can win at most one prize.

Naturally, it is against the raffle rules to put your name in the box more than once. However, it is only cheating if you are actually caught, and since not even the raffle organizers want to spend time checking all the names in the box, the only way you can get caught is if your name ends up being drawn for more than one of the prizes. This means that cheating and placing your name more than once can sometimes increase your chances of winning a prize.

You know the number of names in the raffle box placed by other people, and the number of prizes that will be given out. By carefully choosing how many times to add your own name to the box, how large can you make your chances of winning a prize (i.e., the probability that your name is drawn exactly once)?

Input:

The input consists of a single line containing two integers n and p (2 ≤ p ≤ n ≤ 106 ), where n is the number of names in the raffle box excluding yours, and p is the number of prizes that will be given away.

Output:

Output a single line containing the maximum possible probability of winning a prize, accurate up to an absolute error of 10-6.

题意:

有n个人,p个获奖名额。游戏规则为每个人往一个盒子中放一张带有自己名字的纸条,然后一次性从中拿出p张这条,这p个就是获奖的人。小明想往里边多放一些带有自己名字的纸条来提高自己中奖的概率,被发现作弊的情况为,抽出的p张纸条中有两条写着“小明”。问在不被发现的情况下,小明通过作弊最高的获奖率为多少。

思路:

嗯,,,,,,在队友的启发下,跟高中数学老师把概率这块的知识要回来后,终于自己推出了公式如下图(字丑不要喷啊):

图中圈出来的两部分的项是相等的。所以枚举a直接上暴力大法就可以了。写完交题,结果TLE,然后在取最大值之前输出了一下答案,发现到达最大后后边的值都是相等的就没有必要计算了,直接break就ok了。

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <string.h>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <map>
#include <malloc.h>
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
typedef long long ll;
double m, p;

int main() {
    cin >> m >> p;
    double ans = 1.0, sum = 0.0;
    for(double k = 1; k <= m; k++) {
        ans = 1.0;
        ans = ans*(k/(m+k));
        for(double i = 0; i < p-1; i++) {
            ans *= (m - i);
            ans /= (m - i + k - 1);
        }
        if(ans > sum)
            sum = ans;
        else
            break;
    }
    sum = sum*p;
    cout << setprecision(8) << sum << endl;
    return 0;
}
/*
样例输入:
3 2
23 5
样例输出:
0.6
0.45049857550
*/
View Code
原文地址:https://www.cnblogs.com/sykline/p/9755702.html