Submission #371467

# Submission time Handle Problem Language Result Execution time Memory
371467 2021-02-26T16:49:35 Z BartolM Boat (APIO16_boat) C++17
100 / 100
1550 ms 10540 KB
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;

const int INF=0x3f3f3f3f;
const int N=505;
const int MOD=1e9+7;

int n, m;
int A[N], B[N], povrh[2*N][N], siz[2*N];
pii p[2*N];
set <pii> S;
int dp[2][2*N][N][2];
#define DEBUG 0

int add(int a, int b) {
    a+=b;
    if (a>=MOD) a-=MOD;
    return a;
}

int mul(int a, int b) {
    return (ll)a*b%MOD;
}

int pot(int baza, int ex) {
    int ret=1;
    while (ex) {
        if (ex%2) ret=mul(ret, baza);
        baza=mul(baza, baza);
        ex/=2;
    }
    return ret;
}

int divv(int a, int b) {
    return mul(a, pot(b, MOD-2));
}

void prec() {
    for (int i=0; i<m; ++i) {
        povrh[i][0]=1;
        for (int j=1; j<=min(n, siz[i]); ++j) povrh[i][j]=divv(mul(povrh[i][j-1], siz[i]-j+1), j);
    }
}

void solve() {
//    p[0]=mp(0, S.begin()->X-1);
    m=0;
    for (auto it=S.begin(); it!=S.end(); ++it) {
        if (it==S.begin()) continue;
        auto pr=it; pr--;
        p[m++]=mp(pr->X+pr->Y, it->X-!(it->Y));
    }
    for (int i=0; i<m; ++i) siz[i]=p[i].Y-p[i].X+1;

    #if DEBUG
        printf("m == %d\n", m);
        for (int i=0; i<m; ++i) printf("[%d, %d]\n", p[i].X, p[i].Y);
    #endif // DEBUG

    prec();

    int b=0;
    for (int gr=0; gr<m; ++gr) {
        for (int kol=1; kol<=min(siz[gr], n); ++kol) {
            for (int fl=0; fl<=1; ++fl) dp[b][gr][kol][fl]=povrh[gr][kol];
        }
    }
    b^=1;
    for (int i=n-1; i>=0; --i) {
        memset(dp[b], 0, sizeof dp[b]);
        for (int gr=m-1; gr>=0; --gr) {
            for (int kol=0; kol<=min(siz[gr], i+1); ++kol) {
                for (int fl=0; fl<=1; ++fl) {
                    int &ret=dp[b][gr][kol][fl];
                    ret=0;
                    ret=add(ret, mul(povrh[gr][kol], dp[b][gr+1][0][0]));
                    if (fl) ret=add(ret, dp[!b][gr][kol][1]);
                    if (p[gr].X>=A[i] && p[gr].Y<=B[i])
                        ret=add(ret, dp[!b][gr][kol+1][1]);
                }
            }
        }
        b^=1;
    }

//    int sol=add(rek(0, 0, 0, 1), MOD-1);
    printf("%d\n", dp[!b][0][0][1]);
}

void load() {
    scanf("%d", &n);
    for (int i=0; i<n; ++i) {
        scanf("%d %d", &A[i], &B[i]);
        S.insert(mp(A[i], 0));
        S.insert(mp(B[i], 1));
    }
}

int main() {
    load();
    solve();
    return 0;
}

Compilation message

