Submission #1048086

# Submission time Handle Problem Language Result Execution time Memory
1048086 2024-08-07T22:09:04 Z HossamHero7 Boat (APIO16_boat) C++14
100 / 100
1241 ms 4312 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int mod = 1e9 + 7;
int add(int a,int b){
    return (a + b >= mod ? a + b - mod : a + b);
}
int sub(int a,int b){
    return (a - b >= 0 ? a - b : a - b + mod);
}
int mult(ll a,ll b){
	return (a*b)%mod;
}
int power(int a,int b){
	if(b == 0) return 1;
	if(b == 1) return a;
	int r = power(a,b/2);
	if(b&1) return mult(mult(r,r),a);
	return mult(r,r);
}
int divide(int a,int b){
    return mult(a , power(b,mod-2));
}
int dp[1005][505];
void solve(){
    int n;
    cin>>n;
    vector<int> a(n),b(n);
    vector<int> v;
    for(int i=0;i<n;i++){
        cin>>a[i]>>b[i];
        v.push_back(a[i]);
        v.push_back(b[i]+1);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()) , v.end());
    int new_n = v.size();
    vector<vector<int>> idx(new_n+1);
    for(int i=0;i<n;i++){
        int l = lower_bound(v.begin(),v.end(),a[i]) - v.begin();
        int r = upper_bound(v.begin(),v.end(),b[i]) - v.begin();
        for(int j=l;j<r;j++){
            idx[j].push_back(i+1);
        }
    }
    vector<int> inv(n+1);
    for(int i=1;i<=n;i++) inv[i] = divide(1,i);
    for(int i=1;i<=n;i++) dp[new_n][i] = 1;
    for(int j=new_n-1;j>=0;j--){
        int sz = idx[j].size();
        vector<vector<int>> dp2(sz+1,vector<int>(sz+1));
        for(int i=sz-1;i>=0;i--){
            for(int k=1;k<=sz;k++){
                if(k == 1) dp2[i][k] = add(dp2[i][k] , dp[j+1][idx[j][i]]);
                else dp2[i][k] = add(dp2[i][k] , dp2[i+1][k-1]);
                dp2[i][k] = add(dp2[i][k] , dp2[i+1][k]);
            }
        }
        for(int i=0;i<=n;i++){
            int gap = (j < new_n - 1 ? v[j+1] - v[j] : 0);
            sz = min(sz , gap);
            int x = 1;
            int st = upper_bound(idx[j].begin(),idx[j].end(),i) - idx[j].begin();
            dp[j][i] = dp[j+1][i];
            for(int k=1;k<=sz;k++){
                x = mult(x , gap-k+1);
                x = mult(x , inv[k]);
                dp[j][i] = add(dp[j][i],mult(dp2[st][k],x));
            }
        }
    }
    cout<<dp[0][0]<<endl;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);      cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2392 KB Output is correct
