24h購物| | PChome| 登入
2013-08-11 17:26:39| 人氣1,562| 回應0 | 上一篇 | 下一篇

[UVA] 11021 - Tribles

推薦 0 收藏 0 轉貼0 訂閱站台

Problem A
Tribbles
Input: Standard Input

Output: Standard Output

GRAVITATION, n.
"The tendency of all bodies to approach one another with a strength
proportion to the quantity of matter they contain -- the quantity of
matter they contain being ascertained by the strength of their tendency
to approach one another. This is a lovely and edifying illustration of
how science, having made A the proof of B, makes B the proof of A."

Ambrose Bierce

You have a population of k Tribbles. This particular species of Tribbles live for exactly one day and then die. Just before death, a single Tribble has the probability Pi of giving birth to i more Tribbles. What is the probability that after m generations, every Tribble will be dead?

Input
The first line of input gives the number of cases, N. N test cases follow. Each one starts with a line containing n (1<=n<=1000), k (0<=k<=1000) and m (0<=m<=1000). The next n lines will give the probabilities P0, P1, ..., Pn-1.

Output
For each test case, output one line containing "Case #x:" followed by the answer, correct up to an absolute or relative error of 10-6.

Sample Input

Sample Output

4
3 1 1
0.33
0.34
0.33
3 1 2
0.33
0.34
0.33
3 1 2
0.5
0.0
0.5
4 2 2
0.5
0.0
0.0
0.5
Case #1: 0.3300000
Case #2: 0.4781370
Case #3: 0.6250000
Case #4: 0.3164062
 

Problemsetter: Igor Naverniouk, EPS

Special Thanks: Joachim Wulff

題目描述:

有一種生物,生完下一代就會滅亡,也有可能不生就滅亡。
根據機率 Pi 產生 i 個子代。假使一開始有 k 隻這種生物,問剛好在第 m 代絕種的機率為何。


題目解法:


一開始的 k 隻,差別只在於次方,因此只先考慮 1 隻起頭。

假設 dp[i] 為只有 1 隻起頭的情況下,在第 i 代絕種的機率。

打算窮舉第一代的情況
dp[i] += p[j]*pow(dp[i-1], j)

即第一代如果根據機率 pj 產生 j 隻的情況下,其中一隻的後代必須存活 i-1 代,

而當 j 越大,絕種的機率當然成指數下降。



#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;

double dp[1005], p[1005];
int main() {
    int testcase, cases = 0;
    int N, K, M;
    int i, j, k;
    scanf("%d", &testcase);
    while(testcase--) {
        int N, K, M;
        scanf("%d %d %d", &N, &K, &M);
        for(i = 0; i < N; i++)
            scanf("%lf", &p[i]);
        dp[0] = 0;
        for(i = 1; i <= M; i++) {
            dp[i] = 0;
            for(j = 0; j < N; j++)
                dp[i] += p[j]*pow(dp[i-1], j);
        }
        printf("Case #%d: %.7lf\n", ++cases, pow(dp[M], K));

    }
    return 0;
}

台長: Morris
人氣(1,562) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][dp] 11176 - Winning Streak
此分類上一篇:[UVA][dp] 10218 - Let's Dance !!!

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文