답안 #47635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
47635 2018-05-06T06:45:46 Z E869120 Boat (APIO16_boat) C++14
58 / 100
2000 ms 23256 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

long long N, A[509], B[509], nr1[1009][1009], nr2[1009][1009], dp[1009][1009], inv[1009], mod = 1000000007; vector<long long>V;
long long rets[1009][1009];

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 60; i++) {
		if ((b / (1LL << i)) % 2 == 1) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}
long long Div(long long a, long long b, long long m) {
	return (a*modpow(b, m - 2, m)) % m;
}

void init() {
	for (int i = 0; i <= 500; i++) {
		for (int j = 0; j <= 500; j++) {
			if (i == 0 || j == 0) nr1[i][j] = 1;
			else nr1[i][j] = (nr1[i - 1][j] + nr1[i][j - 1]) % mod;
		}
	}
	for (int i = 1; i <= 1000; i++) inv[i] = Div(1, i, mod);
	for (int i = 0; i < V.size() - 1; i++) {
		long long L = V[i + 1] - V[i], S = 1;
		for (int j = 0; j <= 500; j++) {
			if (L < j) break;
			nr2[i][j] = S;
			S *= (L - j); S %= mod;
			S *= inv[j + 1]; S %= mod;
		}
	}
}
long long ncr(long long n, long long r) {
	if (r < 0 || n < r) return 0;
	return nr1[n - r][r];
}
long long solve(long long v, long long pos) {
	if (rets[v][pos] >= 1) return rets[v][pos] - 1;
	long long res = 0;
	for (int i = 1; i <= v; i++) {
		res += ncr(v - 1, i - 1)*nr2[pos][i];
		res %= mod;
	}
	rets[v][pos] = res + 1;
	return res;
}

int main() {
	cin >> N;
	for (int i = 1; i <= N; i++) {
		cin >> A[i] >> B[i]; B[i]++;
		V.push_back(A[i]);
		V.push_back(B[i]);
	}
	sort(V.begin(), V.end());
	V.erase(unique(V.begin(), V.end()), V.end());
	for (int i = 1; i <= N; i++) {
		A[i] = lower_bound(V.begin(), V.end(), A[i]) - V.begin();
		B[i] = lower_bound(V.begin(), V.end(), B[i]) - V.begin();
	}
	init();
	dp[0][1] = 1;
	for (int i = 0; i < V.size() - 1; i++) {
		for (int j = 1; j <= N + 1; j++) {
			if (dp[i][j] == 0) continue;
			dp[i + 1][j] += dp[i][j]; dp[i + 1][j] %= mod;
			int G = 0;
			for (int k = j + 1; k <= N + 1; k++) {
				if (A[k - 1] <= i && i < B[k - 1]) {
					G++;
					dp[i + 1][k] += dp[i][j] * solve(G, i);
					dp[i + 1][k] %= mod;
				}
			}
		}
	}
	long long sum = 0;
	for (int i = 2; i <= N + 1; i++) sum += dp[V.size() - 1][i];
	cout << sum%mod << endl;
	return 0;
}

Compilation message

