POJ3682 概率DP

King Arthur's Birthday Celebration
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3575 Accepted: 1130

Description

King Arthur is an narcissist who intends to spare no coins to celebrate his coming K-th birthday. The luxurious celebration will start on his birthday and King Arthur decides to let fate tell when to stop it. Every day he will toss a coin which has probability p that it comes up heads and 1-p up tails. The celebration will be on going until the coin has come up heads for K times. Moreover, the king also decides to spend 1 thousand coins on the F[i]rst day's celebration, 3 thousand coins on the second day's, 5 thousand coins on the third day's ... The cost of next day will always be 2 thousand coins more than the previous one's. Can you tell the minister how many days the celebration is expected to last and how many coins the celebration is expected to cost?

Input

The input consists of several test cases. 
For every case, there is a line with an integer K ( 0 < K ≤ 1000 ) and a real number p (0.1 ≤ p ≤ 1). 
Input ends with a single zero.

Output

For each case, print two number -- the expected number of days and the expected number of coins (in thousand), with the fraction rounded to 3 decimal places.

Sample Input

1 1
1 0.5
0

Sample Output

1.000 1.000
2.000 6.000

Source

题意: 有一个富豪,他决定每天撒钱,并且抛硬币,第一天1块钱,第二天3块钱,第三天5块,直到他抛到硬币向上的数量为K。 求天数期望和钱期望。

思路

C[i]表示掷出了i枚正面朝上的硬币的期望次数,F[i]表示掷出了i枚正面朝上的硬币的期望费用。

C[i] = pC[i]-1 + (1-p)C[i] + 1   1表示抛出了第i枚硬币用去1次,如果是正面那么要到i枚正面硬币只需要再抛出C[i]-1次,如果是反面还需要再抛C[i]次。

F[i] = p(F[i]-1 + 2 * (C[i]-1+1) -1) + (1-p)(F[i] + 2 * (C[i]+1) -1)    第i枚硬币抛出,如果是正面,那么现在抛出的这枚就是第C[i]-1 + 1 枚,如果是反面,现在抛出的这枚就是第C[i] + 1 枚。

PS:C[i],F[i]的转移方程都需要移项。

代码:

 1 #include"bits/stdc++.h"
 2 
 3 #define db double
 4 #define ll long long
 5 #define vl vector<ll>
 6 #define ci(x) scanf("%d",&x)
 7 #define cd(x) scanf("%lf",&x)
 8 #define cl(x) scanf("%lld",&x)
 9 #define pi(x) printf("%d\n",x)
10 #define pd(x) printf("%f\n",x)
11 #define pl(x) printf("%lld\n",x)
12 #define rep(i, n) for(int i=0;i<n;i++)
13 using namespace std;
14 const int N   = 1e6 + 5;
15 const int mod = 1e9 + 7;
16 const int MOD = 998244353;
17 const db  PI  = acos(-1.0);
18 const db  eps = 1e-10;
19 const ll INF = 0x3fffffffffffffff;
20 
21 db c[N];
22 db f[N];
23 int n;
24 db p;
25 int main()
26 {
27     while(scanf("%d%lf",&n,&p)==2&&n)
28     {
29         c[0]=f[0]=0;
30         for(int i=1;i<=n;i++) c[i]=c[i-1]+1/p;
31         for(int i=1;i<=n;i++) f[i]=(p*(f[i-1]+2*(c[i-1]+1)-1)+(1-p)*(2*(c[i]+1)-1))/p;
32         printf("%.3f %.3f\n",c[n],f[n]);
33     }
34     return 0;
35 }

 

 

转载于:https://www.cnblogs.com/mj-liylho/p/9536125.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值