답안 #540970

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
540970 2022-03-22T05:31:31 Z amunduzbaev Boat (APIO16_boat) C++17
100 / 100
1662 ms 4304 KB
#include "bits/stdc++.h"
using namespace std;

#define ar array
//~ #define int long long

const int mod = 1e9 + 7;

int bp(int a, int b){
	int r=1;
	while(b){
		if(b&1) r = r * 1ll * a % mod;
		a = a * 1ll * a % mod; b >>= 1;
	} return r;
}

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	//~ freopen("in.txt", "r", stdin);
	
	int n; cin>>n;
	vector<int> a(2 * n), p(2 * n), og;
	iota(p.begin(), p.end(), 0);
	for(int i=0;i<n;i++){
		cin>>a[i<<1]>>a[i<<1|1];
		a[i<<1|1]++;
	}
	
	sort(p.begin(), p.end(), [&](int i, int j){
		return (a[i] < a[j]);
	});
	int last=0;
	for(int i=0, j=0;i<2*n;last++){
		og.push_back(a[p[i]]);
		while(j<2*n && a[p[j]] == a[p[i]]) j++;
		while(i < j) a[p[i]] = last, i++;
	}
	
	vector<int> f(n + 1), inv(n + 1);
	f[0] = inv[0] = 1;
	for(int i=1;i<=n;i++){
		f[i] = f[i-1] * 1ll * i % mod;
		inv[i] = bp(f[i], mod - 2);
	}
	
	//~ for(int i=0;i<10;i++) cout<<a[i<<1]<<" "<<a[i<<1|1]<<"\n";
	
	auto C = [&](int n, int k){
		if(n < k) return 0ll;
		return f[n] * 1ll * inv[k] % mod * inv[n - k] % mod;
	};

	vector<vector<int>> cnt(n + 1, vector<int>(last));
	for(int c=1;c<=n;c++){
		for(int k=0;k+1<last;k++){
			int x = og[k+1] - og[k], f = x;
			for(int j=0;j<c && j<x;j++){
				cnt[c][k] = (cnt[c][k] + C(c - 1, j) * f % mod * inv[j + 1]) % mod;
				f = f * 1ll * (x - j - 1) % mod; 
			}
		}
	}
	
	int res = 0;
	vector<vector<int>> dp(n, vector<int>(last));
	for(int i=0;i<n;i++){
		int l = a[i<<1], r = a[i<<1|1];
		for(int k=l;k<r;k++){
			int c=1;
			for(int j=i-1;~j;j--){
				if(k){
					dp[i][k] = (dp[i][k] + dp[j][k-1] * 1ll * cnt[c][k]) % mod;
				}
				if(a[j<<1] <= k && k < a[j<<1|1]) c++;
			}
			
			dp[i][k] = (dp[i][k] + cnt[c][k]) % mod;
		}
		
		for(int k=1;k<last;k++) dp[i][k] = (dp[i][k] + dp[i][k-1]) % mod;
		res = (res + dp[i][last - 1]) % mod;
	}
	
	cout<<res<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 630 ms 4236 KB Output is correct
