Videogame Probability

You have just joined the illustrious Ewokin guild (group of players) in your favorite video game. This guild is known for attempting the hardest raids the instant that they are released. For these new raids you need the best gear (items) in order to have a viable chance of success. With your skills as a programmer, you have managed to determine the current drop rates for different item types in the game (drop rate refers to the probability of catching an item when the item is dropped). Now you need to know how likely it is that you will gather your gear (items) and bring your guild one step closer to success.

The Problem:

Given the number of different item types in a game, how many of each item type you need, the probability of obtaining (catching) each item type when it drops, and the maximum number of possible attempts you have for catching the items, determine the probability that you will obtain the desired number of each item type.

The Input:

The first input line contains a positive integer, n, indicating the number of test cases to process. Each test case starts with an integer, g (1 ≤ g ≤ 50), indicating the number of different item types in the game. The following g input lines provide the information about the different item types. Each such line contains two values: an integer, c (0 ≤ c ≤ 50), representing how many of this item type is needed, and a floating point number, p (0.0 ≤ p ≤ 1.0), representing the probability of catching this item type when it drops. The last input line for a test case contains an integer, a (0 ≤ a ≤ 10000), representing the maximum number of attempts you have for catching the items. Note that this last input represents the total number of attempts and not the attempts for each item type. An attempt can, of course, be used (applied) to obtain any item type.

The Output:

For each test case, print a floating point number on a line by itself, representing the probability that you will obtain the desired number of each item type. Output the results to 3 decimal places, rounded to the nearest thousandth (e.g., 0.0113 should round to 0.011, 0.0115 should round to 0.012, and 0.0117 should round to 0.012).

样例输入
4
2
3 0.5
3 0.3
20
4
2 0.75
1 0.01
2 1.0
3 0.8
25
2
1 0.5
1 0.3
2
2
50 0.4
50 0.3
250
样例输出
0.816
0.153
0.150
0.036
#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define vi vector<int>
#define vc vector<char>
#define pii pair<int,int>
#define pll pair<long,long>
#define pil pair<int,long>
#define pli pair<long,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define pr(x) cout<<#x<<": "<<x<<endl

using namespace std;

const int N = 1e4 + 10, M = 55;
int tot, t, a, num[M];
double p[N][M * M], P[M], kp[M * M];

int main() {
    t = qr();
    while (t--) {
        int g = qr();
        repi(i, 1, g)si(num[i]), sd(P[i]);
        si(a);
        tot = 0;
        repi(i, 1, g)repi(j, 1, num[i]) kp[++tot] = P[i];
        if (a < tot) {
            puts("0.000");
            continue;
        }
        kp[tot + 1] = 0, p[1][1] = 1;
        repi(j, 1, a - tot + 1)repi(i, 1, tot + 1)
                if (!(i == 1 && j == 1))
                    p[j][i] = p[j][i - 1] * kp[i - 1], p[j][i] += p[j - 1][i] * (1.0 - kp[i]);
        printf("%.3lf\n", p[a - tot + 1][tot + 1]);
    }
    return 0;
}
发布了360 篇原创文章 · 获赞 28 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45323960/article/details/105350811
今日推荐