Problem description
There's a box.NA black ball andmA white ball. Now Dzy randomly takes a ball out of the box each time and takesn+m Times, just take it out. Dzy used this strange method to generate a random 01-stringS[1? (n+m)] 。 If DzyIThe ball that was taken out of the shot is black, soS[i]=1 , if it's white, then S[i]=0. Dzy now wants to know the number of times that ' 01 ' appears in the S string.
Enter a description
Input has multiple sets of test data. (TesTCase≤ ) two integers per line, n, m(1≤n,m≤)
Output description
For each test data, output a line of answers in the form p/q(p,q coprime).
Input sample
1 12 3
Output sample
1/26/5
Hint
Case 1:s= ' or s= ' 10 ', so the desired number = 1/2case 2:s= ' 00011 ' or s= ' 00101 ' or s= ' 00110 ' or s= ' 01001 ' or s= ' 01010 ' or s= ' 01100 ' O R s= ' 10001 ' or s= ' 10010 ' or s= ' 10100 ' or s= ' 11000 ', so the desired number = (1+2+1+2+2+1+1+1+1+0)/10 = 12/10 = 6/5
*************************************************************************************************
The official explanation of the subject is mathematics:
The second position appears 0, the first i+1 bit appears 1, this probability is: (m/(N+M)) * (n/(n+m-1)), 0 of the location can appear in the first to the penultimate, so the accumulation (n+m-1), the formula is n*m/(N+M);
That's not what I'm doing, combinatorial math:
At first, all 1 hair is at the far left, 0 on the far right, there are up to n or M 01 strings, (the number is the smaller one), the number of enumeration strings from 1 to the maximum, when there are 1
01 Strings of Time, from N 1 to take a blank, to insert 0, from M 0 can choose 1 0 in neutral, you can choose two to choose M, so for each 1 have m-type interpolation, C (n,1) *c (m,1) * *;
When there are two 01 strings, take 1,c (n,2), take 0, the first 1 has 1 to m-1 species interpolation, the second 1 has the remaining interpolation method, the combination number formula, C (m,2); C (n,2) *c (m,2) * *;
Keep it up.
The total number of permutations is C (n+m,n);
Compare, numerator, get the answer;
The code is as follows:
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <string>5#include <cstring>6 #definell Long Long7 using namespacestd;8 intn,m;9 Long Longk,t;Ten Long LongZuhe (ll n,ll x) One { All di=1, gao=1, ans=1; - for(LL i=1; i<=x;i++) - { thedi*=N; -n--; -gao*=i; - } + returndi/Gao; - } + voidYuefen (ll a,ll b) A { at for(LL i=b;i>=1; i--) - { - if(a%i==0&&b%i==0) - { -k=a/i; -t=b/i; in return ; - } to } + } - intMain () the { * //freopen ("in", "R", stdin); $ //freopen ("Out.out", "w", stdout);Panax Notoginseng while(SCANF ("%d%d", &n,&m)! =EOF) - { thek=0; +t=0; A for(LL i=1; i<=n;i++) the { +ll temp=0; - if(i>M) $ Break; $T+=i*zuhe (n,i) *Zuhe (m,i); - } -K=zuhe (n+m,m); the Yuefen (k,t); -printf"%i64d/%i64d\n", t,k);Wuyi } the return 0; -}View Code
Best coder #35 -01< Combinatorial Math | | Probabilistic Mathematics >