2 Correct 625 ms 4172 KB Output is correct
3 Correct 667 ms 4236 KB Output is correct
4 Correct 670 ms 4240 KB Output is correct
5 Correct 658 ms 4244 KB Output is correct
6 Correct 697 ms 4232 KB Output is correct
7 Correct 678 ms 4244 KB Output is correct
8 Correct 737 ms 4240 KB Output is correct
9 Correct 748 ms 4232 KB Output is correct
10 Correct 701 ms 4252 KB Output is correct
11 Correct 648 ms 4240 KB Output is correct
12 Correct 635 ms 4244 KB Output is correct
13 Correct 633 ms 4232 KB Output is correct
14 Correct 644 ms 4232 KB Output is correct
15 Correct 637 ms 4236 KB Output is correct
16 Correct 111 ms 972 KB Output is correct
17 Correct 117 ms 1116 KB Output is correct
18 Correct 120 ms 956 KB Output is correct
19 Correct 122 ms 988 KB Output is correct
20 Correct 117 ms 1088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 630 ms 4236 KB Output is correct
2 Correct 625 ms 4172 KB Output is correct
3 Correct 667 ms 4236 KB Output is correct
4 Correct 670 ms 4240 KB Output is correct
5 Correct 658 ms 4244 KB Output is correct
6 Correct 697 ms 4232 KB Output is correct
7 Correct 678 ms 4244 KB Output is correct
8 Correct 737 ms 4240 KB Output is correct
9 Correct 748 ms 4232 KB Output is correct
10 Correct 701 ms 4252 KB Output is correct
11 Correct 648 ms 4240 KB Output is correct
12 Correct 635 ms 4244 KB Output is correct
13 Correct 633 ms 4232 KB Output is correct
14 Correct 644 ms 4232 KB Output is correct
15 Correct 637 ms 4236 KB Output is correct
16 Correct 111 ms 972 KB Output is correct
17 Correct 117 ms 1116 KB Output is correct
18 Correct 120 ms 956 KB Output is correct
19 Correct 122 ms 988 KB Output is correct
20 Correct 117 ms 1088 KB Output is correct
21 Correct 311 ms 3944 KB Output is correct
22 Correct 314 ms 4044 KB Output is correct
23 Correct 277 ms 3916 KB Output is correct
24 Correct 305 ms 3924 KB Output is correct
25 Correct 311 ms 3912 KB Output is correct
26 Correct 393 ms 3752 KB Output is correct
27 Correct 411 ms 3864 KB Output is correct
28 Correct 410 ms 3796 KB Output is correct
29 Correct 404 ms 3800 KB Output is correct
30 Correct 629 ms 4240 KB Output is correct
31 Correct 631 ms 4232 KB Output is correct
32 Correct 669 ms 4228 KB Output is correct
33 Correct 731 ms 4236 KB Output is correct
34 Correct 677 ms 4224 KB Output is correct
35 Correct 631 ms 4224 KB Output is correct
36 Correct 635 ms 4240 KB Output is correct
37 Correct 630 ms 4224 KB Output is correct
38 Correct 599 ms 4228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 468 KB Output is correct
2 Correct 13 ms 408 KB Output is correct
3 Correct 14 ms 408 KB Output is correct
4 Correct 13 ms 456 KB Output is correct
5 Correct 14 ms 408 KB Output is correct
6 Correct 15 ms 468 KB Output is correct
7 Correct 15 ms 408 KB Output is correct
8 Correct 17 ms 468 KB Output is correct
9 Correct 14 ms 404 KB Output is correct
10 Correct 14 ms 468 KB Output is correct
11 Correct 14 ms 404 KB Output is correct
12 Correct 12 ms 400 KB Output is correct
13 Correct 12 ms 444 KB Output is correct
14 Correct 14 ms 400 KB Output is correct
15 Correct 16 ms 408 KB Output is correct
16 Correct 7 ms 340 KB Output is correct
17 Correct 6 ms 320 KB Output is correct
18 Correct 6 ms 340 KB Output is correct
19 Correct 7 ms 368 KB Output is correct
20 Correct 6 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 630 ms 4236 KB Output is correct
2 Correct 625 ms 4172 KB Output is correct
3 Correct 667 ms 4236 KB Output is correct
4 Correct 670 ms 4240 KB Output is correct
5 Correct 658 ms 4244 KB Output is correct
6 Correct 697 ms 4232 KB Output is correct
7 Correct 678 ms 4244 KB Output is correct
8 Correct 737 ms 4240 KB Output is correct
9 Correct 748 ms 4232 KB Output is correct
10 Correct 701 ms 4252 KB Output is correct
11 Correct 648 ms 4240 KB Output is correct
12 Correct 635 ms 4244 KB Output is correct
13 Correct 633 ms 4232 KB Output is correct
14 Correct 644 ms 4232 KB Output is correct
15 Correct 637 ms 4236 KB Output is correct
16 Correct 111 ms 972 KB Output is correct
17 Correct 117 ms 1116 KB Output is correct
18 Correct 120 ms 956 KB Output is correct
19 Correct 122 ms 988 KB Output is correct
20 Correct 117 ms 1088 KB Output is correct
21 Correct 311 ms 3944 KB Output is correct
22 Correct 314 ms 4044 KB Output is correct
23 Correct 277 ms 3916 KB Output is correct
24 Correct 305 ms 3924 KB Output is correct
25 Correct 311 ms 3912 KB Output is correct
26 Correct 393 ms 3752 KB Output is correct
27 Correct 411 ms 3864 KB Output is correct
28 Correct 410 ms 3796 KB Output is correct
29 Correct 404 ms 3800 KB Output is correct
30 Correct 629 ms 4240 KB Output is correct
31 Correct 631 ms 4232 KB Output is correct
32 Correct 669 ms 4228 KB Output is correct
33 Correct 731 ms 4236 KB Output is correct
34 Correct 677 ms 4224 KB Output is correct
35 Correct 631 ms 4224 KB Output is correct
36 Correct 635 ms 4240 KB Output is correct
37 Correct 630 ms 4224 KB Output is correct
38 Correct 599 ms 4228 KB Output is correct
39 Correct 14 ms 468 KB Output is correct
40 Correct 13 ms 408 KB Output is correct
41 Correct 14 ms 408 KB Output is correct
42 Correct 13 ms 456 KB Output is correct
43 Correct 14 ms 408 KB Output is correct
44 Correct 15 ms 468 KB Output is correct
45 Correct 15 ms 408 KB Output is correct
46 Correct 17 ms 468 KB Output is correct
47 Correct 14 ms 404 KB Output is correct
48 Correct 14 ms 468 KB Output is correct
49 Correct 14 ms 404 KB Output is correct
50 Correct 12 ms 400 KB Output is correct
51 Correct 12 ms 444 KB Output is correct
52 Correct 14 ms 400 KB Output is correct
53 Correct 16 ms 408 KB Output is correct
54 Correct 7 ms 340 KB Output is correct
55 Correct 6 ms 320 KB Output is correct
56 Correct 6 ms 340 KB Output is correct
57 Correct 7 ms 368 KB Output is correct
58 Correct 6 ms 320 KB Output is correct
59 Correct 1571 ms 4292 KB Output is correct
60 Correct 1544 ms 4304 KB Output is correct
61 Correct 1502 ms 4300 KB Output is correct
62 Correct 1512 ms 4232 KB Output is correct
63 Correct 1501 ms 4240 KB Output is correct
64 Correct 1658 ms 4248 KB Output is correct
65 Correct 1649 ms 4292 KB Output is correct
66 Correct 1636 ms 4244 KB Output is correct
67 Correct 1662 ms 4292 KB Output is correct
68 Correct 1650 ms 4236 KB Output is correct
69 Correct 1467 ms 4236 KB Output is correct
70 Correct 1495 ms 4236 KB Output is correct
71 Correct 1504 ms 4236 KB Output is correct
72 Correct 1473 ms 4236 KB Output is correct
73 Correct 1516 ms 4236 KB Output is correct
74 Correct 182 ms 956 KB Output is correct
75 Correct 184 ms 1088 KB Output is correct
76 Correct 183 ms 980 KB Output is correct
77 Correct 179 ms 964 KB Output is correct
78 Correct 182 ms 972 KB Output is correct