CSUOJ 2019: Fleecing the Raffle 暴力求解

题目链接

Description

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

There will be several test cases. Each case 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.
Sample Input

3 2
23 5

Sample Output

0.6
0.45049857550

Hint
Source

NCPC2016
题意: 现有一种抽奖活动,箱子内已经有n张票 不放回的抽奖p次 你可以往箱子内放任意多张票。 问你被抽中一次的概率最大是多少。

假设放入a张票概率公式就是
a*(n+1-p到 n+a-p的连乘)/(n+1到n+a的连乘) n和p最多只有1e6
那么a从1枚举到1e6差不多就行了。 具体证明不会。。。 求大神指教。

#include 
#include 
#include 
#include 
#include
using namespace std;
typedef long long LL;
const int MAX = 365 + 10;
LL price[MAX];
int  main() {
    double n,p;
    while(cin>>n>>p) {
        double cnt;
        double ans=cnt=p/(1+n);
        for(int i=2; i<=1000000; i++) {
            cnt*=i;
            cnt/=(i-1);
            cnt/=(n+i);
            cnt*=(n+i-p);
            ans=max(cnt,ans);
        }
        printf("%.6f\n", ans);
    }
}

你可能感兴趣的:(暴力求解)