UVA 10169 - Urn-ball Probabilities !(概率 打表)

Problem B

Urn-ball Probabilities!

Input: standard input

Output: standard output

Time Limit: 3 seconds

 

Assume that you have two urns before you. Initially, one urn has one ball and the other urn has two balls and exactly one ball in each urn is red. At this initial stage you are asked to pick up two balls, one from each urn. Then one white ball is added in each urn and you are again asked to pick up one ball from each urn then again one white ball is added in each urn. This process continues for a certain time. Remember that you place the picked ball back to the urn after each pick up. You will have to determine the probability that in any of your pickups both of the picked balls were red and also the probability that all of your picked balls were red after certain steps.

 

Input

The input file contains several lines of inputs. Each line of the input file contains an unsigned integer N (N<1000000) indicating how many times you will pick up. Of course after each pick up an increment in balls occurs as described previously.

 

Output

For each line of input print a single line of output containing a floating point number and an integer. The floating-point number indicates the probability that you have picked up two red balls in at least one of your pick-ups and the second integer denotes how many consecutive zeros are there after decimal point in the probability value that all of your pick ups has both balls as red.

 

Sample Input:

1
2
20

 

Sample Output:

0.500000 0
0.583333 1
0.688850 38

题意:两个罐子,开始一个有一个红球,一个有一个红球一个白球,每次取一个球(放回),然后多放一个白球进去。给定n,求出取n次出现取得两个红球的概率。和取n次都是两个红球的概率的小数点后面的0有几位。

思路:第一个问题:当前次取到两个红球概率等于 p之前没取到两红球 * p这次取到两红球,这样叠乘上去的 pi = (1 - pi - 1) * (1 / (i + 1) * i)。

第二个问题:每次都取到两红球概率不断叠乘上去 pi = pi - 1 * (1 / (1 + i) * i); 位数为 - lg(pi)

要先打表预处理不然会超时。。在处理第二个问题的时候,由于有精度误差,我们可以转化为 0 - lg(pi) = lg1 - lg 1 / ((1 + i) * i = lg(i * (1 + i);这样,注意类型要用longlong 不然100W^2 int是存不下的

代码:

#include <stdio.h>
#include <math.h>
const long long N = 1000005;
long long n, i;
double ans, p, a[N], count;
long long b[N];

int main() {
		count = 0; ans = p = 0;
		for (i = 1; i <= 1000000; i ++) {
			p = 1.0 / (i * (i + 1));
			ans += (1 - ans) * p;
			count += log10(i * (i + 1));
			a[i] = ans; b[i] = (int)count;
		}
	while (~scanf("%lld", &n)) {
		printf("%.6lf %lld\n", a[n], b[n]);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值