답안 #20868

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
20868 2017-02-27T04:48:30 Z kdh9949 Boat (APIO16_boat) C++
100 / 100
959 ms 7136 KB
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;

int n;
set<int> ss;
int a[505], b[505], seg[1005], c, q, t;
int dp[505][1005], dps[505][1005];
int ufac[505], dfacin[505], newcomb[1005][505];
int mod = 1000000007;

long long pow(long long n, int k){
	if(k == 1) return n;
	long long t = pow(n, k / 2);
	t *= t; t %= mod;
	if(k % 2) return t * n % mod;
	return t;
}

int modinv(int x){
	return (int)pow(x, mod - 2);
}

int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%d%d", a + i, b + i);
		ss.insert(a[i]); ss.insert(b[i] + 1);
	}
	ss.insert(0); ss.insert(1);
	for(set<int>::iterator it = ss.begin(); it != ss.end(); it++) seg[++c] = *it;
	for(int i = 1; i <= n; i++){
		a[i] = (int)(lower_bound(seg + 1, seg + c + 1, a[i]) - seg);
		b[i] = (int)(lower_bound(seg + 1, seg + c + 1, b[i] + 1) - seg);
	}
	dfacin[0] = modinv(2); q = 2;
	for(int i = 1; i < n; i++){
		q = (int)((long long)q * (i + 2) % mod);
		dfacin[i] = modinv(q);
	}
	for(int i = 1; i < c; i++){
		ufac[0] = (int)((long long)(seg[i + 1] - seg[i]) * (seg[i + 1] - seg[i] - 1) % mod);
		newcomb[i][0] = (int)((long long)ufac[0] * dfacin[0] % mod);
		for(int j = 1; j < n; j++){
			ufac[j] = (int)((long long)ufac[j - 1] * (seg[i + 1] - seg[i] + j) % mod);
			newcomb[i][j] = (int)((long long)ufac[j] * dfacin[j] % mod);
		}
	}
	dp[0][0] = 1;
	for(int i = 0; i < c; i++) dps[0][i] = 1;
	for(int i = 1; i <= n; i++){
		dps[i][0] = dps[i - 1][0];
		for(int j = 1; j < a[i]; j++) dps[i][j] = ((dps[i - 1][j] + dps[i][j - 1]) % mod - dps[i - 1][j - 1] + mod) % mod;
		for(int j = a[i]; j < b[i]; j++){
			dp[i][j] = dps[i - 1][j - 1];
			dps[i][j] = ((dps[i - 1][j] + dps[i][j - 1]) % mod - dps[i - 1][j - 1] + mod) % mod;
			dps[i][j] += (int)((long long)(seg[j + 1] - seg[j]) * dp[i][j] % mod);
			dps[i][j] %= mod;
			q = 0;
			for(int k = i - 1; k > 0; k--){
				if(a[k] <= j && j < b[k]){
					t = (int)((long long)newcomb[j][q] * dp[k][j] % mod);
					dps[i][j] += t;
					dps[i][j] %= mod;
					q++;
				}
			}
		}
		for(int j = b[i]; j < c; j++) dps[i][j] = ((dps[i - 1][j] + dps[i][j - 1]) % mod - dps[i - 1][j - 1] + mod) % mod;
	}
	printf("%d", (dps[n][c - 1] - 1 + mod) % mod);
	return 0;
}

Compilation message

boat.cpp: In function 'int main()':
boat.cpp:26:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
                 ^
boat.cpp:28:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", a + i, b + i);
                              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 7136 KB Output is correct
