POJ3682 King Arthur's Birthday Celebration

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

数学问题 数学期望 递推

水道期望题,防止自己彻底忘掉期望是个啥……

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 using namespace std;
 7 const int mxn=10010;
 8 double f[mxn],g[mxn];
 9 double p;
10 int n;
11 int main(){
12     int i,j;
13     while(scanf("%d",&n)!=EOF && n){
14         scanf("%lf",&p);
15         for(i=1;i<=n;i++){
16             f[i]=1.0/p+f[i-1];
17             g[i]=(g[i-1]+2*(f[i-1]+1)-1)+(1-p)*(2*(f[i]+1)-1)/p;
18         }
19         printf("%.3f %.3f\n",f[n],g[n]);
20     }
21     return 0;
22 }

POJ3682 King Arthur's Birthday Celebration

时间: 2024-07-28 17:20:00

POJ3682 King Arthur's Birthday Celebration的相关文章

POJ3682King Arthur&#39;s Birthday Celebration(数学期望||概率DP)

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 p

HDU 4337 King Arthur&#39;s Knights 找出一条哈密顿回路

n个点m条无向边 输出一条哈密顿回路 #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 155; int n, m; bool mp[N][N]; int S, T, top, Stack[N]; bool vis[N]; void _reverse(int l,int r) { while (l<r) swap(Stack[l++

HDU 4337 King Arthur&amp;#39;s Knights 它输出一个哈密顿电路

n积分m文章无向边 它输出一个哈密顿电路 #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 155; int n, m; bool mp[N][N]; int S, T, top, Stack[N]; bool vis[N]; void _reverse(int l,int r) { while (l<r) swap(Stack[l

poj3682:数学期望,O(1)做法附推导过程

这几天一直在磨蹭这题..第一个答案很容易,但在第二个答案我无法算出来了,于是只好求助于Zayin.Zayin又求助于我们年级里面的一个研究生数学老师..而现在终于算出来了,我看了看,自己也推出来几次了,先看题:) King Arthur's Birthday Celebration Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2921 Accepted: 926 Description King Arthur is an

HDU 5316 Magician(线段树区间合并, 子序列最值 多校2015啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 Problem Description Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an innate talent, gaining it through study and practice, or receiving it from an

HDU 5316 Magician(线段树区间合并入门)

本文纯属原创,转载请注明出处谢谢.http://blog.csdn.net/zip_fan. 题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5316 Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Fantasy magicians usually gain their ability

class-2

在class内部做点事 class FirstClass: # 定义类对象 def setData(self, value): # 定义类方法 self.data = value # self就指这个实例 def display(self): print self.data # self.data: 该实例的data值 # 实例化两个对象 x = FirstClass() y = FirstClass() x.setData("King Arthur") y.setData(3.141

NLTK学习笔记(二):文本、语料资源和WordNet汇总

[TOC] 语料库基本函数表 示例 描述 fileids() 语料库中的文件 fileids([categories]) 对应分类中的语料库文件 categories() 语料库的分类 categories([fileids]) 文件对应的语料库分类 raw(fileids=[f1,f2..],categories=[c1,c2...]) 对应文件和分类中原始内容.参数可以式空 words(fileids=[f1,f2..],categories=[c1,c2...]) 对应文件和分类的词汇.参

python 自然语言处理(二)____获得文本语料和词汇资源

一, 获取文本语料库 一个文本语料库是一大段文本.它通常包含多个单独的文本,但为了处理方便,我们把他们头尾连接起来当做一个文本对待. 1. 古腾堡语料库 nltk包含古腾堡项目(Project Gutenberg)电子文本档案的一小部分文本.要使用该语料库通常需要用Python解释器加载nltk包,然后尝试nltk.corpus.gutenberg.fileids().实例如下: 1 >>> import nltk 2 >>> nltk.corpus.gutenberg