Submission #231294

# Submission time Handle Problem Language Result Execution time Memory
231294 2020-05-13T10:19:26 Z AlexLuchianov Boat (APIO16_boat) C++14
100 / 100
1860 ms 5496 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <map>

using namespace std;

using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))

int const nmax = 500;
int const modulo = 1000000007;

class ModuloRing{
private:
  void gcd(int a, int b, int &x, int &y){
    if(b == 0){
      x = 1;
      y = 0;
    } else {
      gcd(b, a % b, x, y);
      int aux = x;
      x = y;
      y = aux - a / b * y;
    }
  }
public:
  int number;
  ModuloRing(ll number_ = 0){
    number = number_ % modulo;
  }
  ModuloRing operator + (ModuloRing a) {
    return {number + a.number};
  }
  ModuloRing operator - (ModuloRing a) {
    return {modulo + number - a.number};
  }
  ModuloRing operator * (ModuloRing a) {
    return {1LL * number * a.number};
  }
  ModuloRing operator / (ModuloRing a) {
    int x, y;
    gcd(a.number, modulo, x, y);
    x %= modulo;
    if(x < 0)
      x += modulo;
    return 1LL * number * x;
  }
};

map<int,int> code;
vector<int> dist;

int a[1 + nmax], b[1 + nmax];

void normalize(int n){
  vector<int> temp;
  temp.push_back(0);
  for(int i = 1; i <= n; i++){
    temp.push_back(a[i]);
    temp.push_back(b[i]);
  }
  sort(temp.begin(), temp.end());
  temp.erase(unique(temp.begin(), temp.end()), temp.end());
  dist = temp;
  for(int i = 0; i < temp.size(); i++)
    code[temp[i]] = i;
  for(int i = 1;i <= n; i++){
    a[i] = code[a[i]];
    b[i] = code[b[i]];
  }

}

ModuloRing choose[5 + 2 * nmax][1 + nmax];
ModuloRing dp[5 + nmax][5 + 2 * nmax];
ModuloRing fact[5 + nmax];

void computefact(){
  fact[0] = 1;
  for(int i = 1;i <= nmax; i++)
    fact[i] = fact[i - 1] * i;
}

ModuloRing combfast[1 + nmax][1 + nmax];

void precompute(){
  combfast[0][0] = 1;
  for(int i = 1;i <= nmax; i++) {
    combfast[i][0] = 1;
    for(int j = 1; j <= i; j++)
      combfast[i][j] = combfast[i - 1][j - 1] + combfast[i - 1][j];
  }
}

ModuloRing comb(int n, int k){
  if(n < k)
    return 0;
  if(n <= nmax)
    return combfast[n][k];

  ModuloRing result(1);
  for(int i = n - k + 1; i <= n; i++)
    result = result * i;
  return result / fact[k];
}

int main()
{
  int n;
  cin >> n;
  for(int i = 1;i <= n; i++) {
    cin >> a[i] >> b[i];
    b[i]++;
  }
  normalize(n);
  computefact();
  precompute();

  for(int i = 1; i < dist.size() - 1; i++) {
    int d = dist[i + 1] - dist[i];
    for(int h = 1;h <= n; h++) {
      ModuloRing snap = comb(d, h);
      for(int j = h; j <= n; j++)
        choose[i][j] = choose[i][j] + comb(j - 1, h - 1) * snap;
    }
  }


  for(int i = 0; i < dist.size() - 1; i++)
    dp[0][i] = 1;

  for(int i = 1;i <= n; i++){
    for(int j = a[i]; j < b[i]; j++){
      int _count = 1;
      for(int i2 = i - 1; 0 <= i2; i2--){
        dp[i][j] = dp[i][j] + dp[i2][j - 1] * choose[j][_count];
        if(a[i2] <= j && j < b[i2])
          _count++;
      }
    }
    for(int j = 1; j < dist.size() - 1; j++) {
      dp[i][j] = dp[i][j] + dp[i][j - 1];
    }
  }

  ModuloRing result(0);
  for(int i = 1;i <= n; i++)
    result = result + dp[i][dist.size() - 2];
  cout << result.number;
  return 0;
}

/*
2
1 10
5 20
*/

Compilation message

boat.cpp: In function 'void normalize(int)':
boat.cpp:69:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < temp.size(); i++)
                  ~~^~~~~~~~~~~~~
