There are n black balls and m white balls in the big box.
Now, DZY starts to randomly pick out the balls one by one. It forms a sequence S . If at the i -th operation, DZY takes out the black ball, S i =1 , otherwise S i =0 .
DZY wants to know the expected times that '01' occurs in S .
The input consists several test cases. ( TestCase≤150 )
The first line contains two integers, n , m(1≤n,m≤12)
For each case, output the corresponding result, the format is p/q ( p and q are coprime)
1 1 2 3
1/2 6/5HintCase 1: S='01' or S='10', so the expected times = 1/2 = 1/2 Case 2: S='00011' or S='00101' or S='00110' or S='01001' or S='01010' or S='01100' or S='10001' or S='10010' or S='10100' or S='11000', so the expected times = (1+2+1+2+2+1+1+1+1+0)/10 = 12/10 = 6/5
这次学乖,开始DP乱搞
其实还有更简单的敲公式法,,不过我就直接把打表程序交了
next_permunation 这种暴力也是可以的??
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Forpiter(x) for(int &p=iter[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (100) typedef long long ll; ll mul(ll a,ll b){return (a*b)%F;} ll add(ll a,ll b){return (a+b)%F;} ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;} void upd(ll &a,ll b){a=(a%F+b%F)%F;} ll gcd(ll a,ll b){ if (b==0) return a; return gcd(b,a%b); } class fenshu { public: ll a,b; //分母a 分子b fenshu(ll _b,ll _a):a(_a),b(_b){} fenshu():a(1),b(0){} void relax() { int t=gcd(a,b); a/=t,b/=t; } friend fenshu operator*(fenshu a,fenshu b){fenshu c=fenshu(a.b*b.b,a.a*b.a);c.relax();return c;} friend fenshu operator+(fenshu a,fenshu b){fenshu c=fenshu(a.a*b.b+a.b*b.a,a.a*b.a);c.relax();return c;} void pri() { cout<<b<<'/'<<a; } }f[MAXN][MAXN][2]; int n,m; void turn_out(int n,int m) { fenshu t=f[n][m][0]*fenshu(m,n)+f[n][m][1]*fenshu(n-m,n); t.pri(); } int main() { // freopen("a.in","r",stdin); n=24,m=12; Fork(i,2,n) Rep(j,i+1) { int k=0; f[i][j][k]=f[i-1][j-1][0]*fenshu(j-1,i-1)+f[i-1][j-1][1]*fenshu(i-j,i-1); k=1; f[i][j][k]=(fenshu(1,1)+f[i-1][j][0])*fenshu(j,i-1)+f[i-1][j][1]*fenshu(i-1-j,i-1); } /* For(i,24) { Rep(j,min(i+1,13)) { turn_out(i,j);cout<<' '; } cout<<endl; } */ while(scanf("%d%d",&n,&m)==2) { n+=m; fenshu t=f[n][m][0]*fenshu(m,n)+f[n][m][1]*fenshu(n-m,n); t.pri();cout<<endl; } return 0; }