Submission #23490

# Submission time Handle Problem Language Result Execution time Memory
23490 2017-05-10T11:49:38 Z Nirjhor Boat (APIO16_boat) C++14
58 / 100
2000 ms 7352 KB
#include <bits/stdc++.h> 

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;

const int N = 505;
const int M = 1005;
const int MOD = 1e9 + 7;

vector <pii> v;
vector <int> z[M];
int n, m, a[N], b[N];
int x[M], y[M], dp[N][M];
int sz[M], wide[M];
int comb[M][N], inverse[N];
int counter = 1;

int bigMod (int a, int e) {
  if (e == -1) e = MOD - 2;
  int r = 1; while (e) {
    if (e & 1) r = (r * 1ll * a) % MOD;
    a = (a * 1ll * a) % MOD;
    e >>= 1;
  }
  return r;
}

int call (int at, int range) {
  if (at > n or range > m) return 1;
  if (dp[at][range] != -1) return dp[at][range];
  ll ret = call(at, range + 1);
  for (int i = 0, pos = 0; i < sz[range]; ++i) {
    if (z[range][i] < at) {
      ++pos;
      continue;
    }
    // cout << rem << " comb ---> " << comb[range][rem] << endl;
    ret += comb[range][i - pos] * 1ll * call(z[range][i] + 1, range + 1);
    ret %= MOD;
  }
  return dp[at][range] = ret;
}

int nCr (int n, int r) {
  if (n < r) return 0;
  ll res = 1ll;
  for (int i = n, j = 1; j <= r; --i, ++j) {
    res *= 1ll * i, res %= MOD;
    res *= inverse[j], res %= MOD;
  }
  return res;
}

int main (int argc, char const *argv[]) {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d %d", a + i, b + i);
    v.push_back({a[i], 0});
    v.push_back({b[i], 1});
  }
  sort(v.begin(), v.end());
  for (int i = 1; i < n + n; ++i) {
    int one = v[i - 1].second, two = v[i].second;
    int l = v[i - 1].first, r = v[i].first;
    if (!one and !two) {
      --r, ++counter;
      if (l <= r) x[++m] = l, y[m] = r;
    } else if (!one and two) {
      --counter;
      // cout << "yo " << i << " " << m << endl;
      if (l <= r) x[++m] = l, y[m] = r;
    } else if (one and two) {
      ++l, --counter;
      if (l <= r) x[++m] = l, y[m] = r; 
    } else {
      ++l, --r;
      if (counter and l <= r) x[++m] = l, y[m] = r;
      ++counter;
    }
  }
  // cout << m << endl;
  // for (int i = 1; i <= m; ++i) {
  //   cout << x[i] << " " << y[i] << endl;
  // }
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (a[j] <= x[i] and y[i] <= b[j]) {
        z[i].push_back(j);
        ++sz[i];
      }
    }
    wide[i] = y[i] - x[i] + 1;
  } 
  for (int i = 1; i < N; ++i) {
    inverse[i] = bigMod(i, -1);
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j <= n; ++j) {
      comb[i][j] = nCr(wide[i] + j, j + 1);
    }
  }
  // for (int i = 1; i <= m; ++i) {
  //   cout << i << " --> ";
  //   for (int j : z[i]) cout << j << " ";
  //   cout << endl;
  // } 
  memset(dp, -1, sizeof dp);
  // for (int i = 1; i <= n; ++i) {
  //   for (int j = 1; j <= m; ++j) {
  //     cout << "(" << i << ", " << j << ") ---> " << call(i, j) << endl;
  //   }
  // }
  int ans = call(1, 1) + MOD - 1;
  printf("%d\n", ans % MOD);
  return 0;
}

Compilation message

boat.cpp: In function 'int main(int, const char**)':
boat.cpp:57:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
                  ^
boat.cpp:59:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", a + i, b + i);
                                 ^
