This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'
const ll mod = 1e9+7;
const ll MAXN = 500;
ll numInv[MAXN+10], facInv[MAXN+10], fac[MAXN+10];
void pre() {
numInv[0] = numInv[1] = 1;
for (int i = 2; i <= MAXN; i++) numInv[i] = numInv[mod % i] * (mod - mod / i) % mod;
facInv[0] = facInv[1] = 1;
for (int i = 2; i <= MAXN; i++) facInv[i] = (numInv[i] * facInv[i - 1]) % mod;
fac[0] = 1;
for (int i = 1; i <= MAXN; i++) fac[i] = (fac[i - 1] * i) % mod;
}
ll nCr(ll n, ll r) {
return ((fac[n] * facInv[r]) % mod * facInv[n - r]) % mod;
}
ll choose[MAXN+1], dp[2][MAXN+1];
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
memset(choose, 0, sizeof(choose)); memset(dp, 0, sizeof(dp));
pre();
int n; cin >> n;
vector<ii> lt(n);
vi raw(2*n);
for_(i, 0, n) {
cin >> lt[i][0] >> lt[i][1];
lt[i][1]++;
raw[2*i] = lt[i][0], raw[2*i+1] = lt[i][1];
}
sort(raw.begin(), raw.end());
raw.resize(unique(raw.begin(), raw.end()) - raw.begin());
int ns = raw.size()-1;
int cs = 0; ll len, val;
dp[1][n] = 1;
for (int s = ns-1; s >= 0; s--) {
choose[0] = 1;
len = raw[s+1]-raw[s];
for_(r, 1, n+1) {
choose[r] = 0;
val = 1;
for_(t, 1, r+1) {
if (t > len) val = 0;
else val = ((val * (len-t+1)) % mod) % mod;
choose[r] += (((nCr(r-1, t-1) * val) % mod) * facInv[t]) % mod;
if (choose[r] >= mod) choose[r] -= mod;
}
}
dp[cs][n] = 1;
for_(i, 0, n) {
dp[cs][i] = dp[1-cs][i];
int ct = raw[s] >= lt[i][0] and raw[s+1] <= lt[i][1];
if (ct) {
for_(j, i+1, n+1) {
dp[cs][i] += (choose[ct] * dp[1-cs][j]) % mod;
if (dp[cs][i] >= mod) dp[cs][i] -= mod;
if (j < n and raw[s] >= lt[j][0] and raw[s+1] <= lt[j][1]) ct++;
}
}
}
cs = 1-cs;
}
ll ans = 0;
for_(i, 0, n) {
ans += dp[1-cs][i];
if (ans >= mod) ans -= mod;
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |