Submission #200123

# Submission time Handle Problem Language Result Execution time Memory
200123 2020-02-05T12:20:00 Z ansol4328 Boat (APIO16_boat) C++14
100 / 100
752 ms 6552 KB
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

int n, m[505][2];
ll inv[505], cnt[1505], dp[505][1505], sel[505];
vector<ll> lst;

ll get_pow(ll x, ll p)
{
    if(p==0) return 1;
    if(p==1) return x;
    ll ret=get_pow(x,p>>1);
    (ret*=ret)%=MOD;
    if(p&1) (ret*=x)%=MOD;
    return ret;
}

void init()
{
    ll fac=1;
    inv[0]=1;
    for(ll i=1 ; i<=n ; i++) (fac*=i)%=MOD;
    inv[n]=get_pow(fac,MOD-2);
    for(ll i=n ; i>=1 ; i--) inv[i-1]=(inv[i]*i)%MOD;
    for(int i=1 ; i<=n ; i++){
        if(m[i][0]==1) dp[i][1]=1;
        (dp[i][1]+=dp[i-1][1])%=MOD;
    }
    dp[1][m[1][0]]=1;
    for(int i=m[1][0]+1 ; i<=m[1][1] ; i++) dp[1][i]=(dp[1][i-1]+(lst[i-1]-lst[i-2]))%MOD;
    for(int i=m[1][1]+1 ; i<=(int)lst.size() ; i++) dp[1][i]=dp[1][i-1];
    for(int i=1 ; i<=(int)lst.size() ; i++){
        for(int j=1 ; j<=n ; j++){
            if(m[j][0]<i && i<=m[j][1]) cnt[i]++;
        }
    }
}

