【uva 11021】Tribbles

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


題意:毛球族死後可以變成0~n-1個毛球,概率爲P0~Pn-1,原本有k個毛球,問m天后毛球族滅絕的概率

解題思路:每個個體是獨立的,所以只需先求一個毛球在m天后滅絕的概率,將毛球在1天之後滅絕的概率,2天之後滅絕的概率,3天之後.。。。。推出m天后滅絕的概率。

code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
using namespace std;

int main()
{
    int N;
    scanf("%d",&N);
    double  p[1005];
    double  mp[1005];
    int n,m,k;
    double ans;
    for(int cnt=1;cnt<=N;cnt++)
    {
        ans=0.0;
        scanf("%d%d%d",&n,&k,&m);
        for(int i=0;i<n;i++){
            scanf("%lf",&p[i]);
        }
        if(!k)
           printf("Case #%d: 1.0000000\n",cnt);
        else if(!m)
           printf("Case #%d: 0.0000000\n",cnt);
        else{
            memset(mp,0,sizeof(mp));
            mp[1]=p[0];
            for(int i=2;i<=m;i++){
                for(int j=0;j<n;j++){
                    mp[i]+=(double)(p[j]*pow(mp[i-1],j));
                }
            }
            ans=pow(mp[m],k);
            printf("Case #%d: %.7lf\n",cnt,ans);
        }
    }
    return 0;
}















發佈了68 篇原創文章 · 獲贊 9 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章