Submission #589540

#TimeUsernameProblemLanguageResultExecution timeMemory
589540chonkaBoat (APIO16_boat)C++17
100 / 100
824 ms3448 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...