Submission #849182

# Submission time Handle Problem Language Result Execution time Memory
849182 2023-09-14T08:42:14 Z hngwlog Boat (APIO16_boat) C++14
100 / 100
535 ms 6072 KB
#include <bits/stdc++.h>
using namespace std;

const int maxn = 505;
const long long mod = 1e9 + 7;

int n;
int a[maxn], b[maxn], has[maxn];
long long sum[maxn], g[maxn], p[maxn], dp[maxn], ivp[maxn];
long long f[2 * maxn][maxn]; // segment i, last boat: j
vector<int> c;
vector<vector<int>> adj(2 * maxn);

long long pow_(long long o, long long q) {
    
    if (!q) return 1;
    long long t = pow_(o, q / 2);
    t = (t * t) % mod;
    if (q % 2) t = (t * o) % mod;
    return t;
}

void load() {
    
    p[0] = 1;
    for (int i = 1; i <= n; i++) p[i] = (p[i - 1] * i) % mod;
    for (int i = 0; i <= n; i++) ivp[i] = pow_(p[i], mod - 2);
    for (int i = 1; i <= n; i++) {
        int l = lower_bound(c.begin(), c.end(), a[i] - 1) - c.begin();
        int r = lower_bound(c.begin(), c.end(), b[i]) - c.begin();
        for (int j = l; j < r; j++) adj[j].push_back(i);
    }
}

long long C(int k, int n_) {
    
    if (!k || k == n_) return 1;
    if (k > n) return 0;
    return p[n_] * (ivp[k] * ivp[n_ - k] % mod) % mod;
}

