Submission #109355

#TimeUsernameProblemLanguageResultExecution timeMemory
109355b2563125Boat (APIO16_boat)C++14
9 / 100
1043 ms12844 KiB
#include<iostream> #include<algorithm> #include<vector> using namespace std; #define int long long #define vel vector<long long> #define V vector #define ll long long #define rep(i,n) for(int i=0;i<n;i++) int pr = 1000000007; void ref(int &a) { a = a % pr; } void uni(vel &a) { vel ans(1, a[0]); int n = a.size(); rep(i,n - 1) { if (a[i + 1] != a[i]) { ans.push_back(a[i + 1]); } } a = ans; } int rui(int a, int n) { if (n == 0) { return 1; } int back = rui(a, n / 2); back *= back; ref(back); if (n % 2 == 0) { return back; } return (back*a) % pr; } int inv(int a) { return rui(a, pr - 2); } void pl(int &a, int b) { a += b; ref(a); } signed main() { int n; cin >> n; vel a(n); vel b(n); vel all_time(1, 0); rep(i, n) { cin >> a[i] >> b[i]; b[i]++; all_time.push_back(a[i]); all_time.push_back(b[i]); } sort(all_time.begin(), all_time.end()); uni(all_time); int sz = all_time.size(); vel gap(sz - 1); rep(i, sz - 1) { gap[i] = all_time[i + 1] - all_time[i]; } vel count(sz-1); rep(i, n) { a[i] = lower_bound(all_time.begin(), all_time.end(), a[i])-all_time.begin(); b[i] = lower_bound(all_time.begin(), all_time.end(), b[i])-all_time.begin(); rep(j, b[i] - a[i]) { count[a[i] + j]++; } } V<vel> com(sz-1); rep(i, sz-1) { int ba = 1; rep(j, count[i]) { ba *= gap[i] - j; ref(ba); ba *= inv(j + 1); ref(ba); com[i].push_back(ba); } } V<V<vel>> dp1(2, V<vel>(sz-1,vel(n))); vel now_count(sz - 1, 0); rep(i, n) { int now = i % 2; int nex = (i + 1) % 2; dp1[nex] = dp1[now]; int sum = 1; rep(j, a[i]) { rep(k, now_count[j]) { sum += dp1[now][j][k] * com[j][k]; ref(sum); } } for (int j = a[i]; j < b[i]; j++) { pl(dp1[nex][j][0],sum); rep(k, now_count[j]) { pl(dp1[nex][j][k + 1], dp1[nex][j][k]); sum += dp1[now][j][k] * com[j][k]; ref(sum); } sum += dp1[now][j][now_count[j]] * com[j][now_count[j]]; now_count[j]++; } } int ans = 0; rep(i, sz-1) { rep(k, count[i]) { ans += dp1[n%2][i][k] * com[i][k]; ref(ans); } } cout << ans << endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...