제출 #376291

#제출 시각아이디문제언어결과실행 시간메모리
376291shivensinha4Boat (APIO16_boat)C++17
58 / 100
2082 ms16364 KiB
#pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #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' namespace speedy_io { int read_nonneg() { int c; while ((c = getchar_unlocked()) < '0' || c > '9'); int v = c - '0'; while ((c = getchar_unlocked()) >= '0' && c <= '9') { v = (v << 3) + (v << 1) + (c - '0'); } return v; } const int max_ll_size = 20; void put_nonneg(ll n) { int i = max_ll_size; char output_buffer[max_ll_size]; do { output_buffer[--i] = (n % 10) + '0'; n /= 10; } while (n); do { putchar_unlocked(output_buffer[i]); } while (++i < max_ll_size); } } using namespace speedy_io; const ll mod = 1e9+7; ll power(ll x, ll y) { if (y == 0) return 1; ll p = power(x, y/2) % mod; p = (p * p) % mod; return (y%2 == 0)? p : (x * p) % mod; } ll modInverse(ll a) { return power(a, mod-2); } const int MXN = 500; ll choose[4*MXN+2][MXN+1], dp[2][MXN+1][MXN+1][2]; int main() { #ifdef mlocal freopen("test.in", "r", stdin); #endif // ios_base::sync_with_stdio(false); // cin.tie(0); int n = read_nonneg(); vector<ii> lt(n); vi raw(2*n); for_(i, 0, n) { lt[i][0] = read_nonneg(), lt[i][1] = read_nonneg(); 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()); vector<ii> seg; // int pre = -1; for_(i, 1, raw.size()) { // if (pre < raw[i] and pre != -1) seg.push_back({pre, raw[i]-1}); seg.push_back({raw[i-1], raw[i]-1}); // pre = raw[i]+1; } int ns = seg.size(); // cout << ns << endl; // for (auto i: seg) cout << i[0] << " " << i[1] << endl; memset(choose, 0, sizeof(choose)); memset(dp, 0, sizeof(dp)); vector<ll> modInv(n+1); for_(i, 1, n+1) modInv[i] = modInverse(i); for_(i, 0, ns) { choose[i][0] = 1; for_(r, 1, min(n, seg[i][1]-seg[i][0]+1) + 1) choose[i][r] = (((choose[i][r-1] * (seg[i][1]-seg[i][0]-r+2)) % mod) * modInv[r]) % mod; } bool valid[n]; int c = 0; for (int s = ns-1; s >= 0; s--) { memset(dp[c], 0, sizeof(dp[c])); for_(idx, 0, n) valid[idx] = seg[s][0] >= lt[idx][0] and seg[s][1] < lt[idx][1]; for (int ct = min(n-1, seg[s][1]-seg[s][0]+1); ct >= 0; ct--) { dp[c][ct][n][0] = choose[s][ct]; for (int idx = n-1; idx >= 0; idx--) { if (valid[idx]) dp[c][ct][idx][1] += dp[c][ct+1][idx+1][0]; if (dp[c][ct][idx][1] >= mod) dp[c][ct][idx][1] -= mod; dp[c][ct][idx][1] += (choose[s][ct] * (s < ns-1 ? dp[1-c][0][idx][1] : 0)) % mod; if (dp[c][ct][idx][1] >= mod) dp[c][ct][idx][1] -= mod; dp[c][ct][idx][0] = dp[c][ct][idx][1] + dp[c][ct][idx+1][0]; if (dp[c][ct][idx][0] >= mod) dp[c][ct][idx][0] -= mod; } } c = 1-c; } put_nonneg((dp[1-c][0][0][0]-1+mod) % mod); printf("\n"); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...