1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include <bits/stdc++.h> #define per(i,a,n) for (int i=a;i<n;i++) #define rep(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define mem(a, b) memset(a, b, sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pI; typedef vector<ll> vI; const ll mod(1e9 + 7); const ll INF(0x3f3f3f3f); ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res * a%mod; a = a * a%mod; }return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
const int MAXN(1e3+7); ll Cn2n[MAXN]; ll Inv[MAXN]; ll n, m, sumnm; ll outt; inline ll C(ll nn,ll mm) { if(mm<0)return 0; if(nn<mm)return 0; if(mm>nn-mm)mm=nn-mm; ll up=1,down=1; for(ll i=0;i<mm;i++) { up=up*(nn-i)%mod; down=down*(i+1)%mod; } return up*Inv[down]%mod; } ll dfs(ll pos, ll s, ll xnm, ll ynm) { if(s>m || s<-n) { return C(2*sumnm-pos, sumnm-xnm); } if(xnm==sumnm || ynm==sumnm) {return 0;} ll summ1; summ1 = dfs(pos+1, s+1, xnm+1, ynm); summ1 += dfs(pos+1, s-1, xnm, ynm+1); return summ1; } ll dp[5*MAXN][MAXN*20]; ll dpp[MAXN][MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); dpp[0][0] = 1; while(cin >> n >> m) { ll tmp = min(n, m), tmp1=max(n, m); n = tmp; m=tmp1; if(dpp[n][m]) { cout << dpp[n][m] << endl; continue; } sumnm = n+m; dp[0][10*MAXN] = 1; per(i, 1, sumnm*2+1) { per(j, 0, m+2) { dp[i][10*MAXN+j] = 0; } per(j, 1, n+2) { dp[i][10*MAXN-j] = 0; } per(j, 0, m+1) { if (dp[i - 1][10 * MAXN + j]) { dp[i][10*MAXN+j+1] = (dp[i][10*MAXN+j+1]+dp[i-1][10*MAXN+j])%mod; dp[i][10*MAXN+j-1] = (dp[i][10*MAXN+j-1]+dp[i-1][10*MAXN+j])%mod; } } per(j, 1, n+1) { if(dp[i-1][10*MAXN-j]) { dp[i][10*MAXN-j+1] = (dp[i][10*MAXN-j+1]+dp[i-1][10*MAXN-j])%mod; dp[i][10*MAXN-j-1] = (dp[i][10*MAXN-j-1]+dp[i-1][10*MAXN-j])%mod; } } } dpp[n][m] = dp[sumnm*2][10*MAXN]%mod; cout << dpp[n][m] << endl; } return 0; }
|