boat.cpp: In function 'void init()':
boat.cpp:29:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < V.size() - 1; i++) {
                  ~~^~~~~~~~~~~~~~
boat.cpp: In function 'int main()':
boat.cpp:69:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < V.size() - 1; i++) {
                  ~~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 273 ms 18240 KB Output is correct
2 Correct 267 ms 18240 KB Output is correct
3 Correct 261 ms 18240 KB Output is correct
4 Correct 255 ms 18240 KB Output is correct
5 Correct 263 ms 18240 KB Output is correct
6 Correct 162 ms 18240 KB Output is correct
7 Correct 165 ms 18240 KB Output is correct
8 Correct 178 ms 18240 KB Output is correct
9 Correct 169 ms 18240 KB Output is correct
10 Correct 169 ms 18240 KB Output is correct
11 Correct 165 ms 18240 KB Output is correct
12 Correct 167 ms 18244 KB Output is correct
13 Correct 172 ms 18244 KB Output is correct
14 Correct 157 ms 18244 KB Output is correct
15 Correct 166 ms 18244 KB Output is correct
16 Correct 56 ms 18244 KB Output is correct
17 Correct 65 ms 18244 KB Output is correct
18 Correct 57 ms 18244 KB Output is correct
19 Correct 58 ms 18244 KB Output is correct
20 Correct 58 ms 18244 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 273 ms 18240 KB Output is correct
2 Correct 267 ms 18240 KB Output is correct
3 Correct 261 ms 18240 KB Output is correct
4 Correct 255 ms 18240 KB Output is correct
5 Correct 263 ms 18240 KB Output is correct
6 Correct 162 ms 18240 KB Output is correct
7 Correct 165 ms 18240 KB Output is correct
8 Correct 178 ms 18240 KB Output is correct
9 Correct 169 ms 18240 KB Output is correct
10 Correct 169 ms 18240 KB Output is correct
11 Correct 165 ms 18240 KB Output is correct
12 Correct 167 ms 18244 KB Output is correct
13 Correct 172 ms 18244 KB Output is correct
14 Correct 157 ms 18244 KB Output is correct
15 Correct 166 ms 18244 KB Output is correct
16 Correct 56 ms 18244 KB Output is correct
17 Correct 65 ms 18244 KB Output is correct
18 Correct 57 ms 18244 KB Output is correct
19 Correct 58 ms 18244 KB Output is correct
20 Correct 58 ms 18244 KB Output is correct
21 Correct 915 ms 18244 KB Output is correct
22 Correct 884 ms 18244 KB Output is correct
23 Correct 791 ms 18244 KB Output is correct
24 Correct 828 ms 18244 KB Output is correct
25 Correct 899 ms 18244 KB Output is correct
26 Correct 1792 ms 18244 KB Output is correct
27 Correct 1787 ms 18524 KB Output is correct
28 Correct 1767 ms 18524 KB Output is correct
29 Correct 1752 ms 18524 KB Output is correct
30 Correct 164 ms 18524 KB Output is correct
31 Correct 164 ms 18524 KB Output is correct
32 Correct 173 ms 18524 KB Output is correct
33 Correct 167 ms 18524 KB Output is correct
34 Correct 165 ms 18524 KB Output is correct
35 Correct 241 ms 18524 KB Output is correct
36 Correct 242 ms 18524 KB Output is correct
37 Correct 253 ms 18524 KB Output is correct
38 Correct 251 ms 18524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 18524 KB Output is correct
2 Correct 24 ms 18524 KB Output is correct
3 Correct 25 ms 18524 KB Output is correct
4 Correct 25 ms 18524 KB Output is correct
5 Correct 26 ms 18524 KB Output is correct
6 Correct 33 ms 18524 KB Output is correct
7 Correct 33 ms 18524 KB Output is correct
8 Correct 33 ms 18524 KB Output is correct
9 Correct 33 ms 18524 KB Output is correct
10 Correct 33 ms 18524 KB Output is correct
11 Correct 26 ms 18524 KB Output is correct
12 Correct 25 ms 18524 KB Output is correct
13 Correct 25 ms 18524 KB Output is correct
14 Correct 25 ms 18524 KB Output is correct
15 Correct 26 ms 18524 KB Output is correct
16 Correct 19 ms 18524 KB Output is correct
17 Correct 18 ms 18524 KB Output is correct
18 Correct 19 ms 18524 KB Output is correct
19 Correct 18 ms 18524 KB Output is correct
20 Correct 19 ms 18524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 273 ms 18240 KB Output is correct
2 Correct 267 ms 18240 KB Output is correct
3 Correct 261 ms 18240 KB Output is correct
4 Correct 255 ms 18240 KB Output is correct
5 Correct 263 ms 18240 KB Output is correct
6 Correct 162 ms 18240 KB Output is correct
7 Correct 165 ms 18240 KB Output is correct
8 Correct 178 ms 18240 KB Output is correct
9 Correct 169 ms 18240 KB Output is correct
10 Correct 169 ms 18240 KB Output is correct
11 Correct 165 ms 18240 KB Output is correct
12 Correct 167 ms 18244 KB Output is correct
13 Correct 172 ms 18244 KB Output is correct
14 Correct 157 ms 18244 KB Output is correct
15 Correct 166 ms 18244 KB Output is correct
16 Correct 56 ms 18244 KB Output is correct
17 Correct 65 ms 18244 KB Output is correct
18 Correct 57 ms 18244 KB Output is correct
19 Correct 58 ms 18244 KB Output is correct
20 Correct 58 ms 18244 KB Output is correct
21 Correct 915 ms 18244 KB Output is correct
22 Correct 884 ms 18244 KB Output is correct
23 Correct 791 ms 18244 KB Output is correct
24 Correct 828 ms 18244 KB Output is correct
25 Correct 899 ms 18244 KB Output is correct
26 Correct 1792 ms 18244 KB Output is correct
27 Correct 1787 ms 18524 KB Output is correct
28 Correct 1767 ms 18524 KB Output is correct
29 Correct 1752 ms 18524 KB Output is correct
30 Correct 164 ms 18524 KB Output is correct
31 Correct 164 ms 18524 KB Output is correct
32 Correct 173 ms 18524 KB Output is correct
33 Correct 167 ms 18524 KB Output is correct
34 Correct 165 ms 18524 KB Output is correct
35 Correct 241 ms 18524 KB Output is correct
36 Correct 242 ms 18524 KB Output is correct
37 Correct 253 ms 18524 KB Output is correct
38 Correct 251 ms 18524 KB Output is correct
39 Correct 25 ms 18524 KB Output is correct
40 Correct 24 ms 18524 KB Output is correct
41 Correct 25 ms 18524 KB Output is correct
42 Correct 25 ms 18524 KB Output is correct
43 Correct 26 ms 18524 KB Output is correct
44 Correct 33 ms 18524 KB Output is correct
45 Correct 33 ms 18524 KB Output is correct
46 Correct 33 ms 18524 KB Output is correct
47 Correct 33 ms 18524 KB Output is correct
48 Correct 33 ms 18524 KB Output is correct
49 Correct 26 ms 18524 KB Output is correct
50 Correct 25 ms 18524 KB Output is correct
51 Correct 25 ms 18524 KB Output is correct
52 Correct 25 ms 18524 KB Output is correct
53 Correct 26 ms 18524 KB Output is correct
54 Correct 19 ms 18524 KB Output is correct
55 Correct 18 ms 18524 KB Output is correct
56 Correct 19 ms 18524 KB Output is correct
57 Correct 18 ms 18524 KB Output is correct
58 Correct 19 ms 18524 KB Output is correct
59 Correct 1008 ms 22556 KB Output is correct
60 Correct 943 ms 22556 KB Output is correct
61 Correct 895 ms 22556 KB Output is correct
62 Correct 1033 ms 22604 KB Output is correct
63 Correct 1012 ms 22656 KB Output is correct
64 Execution timed out 2045 ms 23256 KB Time limit exceeded
65 Halted 0 ms 0 KB -