#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, long long> ii;
long long mod = 1000000007;
long long modInverse(long long a, long long m){
long long m0 = m;
long long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1){
// q is quotient
long long q = a / m;
long long t = m;
// m is remainder now, process same as
// Euclid's algo
m = a % m, a = t;
t = y;
// Update y and x
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
static long long a[505];
static long long b[505];
static long long inverse[505];
static ii ranges[1010];
static long long diff[1010];
static long long posa[505];
static long long posb[505];
static long long dp[2][2005][505];
static long long acc[1010];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
for(long long i = 1;i < 505;i++){
inverse[i] = modInverse(i,mod);
}
int n;
cin >> n;
vector<long long> s;
for(int i = 0;i < n;i++){
cin >> a[i] >> b[i];
s.push_back(a[i]);
s.push_back(b[i]+1);
}
//cout << s.size() << endl;
sort(s.begin(),s.end());
s.erase(unique(s.begin(),s.end()),s.end());
vector<long long> key;
for(int i = 0;i < s.size();i++){
key.push_back(s[i]);
}
//cout << k.size() << endl;
int m = key.size() - 1;
for(int i = 0;i < m;i++){
ranges[i] = (ii(key[i],key[i+1]-1));
}
//sort(ranges,ranges + m);
//cout << m << "\n";
for(int i = 0;i < m;i++){
diff[i] = ranges[i].second - ranges[i].first + 1;
diff[i] %= mod;
//cout << ranges[i].first << " " << ranges[i].second << "\n";
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(ranges[j].first == a[i]) posa[i] = j;
if(ranges[j].second == b[i]) posb[i] = j;
}
}
for(int j = posa[0];j <= posb[0];j++){
dp[0][j][1] = diff[j];
}
for(int i = 1;i < n;i++){
fill(acc,acc+m,0);
///take nothing for this school
int ii = i % 2;
for(int j = 0;j < m;j++){
for(int k = 1;k < 505;k++){
long long y = dp[ii^1][j][k];
if(y == 0) break;
dp[ii][j][k] = y;
//dp[ii][j][k] %= mod;
acc[j] += y;
acc[j] %= mod;
}
if(j!=0){
acc[j] += acc[j-1];
acc[j] %= mod;
}
}
///take same as previous school
for(int j = posa[i];j <= posb[i];j++){
if(a[i] <= ranges[j].first && b[i] >= ranges[j].second){
for(int k = 2;k < 505;k++){
long long x = dp[ii^1][j][k-1];
if(x == 0) break;
if(diff[j] - k + 1 <= 0) break;
x *= (diff[j] - k + 1);
x %= mod;
x *= inverse[k];
x %= mod;
dp[ii][j][k] += x;
//dp[ii][j][k] %= mod;
}
}
}
///take for the first time
//long long acc = 0ll;
for(int j = posa[i];j <= posb[i];j++){
if(a[i] <= ranges[j].first && b[i] >= ranges[j].second){
long long x;
if(j == 0) x = 0;
else x = acc[j-1];
x += 1;
x *= diff[j];
dp[ii][j][1] += x;
dp[ii][j][1] %= mod;
}
}
}
long long ans = 0ll;
for(int j = 0;j < m;j++){
for(int k = 1;k < 505;k++){
ans += dp[(n-1)%2][j][k];
ans %= mod;
}
}
cout << ans;
return 0;
}
Compilation message
boat.cpp: In function 'int main()':
boat.cpp:65:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i = 0;i < s.size();i++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4300 KB |
Output is correct |
2 |
Correct |
13 ms |
4312 KB |
Output is correct |
3 |
Correct |
12 ms |
4432 KB |
Output is correct |
4 |
Correct |
13 ms |
4300 KB |
Output is correct |
5 |
Correct |
14 ms |
4352 KB |
Output is correct |
6 |
Correct |
12 ms |
4428 KB |
Output is correct |
7 |
Correct |
12 ms |
4428 KB |
Output is correct |
8 |
Correct |
13 ms |
4428 KB |
Output is correct |
9 |
Correct |
12 ms |
4428 KB |
Output is correct |
10 |
Correct |
12 ms |
4428 KB |
Output is correct |
11 |
Correct |
13 ms |
4428 KB |
Output is correct |
12 |
Correct |
12 ms |
4428 KB |
Output is correct |
13 |
Correct |
12 ms |
4428 KB |
Output is correct |
14 |
Correct |
12 ms |
4428 KB |
Output is correct |
15 |
Correct |
13 ms |
4300 KB |
Output is correct |
16 |
Correct |
3 ms |
1100 KB |
Output is correct |
17 |
Correct |
4 ms |
1100 KB |
Output is correct |
18 |
Correct |
3 ms |
1100 KB |
Output is correct |
19 |
Correct |
3 ms |
1100 KB |
Output is correct |
20 |
Correct |
3 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4300 KB |
Output is correct |
2 |
Correct |
13 ms |
4312 KB |
Output is correct |
3 |
Correct |
12 ms |
4432 KB |
Output is correct |
4 |
Correct |
13 ms |
4300 KB |
Output is correct |
5 |
Correct |
14 ms |
4352 KB |
Output is correct |
6 |
Correct |
12 ms |
4428 KB |
Output is correct |
7 |
Correct |
12 ms |
4428 KB |
Output is correct |
8 |
Correct |
13 ms |
4428 KB |
Output is correct |
9 |
Correct |
12 ms |
4428 KB |
Output is correct |
10 |
Correct |
12 ms |
4428 KB |
Output is correct |
11 |
Correct |
13 ms |
4428 KB |
Output is correct |
12 |
Correct |
12 ms |
4428 KB |
Output is correct |
13 |
Correct |
12 ms |
4428 KB |
Output is correct |
14 |
Correct |
12 ms |
4428 KB |
Output is correct |
15 |
Correct |
13 ms |
4300 KB |
Output is correct |
16 |
Correct |
3 ms |
1100 KB |
Output is correct |
17 |
Correct |
4 ms |
1100 KB |
Output is correct |
18 |
Correct |
3 ms |
1100 KB |
Output is correct |
19 |
Correct |
3 ms |
1100 KB |
Output is correct |
20 |
Correct |
3 ms |
1100 KB |
Output is correct |
21 |
Correct |
43 ms |
7688 KB |
Output is correct |
22 |
Correct |
42 ms |
7680 KB |
Output is correct |
23 |
Correct |
43 ms |
7500 KB |
Output is correct |
24 |
Correct |
43 ms |
7472 KB |
Output is correct |
25 |
Correct |
44 ms |
7544 KB |
Output is correct |
26 |
Correct |
35 ms |
7244 KB |
Output is correct |
27 |
Correct |
36 ms |
7376 KB |
Output is correct |
28 |
Correct |
35 ms |
7260 KB |
Output is correct |
29 |
Correct |
37 ms |
7236 KB |
Output is correct |
30 |
Correct |
16 ms |
8172 KB |
Output is correct |
31 |
Correct |
17 ms |
8268 KB |
Output is correct |
32 |
Correct |
16 ms |
8244 KB |
Output is correct |
33 |
Correct |
17 ms |
8268 KB |
Output is correct |
34 |
Correct |
16 ms |
8128 KB |
Output is correct |
35 |
Correct |
18 ms |
8220 KB |
Output is correct |
36 |
Correct |
18 ms |
8268 KB |
Output is correct |
37 |
Correct |
17 ms |
8268 KB |
Output is correct |
38 |
Correct |
17 ms |
8168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1868 KB |
Output is correct |
2 |
Correct |
6 ms |
1868 KB |
Output is correct |
3 |
Correct |
7 ms |
1868 KB |
Output is correct |
4 |
Correct |
7 ms |
1868 KB |
Output is correct |
5 |
Correct |
8 ms |
1944 KB |
Output is correct |
6 |
Correct |
12 ms |
1860 KB |
Output is correct |
7 |
Correct |
12 ms |
1848 KB |
Output is correct |
8 |
Correct |
13 ms |
1868 KB |
Output is correct |
9 |
Correct |
12 ms |
1868 KB |
Output is correct |
10 |
Correct |
12 ms |
1868 KB |
Output is correct |
11 |
Correct |
8 ms |
1944 KB |
Output is correct |
12 |
Correct |
7 ms |
1868 KB |
Output is correct |
13 |
Correct |
8 ms |
1868 KB |
Output is correct |
14 |
Correct |
7 ms |
1868 KB |
Output is correct |
15 |
Correct |
8 ms |
1868 KB |
Output is correct |
16 |
Correct |
4 ms |
1228 KB |
Output is correct |
17 |
Correct |
4 ms |
1228 KB |
Output is correct |
18 |
Correct |
4 ms |
1228 KB |
Output is correct |
19 |
Correct |
3 ms |
1224 KB |
Output is correct |
20 |
Correct |
4 ms |
1224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4300 KB |
Output is correct |
2 |
Correct |
13 ms |
4312 KB |
Output is correct |
3 |
Correct |
12 ms |
4432 KB |
Output is correct |
4 |
Correct |
13 ms |
4300 KB |
Output is correct |
5 |
Correct |
14 ms |
4352 KB |
Output is correct |
6 |
Correct |
12 ms |
4428 KB |
Output is correct |
7 |
Correct |
12 ms |
4428 KB |
Output is correct |
8 |
Correct |
13 ms |
4428 KB |
Output is correct |
9 |
Correct |
12 ms |
4428 KB |
Output is correct |
10 |
Correct |
12 ms |
4428 KB |
Output is correct |
11 |
Correct |
13 ms |
4428 KB |
Output is correct |
12 |
Correct |
12 ms |
4428 KB |
Output is correct |
13 |
Correct |
12 ms |
4428 KB |
Output is correct |
14 |
Correct |
12 ms |
4428 KB |
Output is correct |
15 |
Correct |
13 ms |
4300 KB |
Output is correct |
16 |
Correct |
3 ms |
1100 KB |
Output is correct |
17 |
Correct |
4 ms |
1100 KB |
Output is correct |
18 |
Correct |
3 ms |
1100 KB |
Output is correct |
19 |
Correct |
3 ms |
1100 KB |
Output is correct |
20 |
Correct |
3 ms |
1100 KB |
Output is correct |
21 |
Correct |
43 ms |
7688 KB |
Output is correct |
22 |
Correct |
42 ms |
7680 KB |
Output is correct |
23 |
Correct |
43 ms |
7500 KB |
Output is correct |
24 |
Correct |
43 ms |
7472 KB |
Output is correct |
25 |
Correct |
44 ms |
7544 KB |
Output is correct |
26 |
Correct |
35 ms |
7244 KB |
Output is correct |
27 |
Correct |
36 ms |
7376 KB |
Output is correct |
28 |
Correct |
35 ms |
7260 KB |
Output is correct |
29 |
Correct |
37 ms |
7236 KB |
Output is correct |
30 |
Correct |
16 ms |
8172 KB |
Output is correct |
31 |
Correct |
17 ms |
8268 KB |
Output is correct |
32 |
Correct |
16 ms |
8244 KB |
Output is correct |
33 |
Correct |
17 ms |
8268 KB |
Output is correct |
34 |
Correct |
16 ms |
8128 KB |
Output is correct |
35 |
Correct |
18 ms |
8220 KB |
Output is correct |
36 |
Correct |
18 ms |
8268 KB |
Output is correct |
37 |
Correct |
17 ms |
8268 KB |
Output is correct |
38 |
Correct |
17 ms |
8168 KB |
Output is correct |
39 |
Correct |
7 ms |
1868 KB |
Output is correct |
40 |
Correct |
6 ms |
1868 KB |
Output is correct |
41 |
Correct |
7 ms |
1868 KB |
Output is correct |
42 |
Correct |
7 ms |
1868 KB |
Output is correct |
43 |
Correct |
8 ms |
1944 KB |
Output is correct |
44 |
Correct |
12 ms |
1860 KB |
Output is correct |
45 |
Correct |
12 ms |
1848 KB |
Output is correct |
46 |
Correct |
13 ms |
1868 KB |
Output is correct |
47 |
Correct |
12 ms |
1868 KB |
Output is correct |
48 |
Correct |
12 ms |
1868 KB |
Output is correct |
49 |
Correct |
8 ms |
1944 KB |
Output is correct |
50 |
Correct |
7 ms |
1868 KB |
Output is correct |
51 |
Correct |
8 ms |
1868 KB |
Output is correct |
52 |
Correct |
7 ms |
1868 KB |
Output is correct |
53 |
Correct |
8 ms |
1868 KB |
Output is correct |
54 |
Correct |
4 ms |
1228 KB |
Output is correct |
55 |
Correct |
4 ms |
1228 KB |
Output is correct |
56 |
Correct |
4 ms |
1228 KB |
Output is correct |
57 |
Correct |
3 ms |
1224 KB |
Output is correct |
58 |
Correct |
4 ms |
1224 KB |
Output is correct |
59 |
Correct |
701 ms |
8296 KB |
Output is correct |
60 |
Correct |
635 ms |
8388 KB |
Output is correct |
61 |
Correct |
626 ms |
8416 KB |
Output is correct |
62 |
Correct |
717 ms |
8292 KB |
Output is correct |
63 |
Correct |
667 ms |
8224 KB |
Output is correct |
64 |
Correct |
1347 ms |
8280 KB |
Output is correct |
65 |
Correct |
1351 ms |
8312 KB |
Output is correct |
66 |
Correct |
1343 ms |
8196 KB |
Output is correct |
67 |
Correct |
1347 ms |
8412 KB |
Output is correct |
68 |
Correct |
1337 ms |
8348 KB |
Output is correct |
69 |
Correct |
668 ms |
8268 KB |
Output is correct |
70 |
Correct |
699 ms |
8388 KB |
Output is correct |
71 |
Correct |
690 ms |
8280 KB |
Output is correct |
72 |
Correct |
713 ms |
8280 KB |
Output is correct |
73 |
Correct |
711 ms |
8268 KB |
Output is correct |
74 |
Correct |
72 ms |
1860 KB |
Output is correct |
75 |
Correct |
69 ms |
1832 KB |
Output is correct |
76 |
Correct |
71 ms |
1740 KB |
Output is correct |
77 |
Correct |
72 ms |
1752 KB |
Output is correct |
78 |
Correct |
72 ms |
1860 KB |
Output is correct |