boat.cpp: In function 'void load()':
boat.cpp:103:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  103 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
boat.cpp:105:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  105 |         scanf("%d %d", &A[i], &B[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 705 ms 10348 KB Output is correct
2 Correct 735 ms 10408 KB Output is correct
3 Correct 771 ms 10476 KB Output is correct
4 Correct 738 ms 10348 KB Output is correct
5 Correct 729 ms 10348 KB Output is correct
6 Correct 729 ms 10276 KB Output is correct
7 Correct 753 ms 10408 KB Output is correct
8 Correct 732 ms 10532 KB Output is correct
9 Correct 794 ms 10496 KB Output is correct
10 Correct 730 ms 10432 KB Output is correct
11 Correct 726 ms 10496 KB Output is correct
12 Correct 723 ms 10476 KB Output is correct
13 Correct 743 ms 10412 KB Output is correct
14 Correct 690 ms 10476 KB Output is correct
15 Correct 711 ms 10476 KB Output is correct
16 Correct 275 ms 8812 KB Output is correct
17 Correct 294 ms 8812 KB Output is correct
18 Correct 287 ms 8684 KB Output is correct
19 Correct 365 ms 8684 KB Output is correct
20 Correct 265 ms 8684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 705 ms 10348 KB Output is correct
2 Correct 735 ms 10408 KB Output is correct
3 Correct 771 ms 10476 KB Output is correct
4 Correct 738 ms 10348 KB Output is correct
5 Correct 729 ms 10348 KB Output is correct
6 Correct 729 ms 10276 KB Output is correct
7 Correct 753 ms 10408 KB Output is correct
8 Correct 732 ms 10532 KB Output is correct
9 Correct 794 ms 10496 KB Output is correct
10 Correct 730 ms 10432 KB Output is correct
11 Correct 726 ms 10496 KB Output is correct
12 Correct 723 ms 10476 KB Output is correct
13 Correct 743 ms 10412 KB Output is correct
14 Correct 690 ms 10476 KB Output is correct
15 Correct 711 ms 10476 KB Output is correct
16 Correct 275 ms 8812 KB Output is correct
17 Correct 294 ms 8812 KB Output is correct
18 Correct 287 ms 8684 KB Output is correct
19 Correct 365 ms 8684 KB Output is correct
20 Correct 265 ms 8684 KB Output is correct
21 Correct 244 ms 10220 KB Output is correct
22 Correct 221 ms 10220 KB Output is correct
23 Correct 229 ms 10348 KB Output is correct
24 Correct 260 ms 10348 KB Output is correct
25 Correct 219 ms 10348 KB Output is correct
26 Correct 223 ms 10220 KB Output is correct
27 Correct 273 ms 10092 KB Output is correct
28 Correct 243 ms 10220 KB Output is correct
29 Correct 225 ms 10092 KB Output is correct
30 Correct 687 ms 10476 KB Output is correct
31 Correct 714 ms 10412 KB Output is correct
32 Correct 734 ms 10408 KB Output is correct
33 Correct 671 ms 10476 KB Output is correct
34 Correct 724 ms 10476 KB Output is correct
35 Correct 717 ms 10368 KB Output is correct
36 Correct 702 ms 10404 KB Output is correct
37 Correct 677 ms 10404 KB Output is correct
38 Correct 730 ms 10400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 8684 KB Output is correct
2 Correct 60 ms 8684 KB Output is correct
3 Correct 51 ms 8812 KB Output is correct
4 Correct 60 ms 8684 KB Output is correct
5 Correct 51 ms 8780 KB Output is correct
6 Correct 51 ms 8684 KB Output is correct
7 Correct 57 ms 8684 KB Output is correct
8 Correct 59 ms 8684 KB Output is correct
9 Correct 58 ms 8812 KB Output is correct
10 Correct 54 ms 8828 KB Output is correct
11 Correct 51 ms 8684 KB Output is correct
12 Correct 52 ms 8684 KB Output is correct
13 Correct 56 ms 8684 KB Output is correct
14 Correct 55 ms 8684 KB Output is correct
15 Correct 62 ms 8684 KB Output is correct
16 Correct 46 ms 8556 KB Output is correct
17 Correct 63 ms 8556 KB Output is correct
18 Correct 60 ms 8556 KB Output is correct
19 Correct 49 ms 8556 KB Output is correct
20 Correct 44 ms 8556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 705 ms 10348 KB Output is correct
2 Correct 735 ms 10408 KB Output is correct
3 Correct 771 ms 10476 KB Output is correct
4 Correct 738 ms 10348 KB Output is correct
5 Correct 729 ms 10348 KB Output is correct
6 Correct 729 ms 10276 KB Output is correct
7 Correct 753 ms 10408 KB Output is correct
8 Correct 732 ms 10532 KB Output is correct
9 Correct 794 ms 10496 KB Output is correct
10 Correct 730 ms 10432 KB Output is correct
11 Correct 726 ms 10496 KB Output is correct
12 Correct 723 ms 10476 KB Output is correct
13 Correct 743 ms 10412 KB Output is correct
14 Correct 690 ms 10476 KB Output is correct
15 Correct 711 ms 10476 KB Output is correct
16 Correct 275 ms 8812 KB Output is correct
17 Correct 294 ms 8812 KB Output is correct
18 Correct 287 ms 8684 KB Output is correct
19 Correct 365 ms 8684 KB Output is correct
20 Correct 265 ms 8684 KB Output is correct
21 Correct 244 ms 10220 KB Output is correct
22 Correct 221 ms 10220 KB Output is correct
23 Correct 229 ms 10348 KB Output is correct
24 Correct 260 ms 10348 KB Output is correct
25 Correct 219 ms 10348 KB Output is correct
26 Correct 223 ms 10220 KB Output is correct
27 Correct 273 ms 10092 KB Output is correct
28 Correct 243 ms 10220 KB Output is correct
29 Correct 225 ms 10092 KB Output is correct
30 Correct 687 ms 10476 KB Output is correct
31 Correct 714 ms 10412 KB Output is correct
32 Correct 734 ms 10408 KB Output is correct
33 Correct 671 ms 10476 KB Output is correct
34 Correct 724 ms 10476 KB Output is correct
35 Correct 717 ms 10368 KB Output is correct
36 Correct 702 ms 10404 KB Output is correct
37 Correct 677 ms 10404 KB Output is correct
38 Correct 730 ms 10400 KB Output is correct
39 Correct 70 ms 8684 KB Output is correct
40 Correct 60 ms 8684 KB Output is correct
41 Correct 51 ms 8812 KB Output is correct
42 Correct 60 ms 8684 KB Output is correct
43 Correct 51 ms 8780 KB Output is correct
44 Correct 51 ms 8684 KB Output is correct
45 Correct 57 ms 8684 KB Output is correct
46 Correct 59 ms 8684 KB Output is correct
47 Correct 58 ms 8812 KB Output is correct
48 Correct 54 ms 8828 KB Output is correct
49 Correct 51 ms 8684 KB Output is correct
50 Correct 52 ms 8684 KB Output is correct
51 Correct 56 ms 8684 KB Output is correct
52 Correct 55 ms 8684 KB Output is correct
53 Correct 62 ms 8684 KB Output is correct
54 Correct 46 ms 8556 KB Output is correct
55 Correct 63 ms 8556 KB Output is correct
56 Correct 60 ms 8556 KB Output is correct
57 Correct 49 ms 8556 KB Output is correct
58 Correct 44 ms 8556 KB Output is correct
59 Correct 1422 ms 10412 KB Output is correct
60 Correct 1391 ms 10412 KB Output is correct
61 Correct 1394 ms 10540 KB Output is correct
62 Correct 1402 ms 10476 KB Output is correct
63 Correct 1387 ms 10476 KB Output is correct
64 Correct 1483 ms 10484 KB Output is correct
65 Correct 1503 ms 10412 KB Output is correct
66 Correct 1525 ms 10348 KB Output is correct
67 Correct 1550 ms 10312 KB Output is correct
68 Correct 1535 ms 10412 KB Output is correct
69 Correct 1334 ms 10460 KB Output is correct
70 Correct 1388 ms 10476 KB Output is correct
71 Correct 1345 ms 10412 KB Output is correct
72 Correct 1406 ms 10448 KB Output is correct
73 Correct 1366 ms 10476 KB Output is correct
74 Correct 328 ms 8812 KB Output is correct
75 Correct 316 ms 8684 KB Output is correct
76 Correct 307 ms 8812 KB Output is correct
77 Correct 349 ms 8684 KB Output is correct
78 Correct 316 ms 8704 KB Output is correct