-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombination_build.cpp
More file actions
69 lines (57 loc) · 1.55 KB
/
combination_build.cpp
File metadata and controls
69 lines (57 loc) · 1.55 KB
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
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1e9 + 7;
int fact[4000005], invfact[4000005];
//https://oj.vnoi.info/problem/olp_ct24_path
int fpow(int a, int b) {
int r = 1;
while (b) {
if (b & 1) r = r * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return r;
}
void init(int N) {
fact[0] = 1;
for (int i = 1; i <= N; i++) fact[i] = fact[i-1] * i % MOD;
invfact[N] = fpow(fact[N], MOD-2);
for (int i = N; i > 0; i--) invfact[i-1] = invfact[i] * i % MOD;
}
int C(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n] * invfact[k] % MOD * invfact[n-k] % MOD;
}
int cnt(int x1,int y1,int x2,int y2){
int dx = x2-x1;
int dy = y2-y1;
if(dx<0 || dy<0) return 0;
return C(dx+dy, dx);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int m,n,k;
cin>>m>>n>>k;
init(2*(m+n));
vector<pair<int,int>> v(k);
for(int i=0;i<k;i++) cin>>v[i].first>>v[i].second;
v.push_back({m,n});
sort(v.begin(), v.end(), [](auto &a, auto &b){
return a.first + a.second < b.first + b.second;
});
int K = v.size();
vector<int> dp(K);
for(int i=0;i<K;i++){
dp[i] = cnt(1,1, v[i].first, v[i].second);
for(int j=0;j<i;j++){
if(v[j].first <= v[i].first && v[j].second <= v[i].second){
dp[i] = (dp[i] - dp[j] * cnt(v[j].first, v[j].second,
v[i].first, v[i].second) % MOD + MOD) % MOD;
}
}
}
cout<<dp.back();
return 0;
}