POJ3682 概率DP
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]的转移方程都需要移项。
代码:
#include"bits/stdc++.h" #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, n) for(int i=0;i<n;i++)
using namespace std;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const ll INF = 0x3fffffffffffffff; db c[N];
db f[N];
int n;
db p;
int main()
{
while(scanf("%d%lf",&n,&p)==&&n)
{
c[]=f[]=;
for(int i=;i<=n;i++) c[i]=c[i-]+/p;
for(int i=;i<=n;i++) f[i]=(p*(f[i-]+*(c[i-]+)-)+(-p)*(*(c[i]+)-))/p;
printf("%.3f %.3f\n",c[n],f[n]);
}
return ;
}
POJ3682 概率DP的更多相关文章
- Codeforces 28C [概率DP]
/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...
- HDU 4405 Aeroplane chess (概率DP)
题意:你从0开始,要跳到 n 这个位置,如果当前位置是一个飞行点,那么可以跳过去,要不然就只能掷骰子,问你要掷的次数数学期望,到达或者超过n. 析:概率DP,dp[i] 表示从 i 这个位置到达 n ...
- POJ 2096 Collecting Bugs (概率DP)
题意:给定 n 类bug,和 s 个子系统,每天可以找出一个bug,求找出 n 类型的bug,并且 s 个都至少有一个的期望是多少. 析:应该是一个很简单的概率DP,dp[i][j] 表示已经从 j ...
- POJ 2151 Check the difficulty of problems (概率DP)
题意:ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 ,求每队至少解出一题且冠军队至少解出N道题的概率. 析:概率DP,dp[i][j][k] 表示第 i 个队伍,前 j 个题,解出 ...
- 概率DP light oj 1030
t组数据 n块黄金 到这里就捡起来 出发点1 到n结束 点+位置>n 重掷一次 dp[i] 代表到这里的概率 dp[i]=(dp[i-1]+dp[i-2]... )/6 如果满6个的话 否则 ...
- hdu 4050 2011北京赛区网络赛K 概率dp ***
题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...
- [转]概率DP总结 by kuangbin
概率类题目一直比较弱,准备把kuangbin大师傅总结的这篇题刷一下! 我把下面的代码换成了自己的代码! 原文地址:http://www.cnblogs.com/kuangbin/archive/20 ...
- SGU 422 Fast Typing(概率DP)
题目大意 某人在打字机上打一个字符串,给出了他打每个字符出错的概率 q[i]. 打一个字符需要单位1的时间,删除一个字符也需要单位1的时间.在任意时刻,他可以花 t 的时间检查整个打出来的字符串,并且 ...
- HDU 4050 wolf5x(动态规划-概率DP)
wolf5x Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
随机推荐
- JavaScript写入文件到本地
工作中有时需要通过 JavaScript 保存文件到本地,我们都知道 JavaScript 基于安全的考虑,是不允许直接操作本地文件的.IE 可以通过 VB 插件的方式进行,而 Chrome 和 fi ...
- Struts2_HelloWorld_5
1.设置开发模式: 更换配置文件中的 action name="hello_struts",重新访问会出错,因为配置没有立刻响应文件的即时修改. 需要加上配置 <consta ...
- 转发-react 性能深度探讨
作者:尤雨溪链接:https://www.zhihu.com/question/31809713/answer/53544875来源:知乎 这里面有好几个方面的问题. 1. 原生 DOM 操作 vs. ...
- C#获取农历的日期(转)
//C# 获取农历日期 ///<summary> /// 实例化一个 ChineseLunisolarCalendar ///</summary> private static ...
- 如何用C# 动态创建Access数据库和表?
记得以前要动态的创建Access数据库的mdb文件都是采用DAO,用VC开发,一大堆的API,很是麻烦.而且以前工作中需要全新的access数据库,可以复制数据库,也可以把新的数据库放到资源里面,用新 ...
- cesium 显示视角高度以及鼠标经纬度
HTML中的内容 <div id="cesiumContainer"> <!-- 设置经纬度显示 --> <span style="font ...
- dd-wrt ddns更新失败由于电信提供的ip不是公网ip
由于电信提供的ip地址原来是公网的ip,后来电信通过nat提供一个内网ip,导致ddns更新失败.电话给电信客服10000号,让他们修改回来之后就可以了. 如果ddns更新失败,尤其是原本是正常的,后 ...
- [论文理解]关于ResNet的进一步理解
[论文理解]关于ResNet的理解 这两天回忆起resnet,感觉残差结构还是不怎么理解(可能当时理解了,时间长了忘了吧),重新梳理一下两点,关于resnet结构的思考. 要解决什么问题 论文的一大贡 ...
- UTF8与ANSI互转
在取回的结果中,如果有Unicode字符,用printf来打印的话,则会出现乱码.通过这个方法,可以判断是否为unicode字符,是的话,通过wprintf来打印.1.判断字符串是否为Unicode的 ...
- 第35章 WWDG—窗口看门狗—零死角玩转STM32-F429系列
第35章 WWDG—窗口看门狗 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...