The expected (DP + probability)

Source: Internet
Author: User

Description

Problem
Expect CT the expected
Input:
Standard Input

Output:Standard output

 

Some mathematicalbackground.This problem asks you to compute the expected value of arandom variable. If you haven't seen those before, the simple definitions areas follows.Random VariableIs a variable that can have one of severalvalues, each with a certain probability. the probabilities of each possiblevalue are positive and add up to one.Expected ValueOf a randomvariable is simply the sum of all its possible values, each multiplied by thecorresponding probability. (There are some more complicated, more generaldefinitions, but you won't need them now .) for example, the value of a fair, 6-sided die is a random variable that has 6 possible values (from 1 to 6), eachwith a probability 1/6 . Its expected value is 1/6 + 2/6 +... + 6/6 = 3.5 . Now the problem.

I like to play solitaire. eachtime I play a game, I have probabilityPOf solving it and Probability (1- P) Of failing. The game keeps statistics of all my games-What percentage ofgames I have won. If I simply keep playing for a long time, this percentagewill always hover somewhere around P* 100% . But I want more.

Here is my plan. every day, Iwill play a game of solitaire. if I win, I'll go to sleep happy until the next day. if I lose, I'll keep playing until the fraction of games I have won todaybecomes largerP. At this point, I'll declare vicep and go tosleep. As you can see, at the end of each day, I'm guaranteed to always keep mystatistics above the expected P* 100% . I will have beatenmathematics!

If your intuition is telling youthat something here must break, then you are right. I can't keep doing thisforever because there is a limit on the number of games I can play in one day. let's say that I can play at mostNGames in one day. How many days canISecondary CTTo be able to continue with my clever plan before it fails? Note that the answer is always at least 1 because it takes me a whole day ofplaying to reach a failure.

Input

The first line of input gives thenumber of cases,N.NTest Cases Follow. Each one is a linecontainingP(As a fraction) andN.

1 ≤N≤ 3000, 0 ≤P<1,

The denominatorPWill be at most 1000,

1 ≤N≤ 100.

Output

For each test case, print a lineof the form "case #X:Y", WhereYIs theexpected Number of days, rounded down to the nearest integer. The answer willalways be at most 1000 and will never be within 0.001 of a round-off errorcase.

Sample input output for sample input

4
1/2 1
1/2 2
0/1 10
1/2 3
Case #1: 2
Case #2: 2
Case #3: 1
Case #4: 2

Problemsetter: Igor naverniouk

Special thanks: Frank Chu

Q: You play cards every night. If you win a game, you go to bed. If you lose and continue playing, the probability of winning each game is P. You will always play until the number of winning games exceeds p, there are only N disks per night. I ask you how many nights you will play on average.

Idea: first set Q to the probability that I will no longer play after playing for only one night. Set d [I] [J] to indicate that the winning ratio in the previous I game cannot exceed p, if I wins the probability of J, then there is a full probability formula,

When J/I <= P, d [I] [J] = d [I-1] [J] * (1-p) + d [I-1] [J-1] * P, d [0] [0] = 1, D [0] [1] = 0, then d [N] [0] + d [N] [1] +... is the probability Q,

Then we will calculate the mathematical expectation of the total number of days X, and discuss when x = 1, x = 2 .... according to EX = q + 2 * q (1-Q) + 3 * Q * (1-Q) ^ 2... release EX = 1/Q

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 105;int main() {int t, cas = 1;scanf("%d", &t);while (t--) {int n, a, b;double d[maxn][maxn], p;scanf("%d/%d%d", &a, &b, &n);p = (double) a / b;memset(d, 0, sizeof(d));d[0][0] = 1.0, d[0][1] = 0.0;for (int i = 1; i <= n; i++)for (int j = 0; j * b <= a * i; j++) {d[i][j] = d[i-1][j] * (1-p);if (j)d[i][j] += d[i-1][j-1] * p;}double Q = 0.0;for (int j = 0; j*b <= a*n; j++)Q += d[n][j];printf("Case #%d: %d\n", cas++, (int)(1/Q));}return 0;}


The expected (DP + probability)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.