답안 #1086000

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1086000 2024-09-09T09:23:22 Z quanlt206 Boat (APIO16_boat) C++17
100 / 100
1010 ms 34396 KB
#include<bits/stdc++.h>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;

template<class A, class B>
    bool maximize(A& x, B y) {
        if (x < y) return x = y, true; else return false;
    }
template<class A, class B>
    bool minimize(A& x, B y) {
        if (x > y) return x = y, true; else return false;
    }

/* END OF TEMPLATE */

const int N = 2e3 + 7;
const int mod = 1e9 + 7;

int n, m, a[N], b[N];

vector<int> E;

pii block[N];

int dp[2][2000][2000][2];

void add(int& x, int y) {
    if ((x+=y) >= mod) x-=mod;
}

int Ckn[N][N];

long long fact[N], inv_fact[N];

long long mul(long long a, long long b) {
    if (b == 0) return 1;
    ll tmp = mul(a, b / 2);
    tmp = tmp * tmp % mod;
    if (b & 1) tmp = tmp * a % mod;
    return tmp;
}

void buildCkn() {
    fact[0] = 1;
    for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % mod;
    inv_fact[n] = mul(fact[n], mod - 2);
    for (int i = n - 1; i > 0; i--) inv_fact[i] = inv_fact[i + 1] * (i + 1) % mod;
    for (int i = 1; i <= m; i++) {
        int sz = block[i].Y - block[i].X + 1;
        ll tich = 1;
        for (int j = sz; j > 0; j--) {
            tich = tich * j % mod;
            Ckn[i][sz - j + 1] = tich * inv_fact[sz - j + 1] % mod;
//            cout<<sz<<" "<<sz - j + 1<<" "<< Ckn[i][sz - j + 1]<<"\n";
            if (sz - j + 1 > n) break;
        }
    }
}

int cnt[N];

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    set<int> s;
    for (int i = 1; i <= n; i++) {
        cin>>a[i]>>b[i];
        s.insert(a[i]);
        s.insert(b[i]);
    }
    vector<int> E(s.begin(), s.end());
    for (int i = 0; i < E.size(); i++) {
        block[++m] = {E[i], E[i]};
        if (i + 1 < E.size() && E[i] + 1 <= E[i + 1] - 1) block[++m] = {E[i] + 1, E[i + 1] - 1};
    }
    buildCkn();
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j <= m; j++)
            if (a[i] <= block[j].X && block[j].Y <= b[i]) cnt[j]++;
    }
    cnt[0] = n;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++)
            if (a[i] <= block[j].X && block[j].Y <= b[i]) cnt[j]--;
        for (int j = 0; j <= m; j++)
            for (int k = 0; k <= cnt[j]; k++)
                for (int z = 0; z <= 1; z++) {
                    int& res = dp[i & 1][j][k][z];
                    res = 0;
                    if (i == 0) {
                        res = Ckn[j][k];
                        continue;
                    }
                    if (a[i] <= block[j].X && block[j].Y <= b[i]) add(res, dp[i & 1 ^ 1][j][k + 1][0]);
                    if (z == 0) add(res, dp[i & 1 ^ 1][j][k][0]);
                    if (j > 0 && k > 0) add(res, 1LL * dp[i & 1][j - 1][0][1] * Ckn[j][k] % mod); else
                    if (j > 0 && k == 0) add(res, dp[i & 1][j - 1][0][1]);
                }
    }
    cout<<dp[n & 1][m][0][0];
    return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:100:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     for (int i = 0; i < E.size(); i++) {
      |                     ~~^~~~~~~~~~
boat.cpp:102:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |         if (i + 1 < E.size() && E[i] + 1 <= E[i + 1] - 1) block[++m] = {E[i] + 1, E[i + 1] - 1};
      |             ~~~~~~^~~~~~~~~~
boat.cpp:122:81: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  122 |                     if (a[i] <= block[j].X && block[j].Y <= b[i]) add(res, dp[i & 1 ^ 1][j][k + 1][0]);
      |                                                                               ~~^~~
boat.cpp:123:47: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  123 |                     if (z == 0) add(res, dp[i & 1 ^ 1][j][k][0]);
      |                                             ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 13400 KB Output is correct
2 Correct 14 ms 13404 KB Output is correct
3 Correct 15 ms 13404 KB Output is correct
4 Correct 14 ms 13404 KB Output is correct
5 Correct 14 ms 13616 KB Output is correct
6 Correct 14 ms 13420 KB Output is correct
7 Correct 13 ms 13620 KB Output is correct
8 Correct 14 ms 13404 KB Output is correct
9 Correct 13 ms 13616 KB Output is correct
10 Correct 14 ms 13404 KB Output is correct
11 Correct 13 ms 13624 KB Output is correct
12 Correct 16 ms 13624 KB Output is correct
13 Correct 14 ms 13404 KB Output is correct
14 Correct 14 ms 13448 KB Output is correct
15 Correct 14 ms 13404 KB Output is correct
16 Correct 4 ms 2652 KB Output is correct
17 Correct 4 ms 2908 KB Output is correct
18 Correct 6 ms 2912 KB Output is correct
19 Correct 5 ms 2908 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 13400 KB Output is correct
2 Correct 14 ms 13404 KB Output is correct
3 Correct 15 ms 13404 KB Output is correct
4 Correct 14 ms 13404 KB Output is correct
5 Correct 14 ms 13616 KB Output is correct
6 Correct 14 ms 13420 KB Output is correct
7 Correct 13 ms 13620 KB Output is correct
8 Correct 14 ms 13404 KB Output is correct
9 Correct 13 ms 13616 KB Output is correct
10 Correct 14 ms 13404 KB Output is correct
11 Correct 13 ms 13624 KB Output is correct
12 Correct 16 ms 13624 KB Output is correct
13 Correct 14 ms 13404 KB Output is correct
14 Correct 14 ms 13448 KB Output is correct
15 Correct 14 ms 13404 KB Output is correct
16 Correct 4 ms 2652 KB Output is correct
17 Correct 4 ms 2908 KB Output is correct
18 Correct 6 ms 2912 KB Output is correct
19 Correct 5 ms 2908 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 560 ms 24924 KB Output is correct
22 Correct 519 ms 24924 KB Output is correct
23 Correct 504 ms 24456 KB Output is correct
24 Correct 542 ms 24924 KB Output is correct
25 Correct 512 ms 24408 KB Output is correct
26 Correct 730 ms 25180 KB Output is correct
27 Correct 743 ms 25436 KB Output is correct
28 Correct 732 ms 25432 KB Output is correct
29 Correct 735 ms 25692 KB Output is correct
30 Correct 31 ms 23900 KB Output is correct
31 Correct 32 ms 23640 KB Output is correct
32 Correct 30 ms 23728 KB Output is correct
33 Correct 29 ms 23640 KB Output is correct
34 Correct 30 ms 23640 KB Output is correct
35 Correct 16 ms 13404 KB Output is correct
36 Correct 16 ms 13400 KB Output is correct
37 Correct 16 ms 13404 KB Output is correct
38 Correct 16 ms 13404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 5468 KB Output is correct
2 Correct 6 ms 5468 KB Output is correct
3 Correct 7 ms 5464 KB Output is correct
4 Correct 8 ms 5468 KB Output is correct
5 Correct 7 ms 5552 KB Output is correct
6 Correct 10 ms 5464 KB Output is correct
7 Correct 10 ms 5664 KB Output is correct
8 Correct 10 ms 5668 KB Output is correct
9 Correct 10 ms 5468 KB Output is correct
10 Correct 12 ms 5720 KB Output is correct
11 Correct 7 ms 5468 KB Output is correct
12 Correct 6 ms 5536 KB Output is correct
13 Correct 6 ms 5468 KB Output is correct
14 Correct 7 ms 5464 KB Output is correct
15 Correct 7 ms 5580 KB Output is correct
16 Correct 3 ms 2652 KB Output is correct
17 Correct 3 ms 2652 KB Output is correct
18 Correct 3 ms 2648 KB Output is correct
19 Correct 3 ms 2516 KB Output is correct
20 Correct 4 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 13400 KB Output is correct
2 Correct 14 ms 13404 KB Output is correct
3 Correct 15 ms 13404 KB Output is correct
4 Correct 14 ms 13404 KB Output is correct
5 Correct 14 ms 13616 KB Output is correct
6 Correct 14 ms 13420 KB Output is correct
7 Correct 13 ms 13620 KB Output is correct
8 Correct 14 ms 13404 KB Output is correct
9 Correct 13 ms 13616 KB Output is correct
10 Correct 14 ms 13404 KB Output is correct
11 Correct 13 ms 13624 KB Output is correct
12 Correct 16 ms 13624 KB Output is correct
13 Correct 14 ms 13404 KB Output is correct
14 Correct 14 ms 13448 KB Output is correct
15 Correct 14 ms 13404 KB Output is correct
16 Correct 4 ms 2652 KB Output is correct
17 Correct 4 ms 2908 KB Output is correct
18 Correct 6 ms 2912 KB Output is correct
19 Correct 5 ms 2908 KB Output is correct
20 Correct 4 ms 2908 KB Output is correct
21 Correct 560 ms 24924 KB Output is correct
22 Correct 519 ms 24924 KB Output is correct
23 Correct 504 ms 24456 KB Output is correct
24 Correct 542 ms 24924 KB Output is correct
25 Correct 512 ms 24408 KB Output is correct
26 Correct 730 ms 25180 KB Output is correct
27 Correct 743 ms 25436 KB Output is correct
28 Correct 732 ms 25432 KB Output is correct
29 Correct 735 ms 25692 KB Output is correct
30 Correct 31 ms 23900 KB Output is correct
31 Correct 32 ms 23640 KB Output is correct
32 Correct 30 ms 23728 KB Output is correct
33 Correct 29 ms 23640 KB Output is correct
34 Correct 30 ms 23640 KB Output is correct
35 Correct 16 ms 13404 KB Output is correct
36 Correct 16 ms 13400 KB Output is correct
37 Correct 16 ms 13404 KB Output is correct
38 Correct 16 ms 13404 KB Output is correct
39 Correct 8 ms 5468 KB Output is correct
40 Correct 6 ms 5468 KB Output is correct
41 Correct 7 ms 5464 KB Output is correct
42 Correct 8 ms 5468 KB Output is correct
43 Correct 7 ms 5552 KB Output is correct
44 Correct 10 ms 5464 KB Output is correct
45 Correct 10 ms 5664 KB Output is correct
46 Correct 10 ms 5668 KB Output is correct
47 Correct 10 ms 5468 KB Output is correct
48 Correct 12 ms 5720 KB Output is correct
49 Correct 7 ms 5468 KB Output is correct
50 Correct 6 ms 5536 KB Output is correct
51 Correct 6 ms 5468 KB Output is correct
52 Correct 7 ms 5464 KB Output is correct
53 Correct 7 ms 5580 KB Output is correct
54 Correct 3 ms 2652 KB Output is correct
55 Correct 3 ms 2652 KB Output is correct
56 Correct 3 ms 2648 KB Output is correct
57 Correct 3 ms 2516 KB Output is correct
58 Correct 4 ms 2652 KB Output is correct
59 Correct 648 ms 31740 KB Output is correct
60 Correct 635 ms 31444 KB Output is correct
61 Correct 588 ms 31324 KB Output is correct
62 Correct 663 ms 31780 KB Output is correct
63 Correct 649 ms 31388 KB Output is correct
64 Correct 1010 ms 34196 KB Output is correct
65 Correct 951 ms 34192 KB Output is correct
66 Correct 996 ms 34140 KB Output is correct
67 Correct 969 ms 34396 KB Output is correct
68 Correct 931 ms 34192 KB Output is correct
69 Correct 495 ms 31324 KB Output is correct
70 Correct 496 ms 31384 KB Output is correct
71 Correct 474 ms 31388 KB Output is correct
72 Correct 496 ms 31324 KB Output is correct
73 Correct 527 ms 31320 KB Output is correct
74 Correct 69 ms 3672 KB Output is correct
75 Correct 61 ms 3432 KB Output is correct
76 Correct 63 ms 3676 KB Output is correct
77 Correct 72 ms 3684 KB Output is correct
78 Correct 65 ms 3672 KB Output is correct