HDU 5810 Balls ans Boxes(二項分佈)

Problem Description
Mr. Chopsticks is interested in random phenomena, and he conducts an experiment to study randomness. In the experiment, he throws n balls into m boxes in such a manner that each ball has equal probability of going to each boxes. After the experiment, he calculated the statistical variance V as
V=∑mi=1(Xi−X¯)2m

where Xi is the number of balls in the ith box, and X¯ is the average number of balls in a box.
Your task is to find out the expected value of V.

Input
The input contains multiple test cases. Each case contains two integers n and m (1 <= n, m <= 1000 000 000) in a line.
The input is terminated by n = m = 0.

Output
For each case, output the result as A/B in a line, where A/B should be an irreducible fraction. Let B=1 if the result is an integer.

Sample Input
2 1
2 2
0 0

Sample Output
0/1
1/2

Hint

In the second sample, there are four possible outcomes, two outcomes with V = 0 and two outcomes with V = 1.

相當於扔一個球到m個盒子,重複n次。
直接套二項分佈公式就出來了:

樣本方差的期望 = 總體方差

公式: np(1-p)

此處p = 1/m;

ac代碼:

#include <bits/stdc++.h>

using namespace std;

#define rep(i,a,n) for(int i = (a); i < (n); i++)
#define per(i,a,n) for(int i = (n)-1; i >= (a); i--)
#define clr(arr,val) memset(arr, val, sizeof(arr))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pi acos(-1)
typedef pair<int, int> pii;
typedef long long LL;
const int maxn = 1006;
const double eps = 1e-8;
const int mod = 1000000007;

int main(int argc, char const *argv[]) {
    LL n, m;
    while (cin >> n >> m && n && m) {
        LL g = __gcd(n*(m-1), m*m);
        cout << n*(m-1)/g << "/" << m*m/g << endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章