// PHK
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define bigint __int128
#define emb emplace_back
#define pb push_back
#define pii pair <int, int>
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define Task ""
#define MASK(k) (1ull << k)
#define bitcnt(k) __builtin_popcount(k)
#define testBit(n, k) ((n >> k) & 1)
#define flipBit(n, k) (n ^ (1ll << k))
#define offBit(n, k) (n & ~MASK(k))
#define onBit(n, k) (n | (1ll << k))
template <class T> bool minimize(T &a, T b) {if (a > b) {a = b; return true;} return false;}
template <class T> bool maximize(T &a, T b) {if (a < b) {a = b; return true;} return false;}
const int N = 5e2 + 5, lim = 60, mod = 1e9 + 7;
const ll INF = 1e18;
int n, l[N], r[N], C[N << 1][N], g[N << 1][N], combi[N][N];
int dp[N][N << 1];
ll powMod(ll a, ll b) {
if (b == 0) return 1;
ll tmp = powMod(a, b >> 1);
return (b & 1) ? (tmp * tmp % mod * a % mod) : (tmp * tmp % mod);
}
// dp[i][j]: ways first i schools such that the number of boats sent <= pts[j]
void add(int &a, int b) {
if ((a += b) >= mod) a -= mod;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
if (fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n; vector <int> points;
for (int i = 1; i <= n; i++) {
cin >> l[i] >> r[i]; --l[i];
points.emplace_back(l[i]);
points.emplace_back(r[i]);
}
sort (all(points)); points.resize(unique(all(points)) - points.begin());
// C(n, k) = C(n, k-1) * (n-k+1) / k
for (int i = 1; i < points.size(); i++) {
C[i][1] = points[i] - points[i - 1];
for (int j = 2; j <= n; j++)
C[i][j] = 1ll * C[i][j - 1] * (points[i] - points[i - 1] - j + 1) % mod * powMod(j, mod - 2) % mod;
}
combi[0][0] = 1;
for (int i = 1; i <= n; i++) {
combi[0][i] = 1;
for (int j = 1; j <= i; j++)
combi[j][i] = (combi[j - 1][i - 1] + combi[j][i - 1]) % mod;
}
for (int i = 1; i < points.size(); i++) {
for (int j = 1; j <= n; j++) {
g[i][j] = 0;
for (int t = 0; t < j; t++)
add(g[i][j], 1ll * combi[t][j - 1] * C[i][t + 1] % mod);
}
}
for (int j = 0; j < points.size(); j++) dp[0][j] = 1;
for (int i = 1; i <= n; i++) {
dp[i][0] = 1;
for (int j = 1; j < points.size(); j++) {
dp[i][j] = dp[i][j - 1];
for (int k = i, cnt = 0; k > 0; k--) {
if (l[k] < points[j] && points[j] <= r[k]) {
++cnt; // forces k is the first school sent > pts[j - 1]
int S = g[j][cnt];
add(dp[i][j], 1ll * dp[k - 1][j - 1] * S % mod);
// for (int t = 0; t < cnt; t++)
// add(S, 1ll * combi[t][cnt - 1] * C[j][t + 1] % mod);
}
}
}
}
cout << (dp[n][points.size() - 1] - 1 + mod) % mod;
}
Compilation message (stderr)
boat.cpp: In function 'int main()':
boat.cpp:49:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen(Task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
boat.cpp:50:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen(Task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |