답안 #699809

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
699809 2023-02-18T04:11:59 Z Abrar_Al_Samit Boat (APIO16_boat) C++17
100 / 100
1798 ms 65668 KB
#include<bits/stdc++.h>
using namespace std;

#define ub upper_bound
const int nax = 503;
const int mod = 1e9 + 7;
const int cax = 1e6 + 3;
const long long sux = 103217;

vector<int>ind[nax*2];
int a[nax], b[nax];
int n, m;
int dp[nax*2][nax];
int inv[nax];
int fac[nax], iv[nax];
vector<int>imp;

int add(int a, int b) {
  a += b;
  if(a>=mod) a -= mod;
  return a;
}
int mul(int a, int b) {
  return (a * 1LL * b) % mod;
}
int quickpow(int a, int b) {
  int ret = 1;
  while(b) {
    if(b&1) ret = mul(ret, a);
    a = mul(a, a);
    b >>= 1;
  }
  return ret;
}
vector<pair<long long,int>>table[cax];
int C(int n, int r) {
  if(n<nax) {
    return mul(fac[n], mul(iv[r], iv[n-r]));
  }

  long long H = n * sux + r;
  int H2 = H % cax;
  for(auto x : table[H2]) {
    if(x.first==H) return x.second;
  }


  int ret = 1;
  for(int i=n-r+1; i<=n; ++i) {
    ret = mul(ret, i);
  }
  for(int i=1; i<=r; ++i) {
    ret = mul(ret, inv[i]);
  }
  table[H2].emplace_back(H, ret);
  return ret;
}

vector<pair<long long,int>>andp[cax];
int get(int len, int op) {
  long long H = len * sux + op;
  int H2 = H % cax;
  for(auto x : andp[H2]) {
    if(x.first==H) return x.second;
  }

  int ret = 0;
  for(int take=0, to=min(op, len-1); take<=to; ++take) {
    ret = add(ret, mul(C(op, take), C(len, take+1)));
  }

  andp[H2].emplace_back(H, ret);
  return ret;
}
int solve(int i, int prv) {
  if(i==m-1) return prv>0;
  int &ret = dp[i][prv];
  if(ret!=-1) return ret;
  ret = 0;

  ret = solve(i+1, prv);
  int len = imp[i+1]-imp[i];

  for(int mx=ub(ind[i].begin(), ind[i].end(), prv)-ind[i].begin(), 
    cur_op=0, sz=ind[i].size(); mx<sz; ++mx, ++cur_op) {

    if(ind[i][mx]<=prv) continue;

    int cur = mul(solve(i+1, ind[i][mx]), get(len, cur_op));
    ret = add(ret, cur);
  }
  return ret;
}
void PlayGround() {
  cin>>n;
  set<int>pts;
  for(int i=1; i<=n; ++i) {
    cin>>a[i]>>b[i];
    pts.insert(a[i]);
    pts.insert(b[i]+1);
  }

  for(int x : pts) {
    imp.push_back(x);
  } 
  m = imp.size();
  for(int i=0; i<m; ++i) {
    for(int j=1; j<=n; ++j) {
      if(imp[i]>=a[j] && imp[i]<=b[j]) {
        ind[i].push_back(j);
      }
    }
  }
  for(int i=1; i<nax; ++i) {
    inv[i] = quickpow(i, mod-2);
  }
  fac[0] = iv[0] = 1;
  for(int i=1; i<nax; ++i) {
    fac[i] = mul(i, fac[i-1]);
    iv[i] = mul(iv[i-1], inv[i]);
  }
  memset(dp, -1, sizeof dp);
  cout<<solve(0, 0)<<'\n';


  // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  PlayGround();
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 49364 KB Output is correct
2 Correct 29 ms 49388 KB Output is correct
3 Correct 29 ms 49380 KB Output is correct
4 Correct 28 ms 49404 KB Output is correct
5 Correct 29 ms 49364 KB Output is correct
6 Correct 30 ms 49376 KB Output is correct
7 Correct 33 ms 49368 KB Output is correct
8 Correct 29 ms 49452 KB Output is correct
9 Correct 30 ms 49380 KB Output is correct
10 Correct 29 ms 49372 KB Output is correct
11 Correct 29 ms 49368 KB Output is correct
12 Correct 32 ms 49364 KB Output is correct
13 Correct 30 ms 49460 KB Output is correct
14 Correct 29 ms 49384 KB Output is correct
15 Correct 30 ms 49364 KB Output is correct
16 Correct 26 ms 49216 KB Output is correct
17 Correct 27 ms 49244 KB Output is correct
18 Correct 26 ms 49368 KB Output is correct
19 Correct 25 ms 49308 KB Output is correct
20 Correct 25 ms 49228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 49364 KB Output is correct
2 Correct 29 ms 49388 KB Output is correct
3 Correct 29 ms 49380 KB Output is correct
4 Correct 28 ms 49404 KB Output is correct
5 Correct 29 ms 49364 KB Output is correct
6 Correct 30 ms 49376 KB Output is correct
7 Correct 33 ms 49368 KB Output is correct
8 Correct 29 ms 49452 KB Output is correct
9 Correct 30 ms 49380 KB Output is correct
10 Correct 29 ms 49372 KB Output is correct
11 Correct 29 ms 49368 KB Output is correct
12 Correct 32 ms 49364 KB Output is correct
13 Correct 30 ms 49460 KB Output is correct
14 Correct 29 ms 49384 KB Output is correct
15 Correct 30 ms 49364 KB Output is correct
16 Correct 26 ms 49216 KB Output is correct
17 Correct 27 ms 49244 KB Output is correct
18 Correct 26 ms 49368 KB Output is correct
19 Correct 25 ms 49308 KB Output is correct
20 Correct 25 ms 49228 KB Output is correct
21 Correct 357 ms 50468 KB Output is correct
22 Correct 341 ms 50608 KB Output is correct
23 Correct 318 ms 50348 KB Output is correct
24 Correct 340 ms 50536 KB Output is correct
25 Correct 335 ms 50432 KB Output is correct
26 Correct 661 ms 50908 KB Output is correct
27 Correct 674 ms 50884 KB Output is correct
28 Correct 730 ms 50852 KB Output is correct
29 Correct 661 ms 50884 KB Output is correct
30 Correct 34 ms 49492 KB Output is correct
31 Correct 33 ms 49468 KB Output is correct
32 Correct 35 ms 49516 KB Output is correct
33 Correct 34 ms 49468 KB Output is correct
34 Correct 34 ms 49376 KB Output is correct
35 Correct 31 ms 49500 KB Output is correct
36 Correct 31 ms 49492 KB Output is correct
37 Correct 31 ms 49492 KB Output is correct
38 Correct 31 ms 49420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 49868 KB Output is correct
2 Correct 30 ms 49700 KB Output is correct
3 Correct 30 ms 49644 KB Output is correct
4 Correct 31 ms 49708 KB Output is correct
5 Correct 32 ms 49748 KB Output is correct
6 Correct 39 ms 49896 KB Output is correct
7 Correct 38 ms 49900 KB Output is correct
8 Correct 39 ms 49972 KB Output is correct
9 Correct 38 ms 49864 KB Output is correct
10 Correct 39 ms 49992 KB Output is correct
11 Correct 32 ms 49740 KB Output is correct
12 Correct 31 ms 49700 KB Output is correct
13 Correct 30 ms 49756 KB Output is correct
14 Correct 30 ms 49740 KB Output is correct
15 Correct 32 ms 49716 KB Output is correct
16 Correct 32 ms 49396 KB Output is correct
17 Correct 32 ms 49420 KB Output is correct
18 Correct 27 ms 49492 KB Output is correct
19 Correct 27 ms 49480 KB Output is correct
20 Correct 27 ms 49440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 49364 KB Output is correct
2 Correct 29 ms 49388 KB Output is correct
3 Correct 29 ms 49380 KB Output is correct
4 Correct 28 ms 49404 KB Output is correct
5 Correct 29 ms 49364 KB Output is correct
6 Correct 30 ms 49376 KB Output is correct
7 Correct 33 ms 49368 KB Output is correct
8 Correct 29 ms 49452 KB Output is correct
9 Correct 30 ms 49380 KB Output is correct
10 Correct 29 ms 49372 KB Output is correct
11 Correct 29 ms 49368 KB Output is correct
12 Correct 32 ms 49364 KB Output is correct
13 Correct 30 ms 49460 KB Output is correct
14 Correct 29 ms 49384 KB Output is correct
15 Correct 30 ms 49364 KB Output is correct
16 Correct 26 ms 49216 KB Output is correct
17 Correct 27 ms 49244 KB Output is correct
18 Correct 26 ms 49368 KB Output is correct
19 Correct 25 ms 49308 KB Output is correct
20 Correct 25 ms 49228 KB Output is correct
21 Correct 357 ms 50468 KB Output is correct
22 Correct 341 ms 50608 KB Output is correct
23 Correct 318 ms 50348 KB Output is correct
24 Correct 340 ms 50536 KB Output is correct
25 Correct 335 ms 50432 KB Output is correct
26 Correct 661 ms 50908 KB Output is correct
27 Correct 674 ms 50884 KB Output is correct
28 Correct 730 ms 50852 KB Output is correct
29 Correct 661 ms 50884 KB Output is correct
30 Correct 34 ms 49492 KB Output is correct
31 Correct 33 ms 49468 KB Output is correct
32 Correct 35 ms 49516 KB Output is correct
33 Correct 34 ms 49468 KB Output is correct
34 Correct 34 ms 49376 KB Output is correct
35 Correct 31 ms 49500 KB Output is correct
36 Correct 31 ms 49492 KB Output is correct
37 Correct 31 ms 49492 KB Output is correct
38 Correct 31 ms 49420 KB Output is correct
39 Correct 35 ms 49868 KB Output is correct
40 Correct 30 ms 49700 KB Output is correct
41 Correct 30 ms 49644 KB Output is correct
42 Correct 31 ms 49708 KB Output is correct
43 Correct 32 ms 49748 KB Output is correct
44 Correct 39 ms 49896 KB Output is correct
45 Correct 38 ms 49900 KB Output is correct
46 Correct 39 ms 49972 KB Output is correct
47 Correct 38 ms 49864 KB Output is correct
48 Correct 39 ms 49992 KB Output is correct
49 Correct 32 ms 49740 KB Output is correct
50 Correct 31 ms 49700 KB Output is correct
51 Correct 30 ms 49756 KB Output is correct
52 Correct 30 ms 49740 KB Output is correct
53 Correct 32 ms 49716 KB Output is correct
54 Correct 32 ms 49396 KB Output is correct
55 Correct 32 ms 49420 KB Output is correct
56 Correct 27 ms 49492 KB Output is correct
57 Correct 27 ms 49480 KB Output is correct
58 Correct 27 ms 49440 KB Output is correct
59 Correct 840 ms 60576 KB Output is correct
60 Correct 799 ms 60052 KB Output is correct
61 Correct 747 ms 59724 KB Output is correct
62 Correct 861 ms 60724 KB Output is correct
63 Correct 817 ms 60352 KB Output is correct
64 Correct 1763 ms 65456 KB Output is correct
65 Correct 1788 ms 65384 KB Output is correct
66 Correct 1743 ms 65668 KB Output is correct
67 Correct 1798 ms 65508 KB Output is correct
68 Correct 1784 ms 65548 KB Output is correct
69 Correct 818 ms 59840 KB Output is correct
70 Correct 817 ms 59916 KB Output is correct
71 Correct 878 ms 59864 KB Output is correct
72 Correct 834 ms 60276 KB Output is correct
73 Correct 859 ms 60184 KB Output is correct
74 Correct 137 ms 50464 KB Output is correct
75 Correct 130 ms 50400 KB Output is correct
76 Correct 135 ms 50508 KB Output is correct
77 Correct 132 ms 50456 KB Output is correct
78 Correct 140 ms 50680 KB Output is correct