/*
exp(x) return a double, so we have to convert its datatype to long double
long double: printf("%Lf"), scanf("%LF")
Run time: 0.056
*/
#include <stdio.h>
#include <math.h>
long double compute(int m, int n);
int main()
{
int p, q, r, s;
long double tmp1, tmp2, result;
while (scanf("%d%d%d%d", &p, &q, &r, &s) == 4)
{
if (2*q > p)
q = p - q;
if (2*s > r)
s = r - s;
if (!q) tmp1 = 0;
else tmp1 = compute(p, q);
if (!s) tmp2 = 0;
else tmp2 = compute(r, s);
result = exp(tmp1-tmp2);
printf("%.5Lf\n", result);
}
return 0;
}
long double compute(int m, int n)
{
int i;
long double tmp = 0;
for ( i = 1; i <= m; i++)
tmp += log(i);
for ( i = 1; i <= n; i++)
tmp -= log(i);
for ( i = 1; i <= (m-n); i++)
tmp -= log(i);
return tmp;
}
Glenn's ACM code
什麼事都盡全力去做吧!!
2009年5月9日 星期六
Q10375: Choose and divide
訂閱:
文章 (Atom)