That Italy: (n + m) th A, (n + m) th B composed of a string, which was cut open, so that a number of n and m AB BA solution
Thinking 1: DP, dp [i] [j] denotes the i-th beginning with A, j at the beginning of the program number of B.
If this current is A, then it should satisfy, ij <= n, the number of AB not exceed (n + 1)
Similarly in the case of B.
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dep(i,a,b) for(int i=b;i>=a;i--) using namespace std; #define ll long long const int N=3e5+5; const int mod = 1e9+7; int n,m,f[3010][3010]; ll rd() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int sum(int a, int b) { int s = (a + b); if (s >= mod) s -= mod; return s; } void init() { rep(i,0,n+m) rep(j,0,n+m) f[i][j]=0; f[0][0]=1; rep(i,0,n+m) rep(j,0,n+m) { if(i-j<=n&&i!=0) f[i][j]=sum(f[i][j],f[i-1][j]); if(j-i<=m&&j!=0) f[i][j]=sum(f[i][j],f[i][j-1]); //printf("i=%d j=%d %d\n",i,j,f[i][j]); } printf("%d\n",f[n+m][n+m]); } int main() { while(~scanf("%d%d",&n,&m)) { init(); } }
Ideas 2: the number of combinations
years = C (2 * (n + m), n + m) - C (2 * (n + m), n-1) -C (2 * (n + m), m-1)
C (2 * (n + m), n-1) is the meaning
We have (n + m + m + 1) th A, and (n-1) th B, rearranges what program number.
Select difference number A and B is (2 * m + 2) prefix, AB interchange, is not legal answers.
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dep(i,a,b) for(int i=b;i>=a;i--) using namespace std; #define ll long long const int N=3e5+5; const int mod = 1e9+7; int n,m; ll fac[10100],inv[10100]; int sum(int a, int b) { int s = (a + b); if (s >= mod) s -= mod; return s; } int sub(int a, int b) { int s = a - b; if (s < 0) s += mod; return s; } int mult(int a, int b) { return (1LL * a * b) % mod; } ll rd() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } ll qpow(ll a,ll b,ll p) { ll ret=1;a%=p; while(b) { if(b&1) ret=ret*a%p; b/=2;a=a*a%p; } return ret; } ll C(ll n,ll m) { if(m>n) return 0; return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod; } int main() { fac[0]=1;inv[0]=1; for(int i=1;i<=10000;i++) { fac[i]=1ll*fac[i-1]*i%mod; inv[i]=qpow(fac[i],mod-2,mod); } while(~scanf("%d%d",&n,&m)) { printf("%d\n",sub(sub(C(2*(n+m),n+m),C(2*(n+m),n-1)),C(2*(n+m),m-1))); } }