Submission #41680

# Submission time Handle Problem Language Result Execution time Memory
41680 2018-02-20T12:25:46 Z gabrielsimoes Boat (APIO16_boat) C++14
100 / 100
708 ms 1364 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MOD = 1'000'000'007;

inline ll mod(ll a) { return a %= MOD, a < 0 ? a + MOD : a; }

int main() {
  int n;
  scanf("%d", &n);

  vector<int> limits;
  int va[n + 1], vb[n + 1];
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &va[i], &vb[i]);
    vb[i]++;
    limits.push_back(va[i]);
    limits.push_back(vb[i]);
  }

  sort(limits.begin(), limits.end());
  limits.resize(unique(limits.begin(), limits.end()) - limits.begin());

  ll dp[n + 1], sum[n + 1], inv[n + 1];

  memset(dp, 0, sizeof(dp));
  memset(sum, 0, sizeof(sum));

  sum[0] = 1;

  inv[1] = 1;
  for (int i = 2; i <= n; i++) inv[i] = mod(-(MOD / i) * inv[MOD % i]);

  for (int region = 1; region < limits.size(); region++) {
    ll len = limits[region] - limits[region - 1];

    for (int i = 1; i <= n; i++) {
      sum[i] = mod(sum[i - 1] + dp[i]);

      if (va[i] >= limits[region] || vb[i] <= limits[region - 1]) continue;

      ll bin = 1;
      for (int k = i, cnt = 0; k >= 1; k--) {
        if (va[k] >= limits[region] || vb[k] <= limits[region - 1]) continue;

        cnt++;
        bin = mod(mod(bin * (len + cnt - 2)) * inv[cnt]);

        if (cnt == 1) {
          dp[i] += mod(sum[k - 1] * len);
        } else {
          dp[i] += mod(sum[k - 1] * bin);
        }
      }

      dp[i] %= MOD;
    }
  }

  ll ans = 0;
  for (int i = 1; i <= n; i++) ans += dp[i];
  printf("%lld\n", ans % MOD);
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:35:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int region = 1; region < limits.size(); region++) {
                               ^
boat.cpp:11:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
                  ^
boat.cpp:16:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &va[i], &vb[i]);
                                   ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