2 Correct 29 ms 7136 KB Output is correct
3 Correct 33 ms 7136 KB Output is correct
4 Correct 29 ms 7136 KB Output is correct
5 Correct 26 ms 7136 KB Output is correct
6 Correct 29 ms 7136 KB Output is correct
7 Correct 26 ms 7136 KB Output is correct
8 Correct 26 ms 7136 KB Output is correct
9 Correct 26 ms 7136 KB Output is correct
10 Correct 29 ms 7136 KB Output is correct
11 Correct 26 ms 7136 KB Output is correct
12 Correct 26 ms 7136 KB Output is correct
13 Correct 29 ms 7136 KB Output is correct
14 Correct 23 ms 7136 KB Output is correct
15 Correct 29 ms 7136 KB Output is correct
16 Correct 6 ms 7136 KB Output is correct
17 Correct 6 ms 7136 KB Output is correct
18 Correct 6 ms 7136 KB Output is correct
19 Correct 3 ms 7136 KB Output is correct
20 Correct 6 ms 7136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 7136 KB Output is correct
2 Correct 29 ms 7136 KB Output is correct
3 Correct 33 ms 7136 KB Output is correct
4 Correct 29 ms 7136 KB Output is correct
5 Correct 26 ms 7136 KB Output is correct
6 Correct 29 ms 7136 KB Output is correct
7 Correct 26 ms 7136 KB Output is correct
8 Correct 26 ms 7136 KB Output is correct
9 Correct 26 ms 7136 KB Output is correct
10 Correct 29 ms 7136 KB Output is correct
11 Correct 26 ms 7136 KB Output is correct
12 Correct 26 ms 7136 KB Output is correct
13 Correct 29 ms 7136 KB Output is correct
14 Correct 23 ms 7136 KB Output is correct
15 Correct 29 ms 7136 KB Output is correct
16 Correct 6 ms 7136 KB Output is correct
17 Correct 6 ms 7136 KB Output is correct
18 Correct 6 ms 7136 KB Output is correct
19 Correct 3 ms 7136 KB Output is correct
20 Correct 6 ms 7136 KB Output is correct
21 Correct 456 ms 7136 KB Output is correct
22 Correct 409 ms 7136 KB Output is correct
23 Correct 346 ms 7136 KB Output is correct
24 Correct 393 ms 7136 KB Output is correct
25 Correct 386 ms 7136 KB Output is correct
26 Correct 766 ms 7136 KB Output is correct
27 Correct 786 ms 7136 KB Output is correct
28 Correct 763 ms 7136 KB Output is correct
29 Correct 763 ms 7136 KB Output is correct
30 Correct 26 ms 7136 KB Output is correct
31 Correct 23 ms 7136 KB Output is correct
32 Correct 29 ms 7136 KB Output is correct
33 Correct 26 ms 7136 KB Output is correct
34 Correct 19 ms 7136 KB Output is correct
35 Correct 29 ms 7136 KB Output is correct
36 Correct 29 ms 7136 KB Output is correct
37 Correct 26 ms 7136 KB Output is correct
38 Correct 26 ms 7136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 7136 KB Output is correct
2 Correct 3 ms 7136 KB Output is correct
3 Correct 0 ms 7136 KB Output is correct
4 Correct 3 ms 7136 KB Output is correct
5 Correct 3 ms 7136 KB Output is correct
6 Correct 6 ms 7136 KB Output is correct
7 Correct 6 ms 7136 KB Output is correct
8 Correct 6 ms 7136 KB Output is correct
9 Correct 6 ms 7136 KB Output is correct
10 Correct 6 ms 7136 KB Output is correct
11 Correct 3 ms 7136 KB Output is correct
12 Correct 3 ms 7136 KB Output is correct
13 Correct 3 ms 7136 KB Output is correct
14 Correct 0 ms 7136 KB Output is correct
15 Correct 3 ms 7136 KB Output is correct
16 Correct 3 ms 7136 KB Output is correct
17 Correct 0 ms 7136 KB Output is correct
18 Correct 3 ms 7136 KB Output is correct
19 Correct 3 ms 7136 KB Output is correct
20 Correct 3 ms 7136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 7136 KB Output is correct
2 Correct 29 ms 7136 KB Output is correct
3 Correct 33 ms 7136 KB Output is correct
4 Correct 29 ms 7136 KB Output is correct
5 Correct 26 ms 7136 KB Output is correct
6 Correct 29 ms 7136 KB Output is correct
7 Correct 26 ms 7136 KB Output is correct
8 Correct 26 ms 7136 KB Output is correct
9 Correct 26 ms 7136 KB Output is correct
10 Correct 29 ms 7136 KB Output is correct
11 Correct 26 ms 7136 KB Output is correct
12 Correct 26 ms 7136 KB Output is correct
13 Correct 29 ms 7136 KB Output is correct
14 Correct 23 ms 7136 KB Output is correct
15 Correct 29 ms 7136 KB Output is correct
16 Correct 6 ms 7136 KB Output is correct
17 Correct 6 ms 7136 KB Output is correct
18 Correct 6 ms 7136 KB Output is correct
19 Correct 3 ms 7136 KB Output is correct
20 Correct 6 ms 7136 KB Output is correct
21 Correct 456 ms 7136 KB Output is correct
22 Correct 409 ms 7136 KB Output is correct
23 Correct 346 ms 7136 KB Output is correct
24 Correct 393 ms 7136 KB Output is correct
25 Correct 386 ms 7136 KB Output is correct
26 Correct 766 ms 7136 KB Output is correct
27 Correct 786 ms 7136 KB Output is correct
28 Correct 763 ms 7136 KB Output is correct
29 Correct 763 ms 7136 KB Output is correct
30 Correct 26 ms 7136 KB Output is correct
31 Correct 23 ms 7136 KB Output is correct
32 Correct 29 ms 7136 KB Output is correct
33 Correct 26 ms 7136 KB Output is correct
34 Correct 19 ms 7136 KB Output is correct
35 Correct 29 ms 7136 KB Output is correct
36 Correct 29 ms 7136 KB Output is correct
37 Correct 26 ms 7136 KB Output is correct
38 Correct 26 ms 7136 KB Output is correct
39 Correct 3 ms 7136 KB Output is correct
40 Correct 3 ms 7136 KB Output is correct
41 Correct 0 ms 7136 KB Output is correct
42 Correct 3 ms 7136 KB Output is correct
43 Correct 3 ms 7136 KB Output is correct
44 Correct 6 ms 7136 KB Output is correct
45 Correct 6 ms 7136 KB Output is correct
46 Correct 6 ms 7136 KB Output is correct
47 Correct 6 ms 7136 KB Output is correct
48 Correct 6 ms 7136 KB Output is correct
49 Correct 3 ms 7136 KB Output is correct
50 Correct 3 ms 7136 KB Output is correct
51 Correct 3 ms 7136 KB Output is correct
52 Correct 0 ms 7136 KB Output is correct
53 Correct 3 ms 7136 KB Output is correct
54 Correct 3 ms 7136 KB Output is correct
55 Correct 0 ms 7136 KB Output is correct
56 Correct 3 ms 7136 KB Output is correct
57 Correct 3 ms 7136 KB Output is correct
58 Correct 3 ms 7136 KB Output is correct
59 Correct 443 ms 7136 KB Output is correct
60 Correct 416 ms 7136 KB Output is correct
61 Correct 389 ms 7136 KB Output is correct
62 Correct 449 ms 7136 KB Output is correct
63 Correct 429 ms 7136 KB Output is correct
64 Correct 959 ms 7136 KB Output is correct
65 Correct 893 ms 7136 KB Output is correct
66 Correct 879 ms 7136 KB Output is correct
67 Correct 889 ms 7136 KB Output is correct
68 Correct 909 ms 7136 KB Output is correct
69 Correct 366 ms 7136 KB Output is correct
70 Correct 373 ms 7136 KB Output is correct
71 Correct 349 ms 7136 KB Output is correct
72 Correct 366 ms 7136 KB Output is correct
73 Correct 373 ms 7136 KB Output is correct
74 Correct 89 ms 7136 KB Output is correct
75 Correct 83 ms 7136 KB Output is correct
76 Correct 89 ms 7136 KB Output is correct
77 Correct 89 ms 7136 KB Output is correct
78 Correct 89 ms 7136 KB Output is correct