2 Correct 4 ms 2396 KB Output is correct
3 Correct 3 ms 2396 KB Output is correct
4 Correct 3 ms 2396 KB Output is correct
5 Correct 3 ms 2396 KB Output is correct
6 Correct 3 ms 2268 KB Output is correct
7 Correct 3 ms 2396 KB Output is correct
8 Correct 3 ms 2396 KB Output is correct
9 Correct 4 ms 2268 KB Output is correct
10 Correct 3 ms 2396 KB Output is correct
11 Correct 3 ms 2396 KB Output is correct
12 Correct 3 ms 2396 KB Output is correct
13 Correct 3 ms 2396 KB Output is correct
14 Correct 3 ms 2396 KB Output is correct
15 Correct 3 ms 2388 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 748 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2392 KB Output is correct
2 Correct 4 ms 2396 KB Output is correct
3 Correct 3 ms 2396 KB Output is correct
4 Correct 3 ms 2396 KB Output is correct
5 Correct 3 ms 2396 KB Output is correct
6 Correct 3 ms 2268 KB Output is correct
7 Correct 3 ms 2396 KB Output is correct
8 Correct 3 ms 2396 KB Output is correct
9 Correct 4 ms 2268 KB Output is correct
10 Correct 3 ms 2396 KB Output is correct
11 Correct 3 ms 2396 KB Output is correct
12 Correct 3 ms 2396 KB Output is correct
13 Correct 3 ms 2396 KB Output is correct
14 Correct 3 ms 2396 KB Output is correct
15 Correct 3 ms 2388 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 748 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 80 ms 3496 KB Output is correct
22 Correct 76 ms 3512 KB Output is correct
23 Correct 72 ms 3284 KB Output is correct
24 Correct 76 ms 3324 KB Output is correct
25 Correct 78 ms 3392 KB Output is correct
26 Correct 158 ms 3984 KB Output is correct
27 Correct 165 ms 4016 KB Output is correct
28 Correct 157 ms 4000 KB Output is correct
29 Correct 152 ms 3864 KB Output is correct
30 Correct 6 ms 2396 KB Output is correct
31 Correct 6 ms 2400 KB Output is correct
32 Correct 6 ms 2396 KB Output is correct
33 Correct 6 ms 2396 KB Output is correct
34 Correct 6 ms 2400 KB Output is correct
35 Correct 5 ms 2396 KB Output is correct
36 Correct 5 ms 2396 KB Output is correct
37 Correct 4 ms 2396 KB Output is correct
38 Correct 4 ms 2480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 860 KB Output is correct
2 Correct 6 ms 860 KB Output is correct
3 Correct 6 ms 860 KB Output is correct
4 Correct 6 ms 860 KB Output is correct
5 Correct 7 ms 860 KB Output is correct
6 Correct 10 ms 860 KB Output is correct
7 Correct 10 ms 912 KB Output is correct
8 Correct 10 ms 1020 KB Output is correct
9 Correct 12 ms 884 KB Output is correct
10 Correct 11 ms 860 KB Output is correct
11 Correct 7 ms 860 KB Output is correct
12 Correct 6 ms 860 KB Output is correct
13 Correct 8 ms 860 KB Output is correct
14 Correct 8 ms 860 KB Output is correct
15 Correct 7 ms 860 KB Output is correct
16 Correct 3 ms 600 KB Output is correct
17 Correct 3 ms 604 KB Output is correct
18 Correct 3 ms 604 KB Output is correct
19 Correct 3 ms 604 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2392 KB Output is correct
2 Correct 4 ms 2396 KB Output is correct
3 Correct 3 ms 2396 KB Output is correct
4 Correct 3 ms 2396 KB Output is correct
5 Correct 3 ms 2396 KB Output is correct
6 Correct 3 ms 2268 KB Output is correct
7 Correct 3 ms 2396 KB Output is correct
8 Correct 3 ms 2396 KB Output is correct
9 Correct 4 ms 2268 KB Output is correct
10 Correct 3 ms 2396 KB Output is correct
11 Correct 3 ms 2396 KB Output is correct
12 Correct 3 ms 2396 KB Output is correct
13 Correct 3 ms 2396 KB Output is correct
14 Correct 3 ms 2396 KB Output is correct
15 Correct 3 ms 2388 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 860 KB Output is correct
18 Correct 1 ms 748 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 80 ms 3496 KB Output is correct
22 Correct 76 ms 3512 KB Output is correct
23 Correct 72 ms 3284 KB Output is correct
24 Correct 76 ms 3324 KB Output is correct
25 Correct 78 ms 3392 KB Output is correct
26 Correct 158 ms 3984 KB Output is correct
27 Correct 165 ms 4016 KB Output is correct
28 Correct 157 ms 4000 KB Output is correct
29 Correct 152 ms 3864 KB Output is correct
30 Correct 6 ms 2396 KB Output is correct
31 Correct 6 ms 2400 KB Output is correct
32 Correct 6 ms 2396 KB Output is correct
33 Correct 6 ms 2396 KB Output is correct
34 Correct 6 ms 2400 KB Output is correct
35 Correct 5 ms 2396 KB Output is correct
36 Correct 5 ms 2396 KB Output is correct
37 Correct 4 ms 2396 KB Output is correct
38 Correct 4 ms 2480 KB Output is correct
39 Correct 7 ms 860 KB Output is correct
40 Correct 6 ms 860 KB Output is correct
41 Correct 6 ms 860 KB Output is correct
42 Correct 6 ms 860 KB Output is correct
43 Correct 7 ms 860 KB Output is correct
44 Correct 10 ms 860 KB Output is correct
45 Correct 10 ms 912 KB Output is correct
46 Correct 10 ms 1020 KB Output is correct
47 Correct 12 ms 884 KB Output is correct
48 Correct 11 ms 860 KB Output is correct
49 Correct 7 ms 860 KB Output is correct
50 Correct 6 ms 860 KB Output is correct
51 Correct 8 ms 860 KB Output is correct
52 Correct 8 ms 860 KB Output is correct
53 Correct 7 ms 860 KB Output is correct
54 Correct 3 ms 600 KB Output is correct
55 Correct 3 ms 604 KB Output is correct
56 Correct 3 ms 604 KB Output is correct
57 Correct 3 ms 604 KB Output is correct
58 Correct 3 ms 604 KB Output is correct
59 Correct 780 ms 3728 KB Output is correct
60 Correct 741 ms 3496 KB Output is correct
61 Correct 715 ms 3492 KB Output is correct
62 Correct 793 ms 3672 KB Output is correct
63 Correct 763 ms 3524 KB Output is correct
64 Correct 1207 ms 4152 KB Output is correct
65 Correct 1229 ms 4312 KB Output is correct
66 Correct 1217 ms 4232 KB Output is correct
67 Correct 1241 ms 4184 KB Output is correct
68 Correct 1201 ms 4064 KB Output is correct
69 Correct 719 ms 3528 KB Output is correct
70 Correct 729 ms 3776 KB Output is correct
71 Correct 731 ms 3588 KB Output is correct
72 Correct 743 ms 3508 KB Output is correct
73 Correct 747 ms 3468 KB Output is correct
74 Correct 85 ms 1292 KB Output is correct
75 Correct 85 ms 1304 KB Output is correct
76 Correct 89 ms 1296 KB Output is correct
77 Correct 86 ms 1316 KB Output is correct
78 Correct 85 ms 1368 KB Output is correct