# Verdict Execution time Memory Grader output
1 Correct 783 ms 6032 KB Output is correct
2 Correct 776 ms 6032 KB Output is correct
3 Correct 779 ms 6032 KB Output is correct
4 Correct 783 ms 6032 KB Output is correct
5 Correct 783 ms 6032 KB Output is correct
6 Correct 783 ms 6032 KB Output is correct
7 Correct 783 ms 6032 KB Output is correct
8 Correct 783 ms 6032 KB Output is correct
9 Correct 769 ms 6032 KB Output is correct
10 Correct 766 ms 6032 KB Output is correct
11 Correct 779 ms 6032 KB Output is correct
12 Correct 773 ms 6032 KB Output is correct
13 Correct 786 ms 6032 KB Output is correct
14 Correct 779 ms 6032 KB Output is correct
15 Correct 789 ms 6032 KB Output is correct
16 Correct 136 ms 6032 KB Output is correct
17 Correct 146 ms 6032 KB Output is correct
18 Correct 143 ms 6032 KB Output is correct
19 Correct 143 ms 6032 KB Output is correct
20 Correct 139 ms 6032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 783 ms 6032 KB Output is correct
2 Correct 776 ms 6032 KB Output is correct
3 Correct 779 ms 6032 KB Output is correct
4 Correct 783 ms 6032 KB Output is correct
5 Correct 783 ms 6032 KB Output is correct
6 Correct 783 ms 6032 KB Output is correct
7 Correct 783 ms 6032 KB Output is correct
8 Correct 783 ms 6032 KB Output is correct
9 Correct 769 ms 6032 KB Output is correct
10 Correct 766 ms 6032 KB Output is correct
11 Correct 779 ms 6032 KB Output is correct
12 Correct 773 ms 6032 KB Output is correct
13 Correct 786 ms 6032 KB Output is correct
14 Correct 779 ms 6032 KB Output is correct
15 Correct 789 ms 6032 KB Output is correct
16 Correct 136 ms 6032 KB Output is correct
17 Correct 146 ms 6032 KB Output is correct
18 Correct 143 ms 6032 KB Output is correct
19 Correct 143 ms 6032 KB Output is correct
20 Correct 139 ms 6032 KB Output is correct
21 Correct 1729 ms 6824 KB Output is correct
22 Correct 1716 ms 6956 KB Output is correct
23 Correct 1679 ms 6824 KB Output is correct
24 Correct 1686 ms 6824 KB Output is correct
25 Correct 1706 ms 6824 KB Output is correct
26 Correct 1919 ms 7220 KB Output is correct
27 Correct 1956 ms 7220 KB Output is correct
28 Correct 1909 ms 7220 KB Output is correct
29 Correct 1913 ms 7220 KB Output is correct
30 Correct 1536 ms 6032 KB Output is correct
31 Correct 1553 ms 6032 KB Output is correct
32 Correct 1553 ms 6032 KB Output is correct
33 Correct 1543 ms 6032 KB Output is correct
34 Correct 1556 ms 6032 KB Output is correct
35 Correct 1536 ms 6032 KB Output is correct
36 Correct 1536 ms 6032 KB Output is correct
37 Correct 1536 ms 6032 KB Output is correct
38 Correct 1533 ms 6032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 6032 KB Output is correct
2 Correct 16 ms 6032 KB Output is correct
3 Correct 13 ms 6032 KB Output is correct
4 Correct 16 ms 6032 KB Output is correct
5 Correct 13 ms 6032 KB Output is correct
6 Correct 13 ms 6164 KB Output is correct
7 Correct 16 ms 6164 KB Output is correct
8 Correct 16 ms 6164 KB Output is correct
9 Correct 16 ms 6164 KB Output is correct
10 Correct 16 ms 6164 KB Output is correct
11 Correct 16 ms 6032 KB Output is correct
12 Correct 9 ms 6032 KB Output is correct
13 Correct 13 ms 6032 KB Output is correct
14 Correct 13 ms 6032 KB Output is correct
15 Correct 16 ms 6032 KB Output is correct
16 Correct 9 ms 6032 KB Output is correct
17 Correct 6 ms 6032 KB Output is correct
18 Correct 9 ms 6032 KB Output is correct
19 Correct 9 ms 6032 KB Output is correct
20 Correct 6 ms 6032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 783 ms 6032 KB Output is correct
2 Correct 776 ms 6032 KB Output is correct
3 Correct 779 ms 6032 KB Output is correct
4 Correct 783 ms 6032 KB Output is correct
5 Correct 783 ms 6032 KB Output is correct
6 Correct 783 ms 6032 KB Output is correct
7 Correct 783 ms 6032 KB Output is correct
8 Correct 783 ms 6032 KB Output is correct
9 Correct 769 ms 6032 KB Output is correct
10 Correct 766 ms 6032 KB Output is correct
11 Correct 779 ms 6032 KB Output is correct
12 Correct 773 ms 6032 KB Output is correct
13 Correct 786 ms 6032 KB Output is correct
14 Correct 779 ms 6032 KB Output is correct
15 Correct 789 ms 6032 KB Output is correct
16 Correct 136 ms 6032 KB Output is correct
17 Correct 146 ms 6032 KB Output is correct
18 Correct 143 ms 6032 KB Output is correct
19 Correct 143 ms 6032 KB Output is correct
20 Correct 139 ms 6032 KB Output is correct
21 Correct 1729 ms 6824 KB Output is correct
22 Correct 1716 ms 6956 KB Output is correct
23 Correct 1679 ms 6824 KB Output is correct
24 Correct 1686 ms 6824 KB Output is correct
25 Correct 1706 ms 6824 KB Output is correct
26 Correct 1919 ms 7220 KB Output is correct
27 Correct 1956 ms 7220 KB Output is correct
28 Correct 1909 ms 7220 KB Output is correct
29 Correct 1913 ms 7220 KB Output is correct
30 Correct 1536 ms 6032 KB Output is correct
31 Correct 1553 ms 6032 KB Output is correct
32 Correct 1553 ms 6032 KB Output is correct
33 Correct 1543 ms 6032 KB Output is correct
34 Correct 1556 ms 6032 KB Output is correct
35 Correct 1536 ms 6032 KB Output is correct
36 Correct 1536 ms 6032 KB Output is correct
37 Correct 1536 ms 6032 KB Output is correct
38 Correct 1533 ms 6032 KB Output is correct
39 Correct 16 ms 6032 KB Output is correct
40 Correct 16 ms 6032 KB Output is correct
41 Correct 13 ms 6032 KB Output is correct
42 Correct 16 ms 6032 KB Output is correct
43 Correct 13 ms 6032 KB Output is correct
44 Correct 13 ms 6164 KB Output is correct
45 Correct 16 ms 6164 KB Output is correct
46 Correct 16 ms 6164 KB Output is correct
47 Correct 16 ms 6164 KB Output is correct
48 Correct 16 ms 6164 KB Output is correct
49 Correct 16 ms 6032 KB Output is correct
50 Correct 9 ms 6032 KB Output is correct
51 Correct 13 ms 6032 KB Output is correct
52 Correct 13 ms 6032 KB Output is correct
53 Correct 16 ms 6032 KB Output is correct
54 Correct 9 ms 6032 KB Output is correct
55 Correct 6 ms 6032 KB Output is correct
56 Correct 9 ms 6032 KB Output is correct
57 Correct 9 ms 6032 KB Output is correct
58 Correct 6 ms 6032 KB Output is correct
59 Correct 1906 ms 6956 KB Output is correct
60 Correct 1873 ms 6824 KB Output is correct
61 Correct 1819 ms 6824 KB Output is correct
62 Correct 1859 ms 7088 KB Output is correct
63 Correct 1843 ms 6824 KB Output is correct
64 Execution timed out 2000 ms 7352 KB Execution timed out
65 Halted 0 ms 0 KB -