answer
- From (0,0) to (n, m) is the number of random walk scenario C (n + m, m)
- Then the subject requirements can not pass this line y = x, i.e. not touch y = x + 1 is linear
- We will then consider an illegal route along the y = x + 1 symmetric elapsed, (n, m) is symmetrical point in the past (m-1, n + 1)
- Consider the proof number Catalan, we found go from the origin point of symmetry (m-1, n + 1 ) can be a path symmetric back and comes to an end corresponding to one from the origin (n, m) passing through the y = x the straight-line path
- Therefore, through the y = x is the number of paths from the origin to the end of the path is symmetrical i.e. C (n + 1 + m-1, m-1) = C (n + m, m-1)
- All the answer is C (n + m, m) = C (n + m, m-1)
- The formula can simplify it:
- The common denominator was:
- Finally, make a precision on the ok
Code
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int N=10000,M=N*10; 6 struct num 7 { 8 int shu[N]; 9 friend num operator-(num y,num z) 10 { 11 num x;memset(x.shu,0,sizeof(x.shu)),x.shu[0]=y.shu[0]; 12 for (int i=1;i<=x.shu[0];i++) 13 { 14 x.shu[i]+=y.shu[i]-z.shu[i]; 15 if (x.shu[i]<0) x.shu[i]+=M,x.shu[i+1]--; 16 } 17 while (!x.shu[x.shu[0]]) x.shu[0]--; 18 return x; 19 } 20 friend num operator*(num y,int z) 21 { 22 num x;memset(x.shu,0,sizeof(x.shu)),x.shu[0]=y.shu[0]; 23 for (int i=1;i<=x.shu[0];i++) x.shu[i]+=y.shu[i]*z,x.shu[i+1]+=x.shu[i]/M,x.shu[i]%=M; 24 while (x.shu[x.shu[0]+1]) x.shu[0]++; 25 return x; 26 } 27 }ans; 28 int p[N+5],c[N+5],n,m; 29 void recout(int x,int f) { for(;x!=1;x/=p[x]) c[p[x]]+=f; } 30 num C(int m,int n) 31 { 32 num x;memset(x.shu,0,sizeof(x.shu)),x.shu[0]=x.shu[1]=1,memset(c,0,sizeof(c)); 33 for (int i=n+1;i<=m;i++) recout(i,1); 34 for (int i=1;i<=m-n;i++) recout(i,-1); 35 for (int i=1;i<=N;i++) for (int j=1;j<=c[i];j++) x=x*i; 36 return x; 37 } 38 int main() 39 { 40 for (int i=2;i<=N;i++) if (!p[i]) for (int j=1;j<=N/i;j++) p[i*j]=i; 41 scanf("%d%d",&n,&m),ans=C(m+n,m)-C(m+n,m-1),printf("%d",ans.shu[ans.shu[0]]); 42 for (int i=ans.shu[0]-1;i>=1;i--) printf("%05d",ans.shu[i]); 43 }