boat.cpp: In function 'int main()':
boat.cpp:123:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 1; i < dist.size() - 1; i++) {
                  ~~^~~~~~~~~~~~~~~~~
boat.cpp:133:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0; i < dist.size() - 1; i++)
                  ~~^~~~~~~~~~~~~~~~~
boat.cpp:145:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int j = 1; j < dist.size() - 1; j++) {
                    ~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 979 ms 5476 KB Output is correct
2 Correct 965 ms 5376 KB Output is correct
3 Correct 1008 ms 5496 KB Output is correct
4 Correct 968 ms 5376 KB Output is correct
5 Correct 981 ms 5376 KB Output is correct
6 Correct 986 ms 5376 KB Output is correct
7 Correct 972 ms 5496 KB Output is correct
8 Correct 978 ms 5376 KB Output is correct
9 Correct 969 ms 5496 KB Output is correct
10 Correct 980 ms 5376 KB Output is correct
11 Correct 961 ms 5496 KB Output is correct
12 Correct 962 ms 5376 KB Output is correct
13 Correct 964 ms 5496 KB Output is correct
14 Correct 969 ms 5496 KB Output is correct
15 Correct 972 ms 5496 KB Output is correct
16 Correct 177 ms 5368 KB Output is correct
17 Correct 192 ms 5248 KB Output is correct
18 Correct 186 ms 5344 KB Output is correct
19 Correct 190 ms 5248 KB Output is correct
20 Correct 186 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 979 ms 5476 KB Output is correct
2 Correct 965 ms 5376 KB Output is correct
3 Correct 1008 ms 5496 KB Output is correct
4 Correct 968 ms 5376 KB Output is correct
5 Correct 981 ms 5376 KB Output is correct
6 Correct 986 ms 5376 KB Output is correct
7 Correct 972 ms 5496 KB Output is correct
8 Correct 978 ms 5376 KB Output is correct
9 Correct 969 ms 5496 KB Output is correct
10 Correct 980 ms 5376 KB Output is correct
11 Correct 961 ms 5496 KB Output is correct
12 Correct 962 ms 5376 KB Output is correct
13 Correct 964 ms 5496 KB Output is correct
14 Correct 969 ms 5496 KB Output is correct
15 Correct 972 ms 5496 KB Output is correct
16 Correct 177 ms 5368 KB Output is correct
17 Correct 192 ms 5248 KB Output is correct
18 Correct 186 ms 5344 KB Output is correct
19 Correct 190 ms 5248 KB Output is correct
20 Correct 186 ms 5248 KB Output is correct
21 Correct 828 ms 5376 KB Output is correct
22 Correct 827 ms 5248 KB Output is correct
23 Correct 793 ms 5368 KB Output is correct
24 Correct 813 ms 5364 KB Output is correct
25 Correct 815 ms 5368 KB Output is correct
26 Correct 919 ms 5364 KB Output is correct
27 Correct 945 ms 5368 KB Output is correct
28 Correct 934 ms 5368 KB Output is correct
29 Correct 925 ms 5368 KB Output is correct
30 Correct 915 ms 5496 KB Output is correct
31 Correct 904 ms 5380 KB Output is correct
32 Correct 916 ms 5368 KB Output is correct
33 Correct 936 ms 5372 KB Output is correct
34 Correct 907 ms 5376 KB Output is correct
35 Correct 910 ms 5408 KB Output is correct
36 Correct 965 ms 5472 KB Output is correct
37 Correct 974 ms 5472 KB Output is correct
38 Correct 936 ms 5404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 5248 KB Output is correct
2 Correct 24 ms 5248 KB Output is correct
3 Correct 24 ms 5248 KB Output is correct
4 Correct 24 ms 5248 KB Output is correct
5 Correct 24 ms 5248 KB Output is correct
6 Correct 24 ms 5248 KB Output is correct
7 Correct 27 ms 5248 KB Output is correct
8 Correct 25 ms 5248 KB Output is correct
9 Correct 27 ms 5248 KB Output is correct
10 Correct 25 ms 5248 KB Output is correct
11 Correct 25 ms 5248 KB Output is correct
12 Correct 24 ms 5248 KB Output is correct
13 Correct 23 ms 5248 KB Output is correct
14 Correct 24 ms 5248 KB Output is correct
15 Correct 24 ms 5248 KB Output is correct
16 Correct 17 ms 5248 KB Output is correct
17 Correct 15 ms 5248 KB Output is correct
18 Correct 16 ms 5248 KB Output is correct
19 Correct 16 ms 5248 KB Output is correct
20 Correct 16 ms 5248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 979 ms 5476 KB Output is correct
2 Correct 965 ms 5376 KB Output is correct
3 Correct 1008 ms 5496 KB Output is correct
4 Correct 968 ms 5376 KB Output is correct
5 Correct 981 ms 5376 KB Output is correct
6 Correct 986 ms 5376 KB Output is correct
7 Correct 972 ms 5496 KB Output is correct
8 Correct 978 ms 5376 KB Output is correct
9 Correct 969 ms 5496 KB Output is correct
10 Correct 980 ms 5376 KB Output is correct
11 Correct 961 ms 5496 KB Output is correct
12 Correct 962 ms 5376 KB Output is correct
13 Correct 964 ms 5496 KB Output is correct
14 Correct 969 ms 5496 KB Output is correct
15 Correct 972 ms 5496 KB Output is correct
16 Correct 177 ms 5368 KB Output is correct
17 Correct 192 ms 5248 KB Output is correct
18 Correct 186 ms 5344 KB Output is correct
19 Correct 190 ms 5248 KB Output is correct
20 Correct 186 ms 5248 KB Output is correct
21 Correct 828 ms 5376 KB Output is correct
22 Correct 827 ms 5248 KB Output is correct
23 Correct 793 ms 5368 KB Output is correct
24 Correct 813 ms 5364 KB Output is correct
25 Correct 815 ms 5368 KB Output is correct
26 Correct 919 ms 5364 KB Output is correct
27 Correct 945 ms 5368 KB Output is correct
28 Correct 934 ms 5368 KB Output is correct
29 Correct 925 ms 5368 KB Output is correct
30 Correct 915 ms 5496 KB Output is correct
31 Correct 904 ms 5380 KB Output is correct
32 Correct 916 ms 5368 KB Output is correct
33 Correct 936 ms 5372 KB Output is correct
34 Correct 907 ms 5376 KB Output is correct
35 Correct 910 ms 5408 KB Output is correct
36 Correct 965 ms 5472 KB Output is correct
37 Correct 974 ms 5472 KB Output is correct
38 Correct 936 ms 5404 KB Output is correct
39 Correct 24 ms 5248 KB Output is correct
40 Correct 24 ms 5248 KB Output is correct
41 Correct 24 ms 5248 KB Output is correct
42 Correct 24 ms 5248 KB Output is correct
43 Correct 24 ms 5248 KB Output is correct
44 Correct 24 ms 5248 KB Output is correct
45 Correct 27 ms 5248 KB Output is correct
46 Correct 25 ms 5248 KB Output is correct
47 Correct 27 ms 5248 KB Output is correct
48 Correct 25 ms 5248 KB Output is correct
49 Correct 25 ms 5248 KB Output is correct
50 Correct 24 ms 5248 KB Output is correct
51 Correct 23 ms 5248 KB Output is correct
52 Correct 24 ms 5248 KB Output is correct
53 Correct 24 ms 5248 KB Output is correct
54 Correct 17 ms 5248 KB Output is correct
55 Correct 15 ms 5248 KB Output is correct
56 Correct 16 ms 5248 KB Output is correct
57 Correct 16 ms 5248 KB Output is correct
58 Correct 16 ms 5248 KB Output is correct
59 Correct 1705 ms 5472 KB Output is correct
60 Correct 1686 ms 5496 KB Output is correct
61 Correct 1682 ms 5376 KB Output is correct
62 Correct 1695 ms 5404 KB Output is correct
63 Correct 1693 ms 5496 KB Output is correct
64 Correct 1859 ms 5376 KB Output is correct
65 Correct 1854 ms 5496 KB Output is correct
66 Correct 1860 ms 5376 KB Output is correct
67 Correct 1850 ms 5496 KB Output is correct
68 Correct 1844 ms 5376 KB Output is correct
69 Correct 1612 ms 5380 KB Output is correct
70 Correct 1613 ms 5376 KB Output is correct
71 Correct 1608 ms 5496 KB Output is correct
72 Correct 1638 ms 5376 KB Output is correct
73 Correct 1651 ms 5376 KB Output is correct
74 Correct 253 ms 5248 KB Output is correct
75 Correct 261 ms 5368 KB Output is correct
76 Correct 256 ms 5248 KB Output is correct
77 Correct 254 ms 5248 KB Output is correct
78 Correct 265 ms 5368 KB Output is correct