uva 10056 What is the Probability ? 概率是多少

原题
Probability has always been an integrated part of computer algorithms. Where the deterministic
algorithms have failed to solve a problem in short time, probabilistic algorithms have come to the
rescue. In this problem we are not dealing with any probabilistic algorithm. We will just try to
determine the winning probability of a certain player.
A game is played by throwing a dice like thing (it should not be assumed that it has six sides like
an ordinary dice). If a certain event occurs when a player throws the dice (such as getting a 3, getting
green side on top or whatever) he is declared the winner. There can be N such player. So the first
player will throw the dice, then the second and at last the N-th player and again the first player and
so on. When a player gets the desired event he or she is declared winner and playing stops. You will
have to determine the winning probability of one (The I-th) of these players.
Input
Input will contain an integer S (S ≤ 1000) at first, which indicates how many sets of inputs are there.
The next S lines will contain S sets of inputs. Each line contain an integer N (N ≤ 1000) which denotes
the number players, a floating point number p which indicates the probability of the happening of a
successful event in a single throw (If success means getting 3 then p is the probability of getting 3 in
a single throw. For a normal dice the probability of getting 3 is 1/6), and I (I ≤ N) the serial of the
player whose winning probability is to be determined (Serial no varies from 1 to N). You can assume
that no invalid probability (p) value will be given as input.
Output
For each set of input, output in a single line the probability of the I-th player to win. The output
floating point number will always have four digits after the decimal point as shown in the sample
output.
Sample Input
2
2 0.166666 1
2 0.166666 2
Sample Output
0.5455
0.4545
题目大意
给你n个人,给你一个游戏获胜的概率,再给你一个数x表示第几个人。
问你第x个人获胜的概率是多少,每个人获胜的概率相同,这些人玩这个游戏从第一个人开始轮流进行,进行到第n个人时再次回到第一个人。

#include<iostream>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    double p,ans;
    int n,t,x;
    cin>>t;
    while(t--)
    {
        cin>>n>>p>>x;
        if(p==0)
        cout<<"0.0000"<<endl;
        else
        {
            ans=p*pow((1-p),x-1)*(1/(1-pow((1-p),n)));
            cout<<fixed<<setprecision(4)<<ans<<endl;
        }
    }
    return 0;
}

思路
比如有4个人,游戏获胜的概率是p。
第一个人赢的概率应该是:
第一次玩就赢,概率是p
加上
第一次玩输了,而且剩下的3个都输了,但是第一次轮回来时这个赢了,概率是(1-p)^4*p
加上
前两轮所有人都输时他赢了,概率是(1-p)^8*p
一直这样加下去,计算等比数列求和公式,比如进行了m轮。
得到一个这样的公式
(1-p)^(x-1)p*(1*(1-p^m)/(1-(1-p)^n)
现在对m求极限可知p^m次幂对m求极限等于0
则公式变成
(1-x)^(x-1)p(1/(1-(1-p)^n))
注意
当p等于0的时候要特别判定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值