UVA10056-What is the Probability? (Probability)
UVA10056-What is the Probability? (Probability)
Question Link
N people play the game until the game ends after a person wins. Otherwise, the game continues from the first to the nth round, the probability of everyone's victory is p, and the probability of my personal victory is asked.
Solution: If I personally want to win, I may win in the first round or in the second round. Then the probability of victory is q = 1-p; probability = q ^ (I-1 )? P? (Q ^ n) ^ 0 + q ^ (I-1 )? P? (Q ^ n) ^ 1 +... + q ^ (I-1 )? P? (Q ^ n) ^ k (to infinity) to p? Q ^ (I-1) is proposed. The formula in the middle can be obtained by using the summation function of the power function. Then the final formula is p? Q ^ (I-1)/(1-q ^ n). However, if p is 0, it must be determined.
Code:
#include
#include
#include
const double esp = 1e-9;int main () { int T; scanf ("%d", &T); int n, I; double p; while (T--) { scanf ("%d%lf%d", &n, &p, &I); if (p < esp) { printf ("0.0000\n"); continue; } double q = 1.0 - p; double ans = p * pow(q, I - 1)/(1.0 - pow(q, n)); printf ("%.4lf\n", ans); } return 0;}