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 first 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

OJ-ID:
poj-3682

author:
Caution_X

date of submission:
20191031

tags:
math

description modelling:
有一个人每天抛一次硬币,直到抛出了K次正面向上才会停止,第i天的金钱花费是2*i-1,现在输入
K,问花费金钱的数学期望

major steps to solve it:
设E[i],F[i],E[i]表示抛出i次正面向上时的期望天数,F[i]表示第i天的花费金钱数学期望
(1)E[i]=1/p+W[i-1]
(2)F[i] = p*(F[i]-1 + 2 * E[i] -1)/*第i天正好得到正面向上*/ + (1-p)*(F[i] + 2 * (E[i]+1) -1) /*第i天没有得到正面向上*/

AC code:

#include <stdio.h>

double E[];
double F[]; int main()
{
int i,j,n;
double q,p;
while()
{
scanf("%d",&n);
if (n==) break;
scanf("%lf",&p);
E[]=;
F[]=;
for (i=;i<=n;i++)
{
E[i]=/p+E[i-];
F[i]=F[i-]+*E[i-]-*E[i]+(+*E[i])/p;
}
printf("%.3f %.3f\n",E[n],F[n]);
}
return ;
}

最新文章

  1. 11g新特性:Health Monitor Checks
  2. 人工智能起步-反向回馈神经网路算法(BP算法)
  3. 又是一道水题 hdu背包
  4. Delphi 常用函数(数学函数)round、trunc、ceil和floor
  5. console深入理解
  6. Jmeter之分布式测试
  7. Linux 搜某个文件里关键字的上下500行到执行文件里
  8. 《笔记》Apache2 mod_wsgi的配置
  9. Centos7.3_x86_64通过systemctl控制tomcat8.0.46启动和停止
  10. Python学习之路基础篇--01Python的基本常识
  11. [echarts] 横纵数据散点图
  12. SSH服务拒绝了密码
  13. maven3 学习
  14. 记录:TensorFlow 中的 padding 方式
  15. 纯CSS3打造非常炫的加载动画
  16. Python 装饰器笔记
  17. MYSQL-实现分组排序 对比 ORACLE 和SQLserver用 row_number() over(partition by ) 分组排序功能
  18. iOS 图片的存储以及读取和删除
  19. 006服务监控看板Hystrix Dashboard
  20. 解析Linux特殊文件【转】

热门文章

  1. 【GUI】基于V7开发板的裸机和各种RTOS版本的emWin程序模板,支持硬件JPEG,已发布(2019-05-26)
  2. API收藏
  3. 计算机网络知识(TCP连接,TCP/UDP区别,HTTP与HTTPS,Socket原理等等)
  4. java高并发系列 - 第26篇:学会使用JUC中常见的集合,常看看!
  5. Slickflow.NET 开源工作流引擎高级开发(五) -- 引擎和外部事件的交互
  6. arcgis api 4.x for js 图层拓展篇之mapvLayer(附源码下载)
  7. 【转载】Android内存泄漏的8种可能
  8. Kotlin Parameter specified as non-null is null
  9. django models 数据库操作
  10. ramdisk配置、解压、创建rootfs、启动简单分析