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 |
证明:
令f(i,j)表示前i天中,有j天带的是狗的概率
于是此处证出来:对于任意一天,其实,带狗出去的概率是
接下来就是证第a天带狗且第b天带狗的概率,相当于是所有的第b天带狗的情况中,第a天带狗的情况,然后除以总情况。
接下来我们求第a天和第b天都带狗的概率。
这是第i天和第i+2天都带狗的概率。
这是第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;
}