void solve() {
    
    sum[0] = 1;
    for (int i = 0; i < (int)c.size(); i++) {
        int sz = (int)adj[i].size();
        int value = c[i + 1] - c[i];
        for (int j = 1; j <= sz; j++) {
            dp[j] = 0;
            long long res = 1;
            for (int z = 0; z < j; z++) {
                res = (res * (value - z)) % mod;
                if (z < j - 1) dp[j] = (dp[j] + C(z, j - 1) * g[z + 1]) % mod;
            }
            g[j] = (res * ivp[j]) % mod;
            if (j > value) g[j] = 0;
            dp[j] = (dp[j] + g[j]) % mod;
        }
        for (int j = 1; j <= sz; j++) g[j] = 0;
        for (auto j: adj[i]) {
            has[j]++;
            int cnt = 1;
            for (int z = j - 1; z >= 0; z--) {
                f[i][j] = (f[i][j] + sum[z] * dp[cnt]) % mod;
                if (has[z]) cnt++;
            }
        }
        for (auto j: adj[i]) sum[j] = (sum[j] + f[i][j]) % mod, has[j]--;
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) ans = (ans + sum[i]) % mod;
    cout << ans << '\n';
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
    
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
        c.push_back(a[i] - 1);
        c.push_back(b[i]);
    }
    sort(c.begin(), c.end());
    c.resize(unique(c.begin(), c.end()) - c.begin());
    load();
    solve();
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2392 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 2 ms 2392 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 2 ms 2428 KB Output is correct
9 Correct 2 ms 2396 KB Output is correct
10 Correct 2 ms 2352 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 2 ms 2396 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 1116 KB Output is correct
18 Correct 2 ms 860 KB Output is correct
19 Correct 1 ms 1116 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2392 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 2 ms 2392 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 2 ms 2428 KB Output is correct
9 Correct 2 ms 2396 KB Output is correct
10 Correct 2 ms 2352 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 2 ms 2396 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 1116 KB Output is correct
18 Correct 2 ms 860 KB Output is correct
19 Correct 1 ms 1116 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 267 ms 4888 KB Output is correct
22 Correct 256 ms 5000 KB Output is correct
23 Correct 233 ms 4692 KB Output is correct
24 Correct 259 ms 5196 KB Output is correct
25 Correct 258 ms 4992 KB Output is correct
26 Correct 475 ms 5160 KB Output is correct
27 Correct 478 ms 5200 KB Output is correct
28 Correct 475 ms 5212 KB Output is correct
29 Correct 470 ms 4964 KB Output is correct
30 Correct 4 ms 4440 KB Output is correct
31 Correct 4 ms 4440 KB Output is correct
32 Correct 4 ms 4444 KB Output is correct
33 Correct 4 ms 4444 KB Output is correct
34 Correct 4 ms 4444 KB Output is correct
35 Correct 4 ms 4444 KB Output is correct
36 Correct 4 ms 4444 KB Output is correct
37 Correct 4 ms 4444 KB Output is correct
38 Correct 4 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1112 KB Output is correct
2 Correct 3 ms 1316 KB Output is correct
3 Correct 3 ms 1116 KB Output is correct
4 Correct 3 ms 1112 KB Output is correct
5 Correct 3 ms 1116 KB Output is correct
6 Correct 5 ms 1116 KB Output is correct
7 Correct 5 ms 1372 KB Output is correct
8 Correct 5 ms 1348 KB Output is correct
9 Correct 5 ms 1116 KB Output is correct
10 Correct 5 ms 1116 KB Output is correct
11 Correct 3 ms 1116 KB Output is correct
12 Correct 2 ms 1116 KB Output is correct
13 Correct 3 ms 1116 KB Output is correct
14 Correct 3 ms 1116 KB Output is correct
15 Correct 3 ms 1116 KB Output is correct
16 Correct 2 ms 860 KB Output is correct
17 Correct 2 ms 860 KB Output is correct
18 Correct 2 ms 860 KB Output is correct
19 Correct 2 ms 800 KB Output is correct
20 Correct 2 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2392 KB Output is correct
2 Correct 2 ms 2396 KB Output is correct
3 Correct 2 ms 2392 KB Output is correct
4 Correct 2 ms 2396 KB Output is correct
5 Correct 2 ms 2396 KB Output is correct
6 Correct 2 ms 2396 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 2 ms 2428 KB Output is correct
9 Correct 2 ms 2396 KB Output is correct
10 Correct 2 ms 2352 KB Output is correct
11 Correct 2 ms 2396 KB Output is correct
12 Correct 2 ms 2396 KB Output is correct
13 Correct 2 ms 2396 KB Output is correct
14 Correct 2 ms 2396 KB Output is correct
15 Correct 2 ms 2396 KB Output is correct
16 Correct 1 ms 860 KB Output is correct
17 Correct 1 ms 1116 KB Output is correct
18 Correct 2 ms 860 KB Output is correct
19 Correct 1 ms 1116 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 267 ms 4888 KB Output is correct
22 Correct 256 ms 5000 KB Output is correct
23 Correct 233 ms 4692 KB Output is correct
24 Correct 259 ms 5196 KB Output is correct
25 Correct 258 ms 4992 KB Output is correct
26 Correct 475 ms 5160 KB Output is correct
27 Correct 478 ms 5200 KB Output is correct
28 Correct 475 ms 5212 KB Output is correct
29 Correct 470 ms 4964 KB Output is correct
30 Correct 4 ms 4440 KB Output is correct
31 Correct 4 ms 4440 KB Output is correct
32 Correct 4 ms 4444 KB Output is correct
33 Correct 4 ms 4444 KB Output is correct
34 Correct 4 ms 4444 KB Output is correct
35 Correct 4 ms 4444 KB Output is correct
36 Correct 4 ms 4444 KB Output is correct
37 Correct 4 ms 4444 KB Output is correct
38 Correct 4 ms 4444 KB Output is correct
39 Correct 3 ms 1112 KB Output is correct
40 Correct 3 ms 1316 KB Output is correct
41 Correct 3 ms 1116 KB Output is correct
42 Correct 3 ms 1112 KB Output is correct
43 Correct 3 ms 1116 KB Output is correct
44 Correct 5 ms 1116 KB Output is correct
45 Correct 5 ms 1372 KB Output is correct
46 Correct 5 ms 1348 KB Output is correct
47 Correct 5 ms 1116 KB Output is correct
48 Correct 5 ms 1116 KB Output is correct
49 Correct 3 ms 1116 KB Output is correct
50 Correct 2 ms 1116 KB Output is correct
51 Correct 3 ms 1116 KB Output is correct
52 Correct 3 ms 1116 KB Output is correct
53 Correct 3 ms 1116 KB Output is correct
54 Correct 2 ms 860 KB Output is correct
55 Correct 2 ms 860 KB Output is correct
56 Correct 2 ms 860 KB Output is correct
57 Correct 2 ms 800 KB Output is correct
58 Correct 2 ms 860 KB Output is correct
59 Correct 290 ms 5508 KB Output is correct
60 Correct 265 ms 5308 KB Output is correct
61 Correct 255 ms 5644 KB Output is correct
62 Correct 287 ms 5460 KB Output is correct
63 Correct 276 ms 5460 KB Output is correct
64 Correct 529 ms 5716 KB Output is correct
65 Correct 530 ms 6072 KB Output is correct
66 Correct 528 ms 5632 KB Output is correct
67 Correct 528 ms 5712 KB Output is correct
68 Correct 535 ms 5716 KB Output is correct
69 Correct 228 ms 5080 KB Output is correct
70 Correct 230 ms 5340 KB Output is correct
71 Correct 229 ms 5204 KB Output is correct
72 Correct 241 ms 5120 KB Output is correct
73 Correct 246 ms 5300 KB Output is correct
74 Correct 56 ms 1196 KB Output is correct
75 Correct 53 ms 1280 KB Output is correct
76 Correct 58 ms 1364 KB Output is correct
77 Correct 57 ms 1360 KB Output is correct
78 Correct 57 ms 1364 KB Output is correct