---
# System prepended metadata

title: What is the Probability!!(UVA10056)
tags: [CPE一顆星必考題, CPE 一顆星]

---

# What is the Probability!!(UVA10056)

## [程式繳交區](https://hackmd.io/@Renektonn/ryuRZ1AwJe/edit)
## 題目
[點我](https://onlinejudge.org/external/100/10056.pdf)

## 解題網站
[UVA](http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=997)
[ZJ](https://zerojudge.tw/ShowProblem?problemid=e510)

## 想法

$\frac{1}{6}+\frac{5}{6}\times \frac{5}{6}\times \frac{1}{6} + ...$


$a1\times \frac{1-r^n}{1-r}, r < 1,n\to\infty$

$=\frac{1}{6}\times \frac{1-(\frac{25}{36})^n}{1-\frac{25}{36}}, r < 1,n\to\infty$

$=p\times \frac{1}{1-r}, r < 1$

$=\frac{1}{6}\times \frac{36}{11}$

$=\frac{6}{11}$


## 需要推導的演算法
```
1. 輸入s
2. 重複s次
2.1 輸入n,p,player
2.2 r = (1-p)^n
2.3 輸出(p/(1-r))*(1-p)^(player-1)
```

## 上述演算法的程式碼
```cpp=
#include <bits/stdc++.h>
using namespace std;

int main() {
    int s;
    cin >> s;
    while (s--) {
        int n, player;
        double p;
        cin >> n >> p >> player;
        if (p == 0) {
            printf("%.4f\n", 0.0);
            continue;
        }
        double r = pow(1 - p, n);
        double ans = p / (1 - r) * pow(1 - p, player - 1);
        printf("%.4f\n", ans);
    }
    return 0;
}
```
:::info
此題重點在於推導，不在於寫程式，若僅為了CPE考試，可將它背起來
:::