3 Correct 6 ms 472 KB Output is correct
4 Correct 6 ms 500 KB Output is correct
5 Correct 6 ms 552 KB Output is correct
6 Correct 6 ms 604 KB Output is correct
7 Correct 6 ms 724 KB Output is correct
8 Correct 6 ms 728 KB Output is correct
9 Correct 6 ms 728 KB Output is correct
10 Correct 7 ms 728 KB Output is correct
11 Correct 6 ms 740 KB Output is correct
12 Correct 6 ms 740 KB Output is correct
13 Correct 6 ms 740 KB Output is correct
14 Correct 6 ms 740 KB Output is correct
15 Correct 6 ms 740 KB Output is correct
16 Correct 3 ms 740 KB Output is correct
17 Correct 4 ms 740 KB Output is correct
18 Correct 4 ms 740 KB Output is correct
19 Correct 3 ms 740 KB Output is correct
20 Correct 4 ms 740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
3 Correct 6 ms 472 KB Output is correct
4 Correct 6 ms 500 KB Output is correct
5 Correct 6 ms 552 KB Output is correct
6 Correct 6 ms 604 KB Output is correct
7 Correct 6 ms 724 KB Output is correct
8 Correct 6 ms 728 KB Output is correct
9 Correct 6 ms 728 KB Output is correct
10 Correct 7 ms 728 KB Output is correct
11 Correct 6 ms 740 KB Output is correct
12 Correct 6 ms 740 KB Output is correct
13 Correct 6 ms 740 KB Output is correct
14 Correct 6 ms 740 KB Output is correct
15 Correct 6 ms 740 KB Output is correct
16 Correct 3 ms 740 KB Output is correct
17 Correct 4 ms 740 KB Output is correct
18 Correct 4 ms 740 KB Output is correct
19 Correct 3 ms 740 KB Output is correct
20 Correct 4 ms 740 KB Output is correct
21 Correct 299 ms 740 KB Output is correct
22 Correct 262 ms 748 KB Output is correct
23 Correct 231 ms 748 KB Output is correct
24 Correct 263 ms 844 KB Output is correct
25 Correct 264 ms 844 KB Output is correct
26 Correct 614 ms 844 KB Output is correct
27 Correct 623 ms 948 KB Output is correct
28 Correct 622 ms 956 KB Output is correct
29 Correct 605 ms 976 KB Output is correct
30 Correct 6 ms 976 KB Output is correct
31 Correct 6 ms 976 KB Output is correct
32 Correct 6 ms 976 KB Output is correct
33 Correct 7 ms 988 KB Output is correct
34 Correct 6 ms 988 KB Output is correct
35 Correct 8 ms 988 KB Output is correct
36 Correct 7 ms 988 KB Output is correct
37 Correct 7 ms 1040 KB Output is correct
38 Correct 7 ms 1040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1040 KB Output is correct
2 Correct 3 ms 1040 KB Output is correct
3 Correct 4 ms 1040 KB Output is correct
4 Correct 4 ms 1040 KB Output is correct
5 Correct 4 ms 1040 KB Output is correct
6 Correct 7 ms 1040 KB Output is correct
7 Correct 7 ms 1040 KB Output is correct
8 Correct 7 ms 1040 KB Output is correct
9 Correct 7 ms 1040 KB Output is correct
10 Correct 7 ms 1040 KB Output is correct
11 Correct 4 ms 1040 KB Output is correct
12 Correct 3 ms 1040 KB Output is correct
13 Correct 4 ms 1040 KB Output is correct
14 Correct 4 ms 1040 KB Output is correct
15 Correct 4 ms 1040 KB Output is correct
16 Correct 3 ms 1040 KB Output is correct
17 Correct 3 ms 1040 KB Output is correct
18 Correct 3 ms 1040 KB Output is correct
19 Correct 3 ms 1040 KB Output is correct
20 Correct 3 ms 1040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 376 KB Output is correct
2 Correct 6 ms 376 KB Output is correct
3 Correct 6 ms 472 KB Output is correct
4 Correct 6 ms 500 KB Output is correct
5 Correct 6 ms 552 KB Output is correct
6 Correct 6 ms 604 KB Output is correct
7 Correct 6 ms 724 KB Output is correct
8 Correct 6 ms 728 KB Output is correct
9 Correct 6 ms 728 KB Output is correct
10 Correct 7 ms 728 KB Output is correct
11 Correct 6 ms 740 KB Output is correct
12 Correct 6 ms 740 KB Output is correct
13 Correct 6 ms 740 KB Output is correct
14 Correct 6 ms 740 KB Output is correct
15 Correct 6 ms 740 KB Output is correct
16 Correct 3 ms 740 KB Output is correct
17 Correct 4 ms 740 KB Output is correct
18 Correct 4 ms 740 KB Output is correct
19 Correct 3 ms 740 KB Output is correct
20 Correct 4 ms 740 KB Output is correct
21 Correct 299 ms 740 KB Output is correct
22 Correct 262 ms 748 KB Output is correct
23 Correct 231 ms 748 KB Output is correct
24 Correct 263 ms 844 KB Output is correct
25 Correct 264 ms 844 KB Output is correct
26 Correct 614 ms 844 KB Output is correct
27 Correct 623 ms 948 KB Output is correct
28 Correct 622 ms 956 KB Output is correct
29 Correct 605 ms 976 KB Output is correct
30 Correct 6 ms 976 KB Output is correct
31 Correct 6 ms 976 KB Output is correct
32 Correct 6 ms 976 KB Output is correct
33 Correct 7 ms 988 KB Output is correct
34 Correct 6 ms 988 KB Output is correct
35 Correct 8 ms 988 KB Output is correct
36 Correct 7 ms 988 KB Output is correct
37 Correct 7 ms 1040 KB Output is correct
38 Correct 7 ms 1040 KB Output is correct
39 Correct 4 ms 1040 KB Output is correct
40 Correct 3 ms 1040 KB Output is correct
41 Correct 4 ms 1040 KB Output is correct
42 Correct 4 ms 1040 KB Output is correct
43 Correct 4 ms 1040 KB Output is correct
44 Correct 7 ms 1040 KB Output is correct
45 Correct 7 ms 1040 KB Output is correct
46 Correct 7 ms 1040 KB Output is correct
47 Correct 7 ms 1040 KB Output is correct
48 Correct 7 ms 1040 KB Output is correct
49 Correct 4 ms 1040 KB Output is correct
50 Correct 3 ms 1040 KB Output is correct
51 Correct 4 ms 1040 KB Output is correct
52 Correct 4 ms 1040 KB Output is correct
53 Correct 4 ms 1040 KB Output is correct
54 Correct 3 ms 1040 KB Output is correct
55 Correct 3 ms 1040 KB Output is correct
56 Correct 3 ms 1040 KB Output is correct
57 Correct 3 ms 1040 KB Output is correct
58 Correct 3 ms 1040 KB Output is correct
59 Correct 302 ms 1040 KB Output is correct
60 Correct 273 ms 1040 KB Output is correct
61 Correct 248 ms 1040 KB Output is correct
62 Correct 311 ms 1040 KB Output is correct
63 Correct 288 ms 1132 KB Output is correct
64 Correct 704 ms 1144 KB Output is correct
65 Correct 708 ms 1292 KB Output is correct
66 Correct 708 ms 1292 KB Output is correct
67 Correct 700 ms 1292 KB Output is correct
68 Correct 707 ms 1292 KB Output is correct
69 Correct 324 ms 1292 KB Output is correct
70 Correct 267 ms 1292 KB Output is correct
71 Correct 258 ms 1292 KB Output is correct
72 Correct 270 ms 1292 KB Output is correct
73 Correct 279 ms 1364 KB Output is correct
74 Correct 61 ms 1364 KB Output is correct
75 Correct 57 ms 1364 KB Output is correct
76 Correct 63 ms 1364 KB Output is correct
77 Correct 58 ms 1364 KB Output is correct
78 Correct 60 ms 1364 KB Output is correct