이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const int MAXN = 502;
ll fact[MAXN * 2];
ll rfact[MAXN * 2];
ll pw(ll a, ll m) {
if (!m)
return 1;
a %= MOD;
if (m % 2)
return (a * pw(a, m - 1)) % MOD;
return pw(a * a, m / 2);
}
ll choose(ll n, ll k) {
return (((fact[n] * rfact[k]) % MOD) * rfact[n - k]) % MOD;
}
ll dp[MAXN * 2][MAXN];
ll ndp[MAXN * 2][MAXN];
ll a[MAXN];
ll b[MAXN];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
rfact[0] = fact[0] = 1;
for (int i = 1; i < 2 * MAXN; ++i) {
fact[i] = (fact[i - 1] * i) % MOD;
rfact[i] = pw(fact[i], MOD - 2);
}
int n;
cin >> n;
vector<ll> cpr = {0};
vector<ll> sz;
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
++b[i];
cpr.push_back(a[i]);
cpr.push_back(b[i]);
}
sort(cpr.begin(), cpr.end());
cpr.resize(unique(cpr.begin(), cpr.end()) - cpr.begin());
int m = cpr.size();
for (int i = 0; i + 1 < m; ++i) {
sz.push_back(cpr[i + 1] - cpr[i]);
//printf("%Ld ", sz[i]);
}
//printf("\n");
for (int i = 0; i < n; ++i) {
a[i] = lower_bound(cpr.begin(), cpr.end(), a[i]) - cpr.begin();
b[i] = lower_bound(cpr.begin(), cpr.end(), b[i]) - cpr.begin();
}
for (int i = 0; i < m; ++i)
dp[i][0] = 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
for (int k = 0; k <= n; ++k)
ndp[j][k] = 0;
}
for (int j = b[i] - 1; j >= a[i]; --j) {
for (int k = 0; k <= n; ++k) {
ndp[j][k] += dp[j][k - 1];
//cout << "(" << j << ' ' << k << ": " << ndp[j][k] << ' ' << choose(sz[j], k) << ") ";
ndp[j + 1][0] += (ndp[j][k] * choose(sz[j], k)) % MOD;
}
}
for (int j = 1; j < m; ++j) {
ndp[j][0] += ndp[j - 1][0];
ndp[j][0] %= MOD;
}
for (int j = 0; j < m; ++j) {
for (int k = 0; k <= n; ++k) {
ndp[j][k] += dp[j][k];
}
}
for (int j = 0; j < m; ++j) {
for (int k = 0; k <= n; ++k)
dp[j][k] = ndp[j][k];
}
//printf("---\n");
//for (int j = 0; j < m; ++j) {
//for (int k = 0; k <= n; ++k)
//printf("%Ld ", dp[j][k]);
//printf("\n");
//}
}
cout << dp[m - 1][0] - 1 << 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... |