Balls and Boxes
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 69 Accepted Submission(s): 55
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.
ac代码:
/* ***********************************************
Author : AnICoo1
Created Time : 2016-08-09-13.17 Tuesday
File Name : D:\MyCode\2016-8月\2016-8-9.cpp
LANGUAGE : C++
Copyright 2016 clh All Rights Reserved
************************************************ */
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x,y) memset(x,(y),sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int main()
{
ll n,m;
while(scanf("%I64d%I64d",&n,&m)!=EOF,n||m)
{
ll up=n*(m-1),down=m*m;
ll k=gcd(up,down);
if(k==0)
printf("%I64d/%I64d\n",up,down);
else
printf("%I64d/%I64d\n",up/k,down/k);
}
return 0;
}