Submission #589540

# Submission time Handle Problem Language Result Execution time Memory
589540 2022-07-04T20:42:13 Z chonka Boat (APIO16_boat) C++17
100 / 100
824 ms 3448 KB
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const int MAXN = 503 ;
const int MOD = 1e9 + 7 ;
// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

int n ;
pair < int , int > a[ MAXN ] ;
int v[ 2 * MAXN ] ;

ll dp[ MAXN ][ 2 * MAXN ] ;
ll en[ MAXN ] , nw[ MAXN ] ;
ll inv[ MAXN ] ;

ll fastpow ( ll x , ll pw ) {
    ll ret = 1 ;
    while ( pw > 0 ) {
        if ( ( pw % 2 ) == 0 ) {
            x = ( x * x ) % MOD ;
            pw /= 2 ;
        }
        else {
            ret = ( ret * x ) % MOD ;
            -- pw ;
        }
    }
    return ret ;
}
set < int > s ;

void solve ( ) {
    cin >> n ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> a[ i ].first >> a[ i ].second ;
        s.insert ( a[ i ].first ) ;
        s.insert ( a[ i ].second + 1 ) ;
    }
    int k = 0 ;
    for ( auto x : s ) {
        v[ ++ k ] = x ;
    }
    for ( int i = 1 ; i < MAXN ; ++ i ) {
        inv[ i ] = fastpow ( i , MOD - 2 ) ;
    }
    dp[ 0 ][ 0 ] = 1 ;
    for ( int i = 0 ; i <= n ; ++ i ) {
        en[ i ] = nw[ i ] = 1 ;
    }
    for ( int j = 1 ; j <= k ; ++ j ) {
        for ( int i = 1 ; i <= n ; ++ i ) {
            for ( int t = i ; t >= 1 ; -- t ) { 
                dp[ i ][ t ] = dp[ i - 1 ][ t ] ;
                if ( a[ i ].first <= v[ j ] && v[ j + 1 ] <= a[ i ].second + 1 ) {
                    if ( v[ j + 1 ] - v[ j ] - t + 1 > 0 ) {
                        ll coef = 1 ;
                        coef = ( coef * ( v[ j + 1 ] - v[ j ] - t + 1 ) ) % MOD ;
                        coef = ( coef * inv[ t ] ) % MOD ;
                        if ( t > 1 ) {
                            dp[ i ][ t ] = ( dp[ i ][ t ] + dp[ i - 1 ][ t - 1 ] * coef ) % MOD ;
                        }
                        else {
                            dp[ i ][ t ] = ( dp[ i ][ t ] + en[ i - 1 ] * coef ) % MOD ;
                        }
                    }
                }
                nw[ i ] = ( nw[ i ] + dp[ i ][ t ] ) % MOD ;
            }
        }
        for ( int i = 1 ; i <= n ; ++ i ) {
            en[ i ] = nw[ i ] ;
        }
    }
    cout << ( en[ n ] - 1 + MOD ) % MOD << "\n" ;
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ; // cin >> t ; 
    while ( t -- ) { solve ( ) ; }
    return 0 ;
}
# Verdict Execution time Memory Grader output
1 Correct 490 ms 3352 KB Output is correct
2 Correct 516 ms 3404 KB Output is correct
3 Correct 516 ms 3352 KB Output is correct
4 Correct 498 ms 3284 KB Output is correct
5 Correct 516 ms 3352 KB Output is correct
6 Correct 492 ms 3284 KB Output is correct
7 Correct 496 ms 3404 KB Output is correct
8 Correct 515 ms 3448 KB Output is correct
9 Correct 505 ms 3356 KB Output is correct
10 Correct 501 ms 3356 KB Output is correct
11 Correct 514 ms 3356 KB Output is correct
12 Correct 513 ms 3352 KB Output is correct
13 Correct 495 ms 3356 KB Output is correct
14 Correct 501 ms 3404 KB Output is correct
15 Correct 513 ms 3368 KB Output is correct
16 Correct 92 ms 3320 KB Output is correct
17 Correct 95 ms 3316 KB Output is correct
18 Correct 91 ms 3404 KB Output is correct
19 Correct 98 ms 3328 KB Output is correct
20 Correct 92 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 490 ms 3352 KB Output is correct
2 Correct 516 ms 3404 KB Output is correct
3 Correct 516 ms 3352 KB Output is correct
4 Correct 498 ms 3284 KB Output is correct
5 Correct 516 ms 3352 KB Output is correct
6 Correct 492 ms 3284 KB Output is correct
7 Correct 496 ms 3404 KB Output is correct
8 Correct 515 ms 3448 KB Output is correct
9 Correct 505 ms 3356 KB Output is correct
10 Correct 501 ms 3356 KB Output is correct
11 Correct 514 ms 3356 KB Output is correct
12 Correct 513 ms 3352 KB Output is correct
13 Correct 495 ms 3356 KB Output is correct
14 Correct 501 ms 3404 KB Output is correct
15 Correct 513 ms 3368 KB Output is correct
16 Correct 92 ms 3320 KB Output is correct
17 Correct 95 ms 3316 KB Output is correct
18 Correct 91 ms 3404 KB Output is correct
19 Correct 98 ms 3328 KB Output is correct
20 Correct 92 ms 3284 KB Output is correct
21 Correct 490 ms 3348 KB Output is correct
22 Correct 476 ms 3284 KB Output is correct
23 Correct 456 ms 3404 KB Output is correct
24 Correct 466 ms 3284 KB Output is correct
25 Correct 454 ms 3360 KB Output is correct
26 Correct 450 ms 3348 KB Output is correct
27 Correct 470 ms 3348 KB Output is correct
28 Correct 439 ms 3344 KB Output is correct
29 Correct 448 ms 3352 KB Output is correct
30 Correct 485 ms 3368 KB Output is correct
31 Correct 508 ms 3360 KB Output is correct
32 Correct 513 ms 3404 KB Output is correct
33 Correct 488 ms 3356 KB Output is correct
34 Correct 509 ms 3356 KB Output is correct
35 Correct 492 ms 3356 KB Output is correct
36 Correct 490 ms 3352 KB Output is correct
37 Correct 509 ms 3356 KB Output is correct
38 Correct 512 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 724 KB Output is correct
2 Correct 6 ms 724 KB Output is correct
3 Correct 6 ms 724 KB Output is correct
4 Correct 6 ms 724 KB Output is correct
5 Correct 6 ms 724 KB Output is correct
6 Correct 7 ms 724 KB Output is correct
7 Correct 7 ms 724 KB Output is correct
8 Correct 7 ms 724 KB Output is correct
9 Correct 7 ms 808 KB Output is correct
10 Correct 7 ms 808 KB Output is correct
11 Correct 6 ms 724 KB Output is correct
12 Correct 6 ms 724 KB Output is correct
13 Correct 6 ms 812 KB Output is correct
14 Correct 7 ms 724 KB Output is correct
15 Correct 7 ms 724 KB Output is correct
16 Correct 4 ms 724 KB Output is correct
17 Correct 4 ms 724 KB Output is correct
18 Correct 4 ms 724 KB Output is correct
19 Correct 4 ms 804 KB Output is correct
20 Correct 4 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 490 ms 3352 KB Output is correct
2 Correct 516 ms 3404 KB Output is correct
3 Correct 516 ms 3352 KB Output is correct
4 Correct 498 ms 3284 KB Output is correct
5 Correct 516 ms 3352 KB Output is correct
6 Correct 492 ms 3284 KB Output is correct
7 Correct 496 ms 3404 KB Output is correct
8 Correct 515 ms 3448 KB Output is correct
9 Correct 505 ms 3356 KB Output is correct
10 Correct 501 ms 3356 KB Output is correct
11 Correct 514 ms 3356 KB Output is correct
12 Correct 513 ms 3352 KB Output is correct
13 Correct 495 ms 3356 KB Output is correct
14 Correct 501 ms 3404 KB Output is correct
15 Correct 513 ms 3368 KB Output is correct
16 Correct 92 ms 3320 KB Output is correct
17 Correct 95 ms 3316 KB Output is correct
18 Correct 91 ms 3404 KB Output is correct
19 Correct 98 ms 3328 KB Output is correct
20 Correct 92 ms 3284 KB Output is correct
21 Correct 490 ms 3348 KB Output is correct
22 Correct 476 ms 3284 KB Output is correct
23 Correct 456 ms 3404 KB Output is correct
24 Correct 466 ms 3284 KB Output is correct
25 Correct 454 ms 3360 KB Output is correct
26 Correct 450 ms 3348 KB Output is correct
27 Correct 470 ms 3348 KB Output is correct
28 Correct 439 ms 3344 KB Output is correct
29 Correct 448 ms 3352 KB Output is correct
30 Correct 485 ms 3368 KB Output is correct
31 Correct 508 ms 3360 KB Output is correct
32 Correct 513 ms 3404 KB Output is correct
33 Correct 488 ms 3356 KB Output is correct
34 Correct 509 ms 3356 KB Output is correct
35 Correct 492 ms 3356 KB Output is correct
36 Correct 490 ms 3352 KB Output is correct
37 Correct 509 ms 3356 KB Output is correct
38 Correct 512 ms 3284 KB Output is correct
39 Correct 6 ms 724 KB Output is correct
40 Correct 6 ms 724 KB Output is correct
41 Correct 6 ms 724 KB Output is correct
42 Correct 6 ms 724 KB Output is correct
43 Correct 6 ms 724 KB Output is correct
44 Correct 7 ms 724 KB Output is correct
45 Correct 7 ms 724 KB Output is correct
46 Correct 7 ms 724 KB Output is correct
47 Correct 7 ms 808 KB Output is correct
48 Correct 7 ms 808 KB Output is correct
49 Correct 6 ms 724 KB Output is correct
50 Correct 6 ms 724 KB Output is correct
51 Correct 6 ms 812 KB Output is correct
52 Correct 7 ms 724 KB Output is correct
53 Correct 7 ms 724 KB Output is correct
54 Correct 4 ms 724 KB Output is correct
55 Correct 4 ms 724 KB Output is correct
56 Correct 4 ms 724 KB Output is correct
57 Correct 4 ms 804 KB Output is correct
58 Correct 4 ms 724 KB Output is correct
59 Correct 738 ms 3356 KB Output is correct
60 Correct 694 ms 3404 KB Output is correct
61 Correct 694 ms 3356 KB Output is correct
62 Correct 710 ms 3360 KB Output is correct
63 Correct 723 ms 3360 KB Output is correct
64 Correct 824 ms 3352 KB Output is correct
65 Correct 808 ms 3360 KB Output is correct
66 Correct 815 ms 3352 KB Output is correct
67 Correct 813 ms 3356 KB Output is correct
68 Correct 816 ms 3360 KB Output is correct
69 Correct 680 ms 3356 KB Output is correct
70 Correct 655 ms 3352 KB Output is correct
71 Correct 659 ms 3360 KB Output is correct
72 Correct 676 ms 3356 KB Output is correct
73 Correct 681 ms 3376 KB Output is correct
74 Correct 114 ms 3284 KB Output is correct
75 Correct 112 ms 3284 KB Output is correct
76 Correct 117 ms 3316 KB Output is correct
77 Correct 118 ms 3312 KB Output is correct
78 Correct 123 ms 3312 KB Output is correct