Problem Description
MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly one of her friends HZ took some (N) strawberries which MMM likes very much to decorate the cake (of course they also eat strawberries, not just for decoration). HZ is in charge of the decoration, and he thinks that it's not a big deal that he put the strawberries on the cake randomly one by one. After that, MMM would cut the cake into M pieces of sector with equal size and shape (the last one came to the party will have no cake to eat), and choose one piece first. MMM wants to know the probability that she can get all N strawberries, can you help her? As the cake is so big, all strawberries on it could be treat as points.
Input
First line is the integer T, which means there are T cases.
For each case, two integers M, N indicate the number of her friends and the number of strawberry.
(2 < M, N <= 20, T <= 400)
For each case, two integers M, N indicate the number of her friends and the number of strawberry.
(2 < M, N <= 20, T <= 400)
Output
As the probability could be very small, you should output the probability in the form of a fraction in lowest terms. For each case, output the probability in a single line. Please see the sample for more details.
Sample Input
2 3 3 3 4
Sample Output
1/3 4/27
题意:
在一块蛋糕上随机撒m 个草莓,把它平均分成 n 块,求出得到的一块蛋糕有所有草莓的概率为多少。(2<m,n<=20)
思路:
所得到的蛋糕必然有一个草莓,则剩下的(n-1)个草莓的分布在这一块上的概率是1/(n的(m-1) 次方,然而固定的那个草莓会有m 个选择。所以公式为 m/ n的m-1次方;
但是将m = 20, n = 20 代进公式会发现分母爆 long long 了,所以需要使用高精度。
使用的技巧,将m平分,分别做幂运算,和m 约简,再将两部分相乘。
CODE:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=0xfffffff;
typedef long long ll;
using namespace std;
ll Pow(ll n, int m)
{
ll s = 1;
for(int i=0; i<m; i++){
s *= n;
}
return s;
}
ll gcd(ll x, ll y)
{
return y == 0 ? x : gcd(y, x % y);
}
void cal(ll aa,ll bb)
{
int a[20], b[20], ans[40];
int la=0, lb=0;
while(aa){
a[la++] = aa%10;
aa /= 10;
}
while(bb){
b[lb++] = bb%10;
bb /= 10;
}
memset(ans, 0, sizeof(ans));
for(int i=0; i<la; i++){
for(int j=0; j<lb; j++)
ans[i+j] += a[i] * b[j];
}
int c = 0;
for(int i=0; i<la+lb; i++){
int t = ans[i];
ans[i] = (ans[i] + c) % 10;
c = (t + c) / 10;
}
int i=39;
while(ans[i] == 0) i--;
while(i>=0){
printf("%d", ans[i]);
i--;
}
printf("\n");
}
int main()
{
// freopen("in", "r", stdin);
int T;
scanf("%d", &T);
while(T--){
ll n, m;
scanf("%I64d %I64d", &n, &m);
int m1=(m-1)/2;
int m2=m-1-m1;
ll n1 = Pow(n, m1);
ll n2 = Pow(n, m2);
ll t = gcd(n1, m);
n1 /= t; m /= t;
t = gcd(n2, m);
m /=t;
n2 /= t;
printf("%I64d/", m);
cal(n1, n2);
}
return 0;
}