void calc_sel(ll L, ll K)
{
    ll lim=min(L,K);
    if(lim<2) return;
    for(ll i=2 ; i<=K ; i++){
        ll v1=1, v2=(L*(L-1))%MOD;
        ll val1, val2;
        sel[i]=(v2*(inv[2]))%MOD;
        for(ll j=3, x1=i-2, x2=L-2 ; j<=min(lim,i) ; j++, x1--, x2--){
            (v1*=x1)%=MOD;
            (v2*=x2)%=MOD;
            val1=(v1*inv[j-2])%MOD, val2=(v2*inv[j])%MOD;
            sel[i]+=(val1*val2)%MOD;
            sel[i]%=MOD;
        }
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=1 ; i<=n ; i++){
        scanf("%d %d",&m[i][0],&m[i][1]);
        lst.push_back((ll)m[i][0]);
        lst.push_back((ll)m[i][0]-1);
        lst.push_back((ll)m[i][1]);
    }
    //contraction
    sort(lst.begin(),lst.end());
    lst.erase(unique(lst.begin(),lst.end()),lst.end());
    for(int i=1 ; i<=n ; i++){
        m[i][0]=lower_bound(lst.begin(),lst.end(),(ll)m[i][0])-lst.begin()+1;
        m[i][1]=lower_bound(lst.begin(),lst.end(),(ll)m[i][1])-lst.begin()+1;
    }
    init();
    //solve
    for(int i=2 ; i<=(int)lst.size() ; i++){
        ll L=lst[i-1]-lst[i-2];
        if(cnt[i]>1) calc_sel(L,cnt[i]);
        for(int j=2 ; j<=n ; j++){
            (dp[j][i]+=dp[j-1][i])%=MOD;
            (dp[j][i]+=dp[j][i-1])%=MOD;
            dp[j][i]=(dp[j][i]-dp[j-1][i-1]+MOD)%MOD;
            if(i<m[j][0] || m[j][1]<i) continue;
            if(i==m[j][0] || L==1){
                (dp[j][i]+=dp[j-1][i-1]+1)%=MOD;
                continue;
            }
            ll val=(dp[j-1][i-1]*L+L)%MOD, c=1;
            for(int k=j-1 ; k>=1 ; k--){
                if(m[k][0]<i && i<=m[k][1]){
                    c++;
                    if(k!=1) (val+=sel[c]*dp[k-1][i-1]+sel[c])%=MOD;
                    else (val+=sel[c])%=MOD;
                }
            }
            (dp[j][i]+=val)%=MOD;
        }
    }
    printf("%lld",dp[n][(int)lst.size()]);
    return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
     ~~~~~^~~~~~~~~
boat.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&m[i][0],&m[i][1]);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 6264 KB Output is correct
2 Correct 19 ms 6264 KB Output is correct
3 Correct 20 ms 6264 KB Output is correct
4 Correct 19 ms 6264 KB Output is correct
5 Correct 19 ms 6392 KB Output is correct
6 Correct 19 ms 6264 KB Output is correct
7 Correct 19 ms 6264 KB Output is correct
8 Correct 19 ms 6268 KB Output is correct
9 Correct 19 ms 6264 KB Output is correct
10 Correct 19 ms 6264 KB Output is correct
11 Correct 19 ms 6236 KB Output is correct
12 Correct 19 ms 6264 KB Output is correct
13 Correct 19 ms 6264 KB Output is correct
14 Correct 18 ms 6268 KB Output is correct
15 Correct 19 ms 6260 KB Output is correct
16 Correct 9 ms 3064 KB Output is correct
17 Correct 9 ms 3064 KB Output is correct
18 Correct 9 ms 3064 KB Output is correct
19 Correct 9 ms 3064 KB Output is correct
20 Correct 9 ms 3064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 6264 KB Output is correct
2 Correct 19 ms 6264 KB Output is correct
3 Correct 20 ms 6264 KB Output is correct
4 Correct 19 ms 6264 KB Output is correct
5 Correct 19 ms 6392 KB Output is correct
6 Correct 19 ms 6264 KB Output is correct
7 Correct 19 ms 6264 KB Output is correct
8 Correct 19 ms 6268 KB Output is correct
9 Correct 19 ms 6264 KB Output is correct
10 Correct 19 ms 6264 KB Output is correct
11 Correct 19 ms 6236 KB Output is correct
12 Correct 19 ms 6264 KB Output is correct
13 Correct 19 ms 6264 KB Output is correct
14 Correct 18 ms 6268 KB Output is correct
15 Correct 19 ms 6260 KB Output is correct
16 Correct 9 ms 3064 KB Output is correct
17 Correct 9 ms 3064 KB Output is correct
18 Correct 9 ms 3064 KB Output is correct
19 Correct 9 ms 3064 KB Output is correct
20 Correct 9 ms 3064 KB Output is correct
21 Correct 107 ms 6264 KB Output is correct
22 Correct 98 ms 6264 KB Output is correct
23 Correct 91 ms 6260 KB Output is correct
24 Correct 101 ms 6264 KB Output is correct
25 Correct 100 ms 6392 KB Output is correct
26 Correct 164 ms 6264 KB Output is correct
27 Correct 171 ms 6264 KB Output is correct
28 Correct 168 ms 6264 KB Output is correct
29 Correct 172 ms 6348 KB Output is correct
30 Correct 23 ms 6264 KB Output is correct
31 Correct 23 ms 6264 KB Output is correct
32 Correct 23 ms 6264 KB Output is correct
33 Correct 23 ms 6264 KB Output is correct
34 Correct 24 ms 6264 KB Output is correct
35 Correct 20 ms 6264 KB Output is correct
36 Correct 20 ms 6264 KB Output is correct
37 Correct 19 ms 6264 KB Output is correct
38 Correct 20 ms 6264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 1016 KB Output is correct
2 Correct 8 ms 1016 KB Output is correct
3 Correct 8 ms 1016 KB Output is correct
4 Correct 9 ms 1016 KB Output is correct
5 Correct 8 ms 1016 KB Output is correct
6 Correct 11 ms 1016 KB Output is correct
7 Correct 12 ms 1016 KB Output is correct
8 Correct 11 ms 1016 KB Output is correct
9 Correct 11 ms 1016 KB Output is correct
10 Correct 12 ms 1016 KB Output is correct
11 Correct 8 ms 1016 KB Output is correct
12 Correct 7 ms 1016 KB Output is correct
13 Correct 9 ms 1144 KB Output is correct
14 Correct 8 ms 1144 KB Output is correct
15 Correct 9 ms 1016 KB Output is correct
16 Correct 7 ms 888 KB Output is correct
17 Correct 7 ms 888 KB Output is correct
18 Correct 7 ms 888 KB Output is correct
19 Correct 6 ms 888 KB Output is correct
20 Correct 7 ms 888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 6264 KB Output is correct
2 Correct 19 ms 6264 KB Output is correct
3 Correct 20 ms 6264 KB Output is correct
4 Correct 19 ms 6264 KB Output is correct
5 Correct 19 ms 6392 KB Output is correct
6 Correct 19 ms 6264 KB Output is correct
7 Correct 19 ms 6264 KB Output is correct
8 Correct 19 ms 6268 KB Output is correct
9 Correct 19 ms 6264 KB Output is correct
10 Correct 19 ms 6264 KB Output is correct
11 Correct 19 ms 6236 KB Output is correct
12 Correct 19 ms 6264 KB Output is correct
13 Correct 19 ms 6264 KB Output is correct
14 Correct 18 ms 6268 KB Output is correct
15 Correct 19 ms 6260 KB Output is correct
16 Correct 9 ms 3064 KB Output is correct
17 Correct 9 ms 3064 KB Output is correct
18 Correct 9 ms 3064 KB Output is correct
19 Correct 9 ms 3064 KB Output is correct
20 Correct 9 ms 3064 KB Output is correct
21 Correct 107 ms 6264 KB Output is correct
22 Correct 98 ms 6264 KB Output is correct
23 Correct 91 ms 6260 KB Output is correct
24 Correct 101 ms 6264 KB Output is correct
25 Correct 100 ms 6392 KB Output is correct
26 Correct 164 ms 6264 KB Output is correct
27 Correct 171 ms 6264 KB Output is correct
28 Correct 168 ms 6264 KB Output is correct
29 Correct 172 ms 6348 KB Output is correct
30 Correct 23 ms 6264 KB Output is correct
31 Correct 23 ms 6264 KB Output is correct
32 Correct 23 ms 6264 KB Output is correct
33 Correct 23 ms 6264 KB Output is correct
34 Correct 24 ms 6264 KB Output is correct
35 Correct 20 ms 6264 KB Output is correct
36 Correct 20 ms 6264 KB Output is correct
37 Correct 19 ms 6264 KB Output is correct
38 Correct 20 ms 6264 KB Output is correct
39 Correct 8 ms 1016 KB Output is correct
40 Correct 8 ms 1016 KB Output is correct
41 Correct 8 ms 1016 KB Output is correct
42 Correct 9 ms 1016 KB Output is correct
43 Correct 8 ms 1016 KB Output is correct
44 Correct 11 ms 1016 KB Output is correct
45 Correct 12 ms 1016 KB Output is correct
46 Correct 11 ms 1016 KB Output is correct
47 Correct 11 ms 1016 KB Output is correct
48 Correct 12 ms 1016 KB Output is correct
49 Correct 8 ms 1016 KB Output is correct
50 Correct 7 ms 1016 KB Output is correct
51 Correct 9 ms 1144 KB Output is correct
52 Correct 8 ms 1144 KB Output is correct
53 Correct 9 ms 1016 KB Output is correct
54 Correct 7 ms 888 KB Output is correct
55 Correct 7 ms 888 KB Output is correct
56 Correct 7 ms 888 KB Output is correct
57 Correct 6 ms 888 KB Output is correct
58 Correct 7 ms 888 KB Output is correct
59 Correct 326 ms 6340 KB Output is correct
60 Correct 296 ms 6264 KB Output is correct
61 Correct 285 ms 6248 KB Output is correct
62 Correct 331 ms 6264 KB Output is correct
63 Correct 311 ms 6316 KB Output is correct
64 Correct 725 ms 6448 KB Output is correct
65 Correct 709 ms 6264 KB Output is correct
66 Correct 752 ms 6384 KB Output is correct
67 Correct 725 ms 6552 KB Output is correct
68 Correct 734 ms 6392 KB Output is correct
69 Correct 285 ms 6264 KB Output is correct
70 Correct 297 ms 6476 KB Output is correct
71 Correct 305 ms 6264 KB Output is correct
72 Correct 312 ms 6520 KB Output is correct
73 Correct 297 ms 6264 KB Output is correct
74 Correct 42 ms 3064 KB Output is correct
75 Correct 38 ms 3064 KB Output is correct
76 Correct 45 ms 3192 KB Output is correct
77 Correct 38 ms 3192 KB Output is correct
78 Correct 44 ms 3064 KB Output is correct