CDOJ 1168 凤神与狗(数论_概率)

7 篇文章 0 订阅

D - 凤神与狗

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
凤神隐居山林,与猫狗为伴。起初,他拥有c只猫和d只狗。每天下午他随机从中选择一只出去游玩并且晚上归来。如果他带的是狗,则第二天早上狗的数量增加w只,否则,猫的数量增加w只。由于凤神特别钟爱狗,某些重要的日子他想带狗出去玩,于是他想知道他在第a天和第b天都带狗出去玩的概率是多少?

Input

第一行包含5个整数:c(0<c≤1000000),d(0<d≤1000000),w(0<w≤1000000),a(0<a<1000000),b(a<b≤1000000)

Output

答案用最简分数表示,x/y,x为分子,y为分母。

Sample input and output

Sample Input Sample Output
1 1 1 1 2
1/3
D题答案很简单:
证明:
令f(i,j)表示前i天中,有j天带的是狗的概率

于是此处证出来:对于任意一天,其实,带狗出去的概率是
接下来就是证第a天带狗且第b天带狗的概率,相当于是所有的第b天带狗的情况中,第a天带狗的情况,然后除以总情况。


接下来我们求第a天和第b天都带狗的概率。
这是第i天和第i+2天都带狗的概率。

于是,我们再经计算可发现规律:

所以,答案就有了,我们只需通分,找出分子分母的最大公约数,各自除以最大公约数,答案就出来了。

AC代码:
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <string>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
using namespace std;
/*long long gcd(long long x, long long y)
{
	long long r;
	while (y > 0)
	{
		r = x%y;
		x = y;
		y = r;
	}
	return x;
}*/
long long int gcd(long long int x, long long int y)
{
	while (x != y)
	{
		if (x>y) x = x - y;
		else
			y = y - x;
	}
	return x;
}
int main()
{
	long long c, d, w, a, b;
	scanf("%lld %lld %lld %lld %lld", &c, &d, &w, &a, &b);
	long long x, y, xy;
//	x = d*d + d*(b - a)*w;
//	y = c*c + 2 * c*d + d*d + (c + d)*(b - a)*w;
	x = d*(d + w);
	y = (c + d)*(c + d + w);
//	cout<<x<<"    "<<y<<endl;
	xy = gcd(x, y);
	x /= xy;
	y /= xy;
	printf("%lld/%lld", x, y);
	
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值