Submission #1048091

# Submission time Handle Problem Language Result Execution time Memory
1048091 2024-08-07T22:21:21 Z HossamHero7 Boat (APIO16_boat) C++17
100 / 100
1151 ms 2840 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[2][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&1][i] = 1;
    for(int j=new_n-1;j>=0;j--){
        int sz = idx[j].size();
        int dp2[sz+1][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&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]);
            }
        }
        int st = 0;
        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;
            while(st < (int)idx[j].size() && idx[j][st] <= i) {st ++; continue;}
            dp[j&1][i] = dp[j+1&1][i];
            for(int k=1;k<=sz;k++){
                x = mult(x , gap-k+1);
                x = mult(x , inv[k]);
                dp[j&1][i] = add(dp[j&1][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;
}

Compilation message

boat.cpp: In function 'void solve()':
boat.cpp:55:60: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   55 |                 if(k == 1) dp2[i][k] = add(dp2[i][k] , dp[j+1&1][idx[j][i]]);
      |                                                           ~^~
boat.cpp:66:30: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   66 |             dp[j&1][i] = dp[j+1&1][i];
      |                             ~^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 4 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 3 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 4 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 3 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 54 ms 1664 KB Output is correct
22 Correct 55 ms 1624 KB Output is correct
23 Correct 49 ms 1372 KB Output is correct
24 Correct 57 ms 1660 KB Output is correct
25 Correct 54 ms 1616 KB Output is correct
26 Correct 91 ms 2640 KB Output is correct
27 Correct 94 ms 2672 KB Output is correct
28 Correct 91 ms 2644 KB Output is correct
29 Correct 89 ms 2384 KB Output is correct
30 Correct 5 ms 344 KB Output is correct
31 Correct 5 ms 348 KB Output is correct
32 Correct 5 ms 348 KB Output is correct
33 Correct 5 ms 348 KB Output is correct
34 Correct 5 ms 348 KB Output is correct
35 Correct 3 ms 348 KB Output is correct
36 Correct 4 ms 348 KB Output is correct
37 Correct 4 ms 348 KB Output is correct
38 Correct 4 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 348 KB Output is correct
2 Correct 6 ms 524 KB Output is correct
3 Correct 6 ms 348 KB Output is correct
4 Correct 6 ms 348 KB Output is correct
5 Correct 6 ms 552 KB Output is correct
6 Correct 9 ms 580 KB Output is correct
7 Correct 10 ms 348 KB Output is correct
8 Correct 9 ms 348 KB Output is correct
9 Correct 9 ms 348 KB Output is correct
10 Correct 9 ms 556 KB Output is correct
11 Correct 7 ms 348 KB Output is correct
12 Correct 5 ms 348 KB Output is correct
13 Correct 6 ms 348 KB Output is correct
14 Correct 6 ms 348 KB Output is correct
15 Correct 6 ms 516 KB Output is correct
16 Correct 3 ms 348 KB Output is correct
17 Correct 3 ms 348 KB Output is correct
18 Correct 3 ms 348 KB Output is correct
19 Correct 3 ms 348 KB Output is correct
20 Correct 4 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 4 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 3 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 54 ms 1664 KB Output is correct
22 Correct 55 ms 1624 KB Output is correct
23 Correct 49 ms 1372 KB Output is correct
24 Correct 57 ms 1660 KB Output is correct
25 Correct 54 ms 1616 KB Output is correct
26 Correct 91 ms 2640 KB Output is correct
27 Correct 94 ms 2672 KB Output is correct
28 Correct 91 ms 2644 KB Output is correct
29 Correct 89 ms 2384 KB Output is correct
30 Correct 5 ms 344 KB Output is correct
31 Correct 5 ms 348 KB Output is correct
32 Correct 5 ms 348 KB Output is correct
33 Correct 5 ms 348 KB Output is correct
34 Correct 5 ms 348 KB Output is correct
35 Correct 3 ms 348 KB Output is correct
36 Correct 4 ms 348 KB Output is correct
37 Correct 4 ms 348 KB Output is correct
38 Correct 4 ms 348 KB Output is correct
39 Correct 8 ms 348 KB Output is correct
40 Correct 6 ms 524 KB Output is correct
41 Correct 6 ms 348 KB Output is correct
42 Correct 6 ms 348 KB Output is correct
43 Correct 6 ms 552 KB Output is correct
44 Correct 9 ms 580 KB Output is correct
45 Correct 10 ms 348 KB Output is correct
46 Correct 9 ms 348 KB Output is correct
47 Correct 9 ms 348 KB Output is correct
48 Correct 9 ms 556 KB Output is correct
49 Correct 7 ms 348 KB Output is correct
50 Correct 5 ms 348 KB Output is correct
51 Correct 6 ms 348 KB Output is correct
52 Correct 6 ms 348 KB Output is correct
53 Correct 6 ms 516 KB Output is correct
54 Correct 3 ms 348 KB Output is correct
55 Correct 3 ms 348 KB Output is correct
56 Correct 3 ms 348 KB Output is correct
57 Correct 3 ms 348 KB Output is correct
58 Correct 4 ms 348 KB Output is correct
59 Correct 762 ms 1796 KB Output is correct
60 Correct 718 ms 1612 KB Output is correct
61 Correct 725 ms 1444 KB Output is correct
62 Correct 760 ms 1648 KB Output is correct
63 Correct 739 ms 1456 KB Output is correct
64 Correct 1140 ms 2640 KB Output is correct
65 Correct 1143 ms 2608 KB Output is correct
66 Correct 1142 ms 2644 KB Output is correct
67 Correct 1151 ms 2712 KB Output is correct
68 Correct 1136 ms 2840 KB Output is correct
69 Correct 711 ms 1372 KB Output is correct
70 Correct 720 ms 1416 KB Output is correct
71 Correct 717 ms 1648 KB Output is correct
72 Correct 723 ms 1368 KB Output is correct
73 Correct 726 ms 1368 KB Output is correct
74 Correct 80 ms 916 KB Output is correct
75 Correct 77 ms 904 KB Output is correct
76 Correct 81 ms 868 KB Output is correct
77 Correct 82 ms 920 KB Output is correct
78 Correct 82 ms 856 KB Output is correct