답안 #45550

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
45550 2018-04-15T10:24:18 Z fallingstar Boat (APIO16_boat) C++17
100 / 100
1148 ms 6464 KB
/**
	Problem: Boat
	Source: APIO 2016 problem 1
*/

#include <iostream>
#include <algorithm>

#define long long long

using namespace std;

const int N = 500 + 2;
const int MOD = 1e9 + 7;

int Mul(int a, int b) { return (long) a * b % MOD; }
void Add(int &a, int b) { a += b; if (a >= MOD) a -= MOD; }
int Sum(int a, int b) { Add(a, b); return a; }
void Sub(int &a, int b) { a -= b; if (a < 0) a += MOD; }
int Pow(int x, int n)
{
	if (n == 1) return x;
	int t = Pow(x, n / 2); t = Mul(t, t);
	return n & 1 ? Mul(t, x) : t;
}

int n, a[N], b[N];
int m, p[N * 2];										// left and right ends of ranges
int c[N][N];											// c[i][j] = choose j from i
int g[N * 2][N];
int inv[N];												// inv[i] = i^-1 
int f[N][N * 2];

void Prep()												// precalculate all g(i, j)
{
	for (int i = 1; i <= n; ++i)
		inv[i] = Pow(i, MOD - 2);

	c[0][0] = 1;
	for (int i = 1; i <= n; ++i)						// calculate all c(i, j) with i, j <= n					
	{
		c[i][0] = 1;
		for (int j = 1; j <= i; ++j)
			c[i][j] = Sum(c[i - 1][j], c[i - 1][j - 1]);
	}
	
	for (int i = 1; i < m; ++i)
	{
		int tmp[N] = {}, len = p[i] - p[i - 1];			// tmp[i] = c(len, i)
		tmp[0] = 1;	
		for (int j = 1; j <= min(n, len); ++j)
			tmp[j] = Mul(Mul(tmp[j - 1], len - j + 1), inv[j]);
		
		for (int j = 1; j <= n; ++j)					// j schools can send, including the current one
			for (int k = 1; k <= min(j, len); ++k)		// k schools actually sent, including the current one
				Add(g[i][j], Mul(c[j - 1][k - 1], tmp[k]));
	}
}

void Solve()
{
	int res = 0;
	fill(f[0], f[0] + m, 1);
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j < m; ++j)
		{
			if (a[i] <= p[j - 1] + 1 && p[j] <= b[i])
			{
				int cnt = 0;							// number of schools that contain range j
				for (int k = i; k >= 1; --k)
				{
					cnt += (a[k] <= p[j - 1] + 1 && p[j] <= b[k]);
					Add(f[i][j], Mul(f[k - 1][j - 1], g[j][cnt]));
				}
				// cerr << "f[" << i << "][" << j << "] = " << f[i][j] << '\n';
				Add(res, f[i][j]);
			}
			Add(f[i][j], f[i][j - 1]);
		}
	cout << res;
}

int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i) 
	{
		cin >> a[i] >> b[i];
		p[m++] = a[i] - 1;
		p[m++] = b[i];
	}
	sort(p, p + m);
	m = unique(p, p + m) - p;
	Prep();
	Solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 202 ms 5368 KB Output is correct
2 Correct 209 ms 5368 KB Output is correct
3 Correct 222 ms 5512 KB Output is correct
4 Correct 202 ms 5512 KB Output is correct
5 Correct 202 ms 5512 KB Output is correct
6 Correct 209 ms 5512 KB Output is correct
7 Correct 235 ms 5512 KB Output is correct
8 Correct 207 ms 5512 KB Output is correct
9 Correct 203 ms 5512 KB Output is correct
10 Correct 288 ms 5588 KB Output is correct
11 Correct 244 ms 5588 KB Output is correct
12 Correct 231 ms 5612 KB Output is correct
13 Correct 216 ms 5612 KB Output is correct
14 Correct 203 ms 5628 KB Output is correct
15 Correct 204 ms 5628 KB Output is correct
16 Correct 42 ms 5628 KB Output is correct
17 Correct 43 ms 5628 KB Output is correct
18 Correct 43 ms 5628 KB Output is correct
19 Correct 43 ms 5628 KB Output is correct
20 Correct 42 ms 5628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 202 ms 5368 KB Output is correct
2 Correct 209 ms 5368 KB Output is correct
3 Correct 222 ms 5512 KB Output is correct
4 Correct 202 ms 5512 KB Output is correct
5 Correct 202 ms 5512 KB Output is correct
6 Correct 209 ms 5512 KB Output is correct
7 Correct 235 ms 5512 KB Output is correct
8 Correct 207 ms 5512 KB Output is correct
9 Correct 203 ms 5512 KB Output is correct
10 Correct 288 ms 5588 KB Output is correct
11 Correct 244 ms 5588 KB Output is correct
12 Correct 231 ms 5612 KB Output is correct
13 Correct 216 ms 5612 KB Output is correct
14 Correct 203 ms 5628 KB Output is correct
15 Correct 204 ms 5628 KB Output is correct
16 Correct 42 ms 5628 KB Output is correct
17 Correct 43 ms 5628 KB Output is correct
18 Correct 43 ms 5628 KB Output is correct
19 Correct 43 ms 5628 KB Output is correct
20 Correct 42 ms 5628 KB Output is correct
21 Correct 594 ms 5628 KB Output is correct
22 Correct 580 ms 5628 KB Output is correct
23 Correct 596 ms 5628 KB Output is correct
24 Correct 568 ms 5628 KB Output is correct
25 Correct 576 ms 5628 KB Output is correct
26 Correct 620 ms 5628 KB Output is correct
27 Correct 641 ms 5628 KB Output is correct
28 Correct 650 ms 5628 KB Output is correct
29 Correct 648 ms 5716 KB Output is correct
30 Correct 249 ms 5724 KB Output is correct
31 Correct 212 ms 5864 KB Output is correct
32 Correct 207 ms 5864 KB Output is correct
33 Correct 208 ms 5864 KB Output is correct
34 Correct 211 ms 5864 KB Output is correct
35 Correct 201 ms 5864 KB Output is correct
36 Correct 207 ms 5928 KB Output is correct
37 Correct 210 ms 5928 KB Output is correct
38 Correct 263 ms 5928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 5928 KB Output is correct
2 Correct 10 ms 5928 KB Output is correct
3 Correct 11 ms 5928 KB Output is correct
4 Correct 11 ms 5928 KB Output is correct
5 Correct 12 ms 5928 KB Output is correct
6 Correct 13 ms 5928 KB Output is correct
7 Correct 13 ms 5928 KB Output is correct
8 Correct 13 ms 5928 KB Output is correct
9 Correct 12 ms 5928 KB Output is correct
10 Correct 12 ms 5928 KB Output is correct
11 Correct 11 ms 5928 KB Output is correct
12 Correct 10 ms 5928 KB Output is correct
13 Correct 10 ms 5928 KB Output is correct
14 Correct 10 ms 5928 KB Output is correct
15 Correct 10 ms 5928 KB Output is correct
16 Correct 7 ms 5928 KB Output is correct
17 Correct 7 ms 5928 KB Output is correct
18 Correct 7 ms 5928 KB Output is correct
19 Correct 7 ms 5928 KB Output is correct
20 Correct 8 ms 5928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 202 ms 5368 KB Output is correct
2 Correct 209 ms 5368 KB Output is correct
3 Correct 222 ms 5512 KB Output is correct
4 Correct 202 ms 5512 KB Output is correct
5 Correct 202 ms 5512 KB Output is correct
6 Correct 209 ms 5512 KB Output is correct
7 Correct 235 ms 5512 KB Output is correct
8 Correct 207 ms 5512 KB Output is correct
9 Correct 203 ms 5512 KB Output is correct
10 Correct 288 ms 5588 KB Output is correct
11 Correct 244 ms 5588 KB Output is correct
12 Correct 231 ms 5612 KB Output is correct
13 Correct 216 ms 5612 KB Output is correct
14 Correct 203 ms 5628 KB Output is correct
15 Correct 204 ms 5628 KB Output is correct
16 Correct 42 ms 5628 KB Output is correct
17 Correct 43 ms 5628 KB Output is correct
18 Correct 43 ms 5628 KB Output is correct
19 Correct 43 ms 5628 KB Output is correct
20 Correct 42 ms 5628 KB Output is correct
21 Correct 594 ms 5628 KB Output is correct
22 Correct 580 ms 5628 KB Output is correct
23 Correct 596 ms 5628 KB Output is correct
24 Correct 568 ms 5628 KB Output is correct
25 Correct 576 ms 5628 KB Output is correct
26 Correct 620 ms 5628 KB Output is correct
27 Correct 641 ms 5628 KB Output is correct
28 Correct 650 ms 5628 KB Output is correct
29 Correct 648 ms 5716 KB Output is correct
30 Correct 249 ms 5724 KB Output is correct
31 Correct 212 ms 5864 KB Output is correct
32 Correct 207 ms 5864 KB Output is correct
33 Correct 208 ms 5864 KB Output is correct
34 Correct 211 ms 5864 KB Output is correct
35 Correct 201 ms 5864 KB Output is correct
36 Correct 207 ms 5928 KB Output is correct
37 Correct 210 ms 5928 KB Output is correct
38 Correct 263 ms 5928 KB Output is correct
39 Correct 14 ms 5928 KB Output is correct
40 Correct 10 ms 5928 KB Output is correct
41 Correct 11 ms 5928 KB Output is correct
42 Correct 11 ms 5928 KB Output is correct
43 Correct 12 ms 5928 KB Output is correct
44 Correct 13 ms 5928 KB Output is correct
45 Correct 13 ms 5928 KB Output is correct
46 Correct 13 ms 5928 KB Output is correct
47 Correct 12 ms 5928 KB Output is correct
48 Correct 12 ms 5928 KB Output is correct
49 Correct 11 ms 5928 KB Output is correct
50 Correct 10 ms 5928 KB Output is correct
51 Correct 10 ms 5928 KB Output is correct
52 Correct 10 ms 5928 KB Output is correct
53 Correct 10 ms 5928 KB Output is correct
54 Correct 7 ms 5928 KB Output is correct
55 Correct 7 ms 5928 KB Output is correct
56 Correct 7 ms 5928 KB Output is correct
57 Correct 7 ms 5928 KB Output is correct
58 Correct 8 ms 5928 KB Output is correct
59 Correct 1016 ms 6136 KB Output is correct
60 Correct 979 ms 6136 KB Output is correct
61 Correct 947 ms 6136 KB Output is correct
62 Correct 1002 ms 6136 KB Output is correct
63 Correct 1030 ms 6136 KB Output is correct
64 Correct 1085 ms 6136 KB Output is correct
65 Correct 1110 ms 6136 KB Output is correct
66 Correct 1148 ms 6136 KB Output is correct
67 Correct 1138 ms 6168 KB Output is correct
68 Correct 1090 ms 6240 KB Output is correct
69 Correct 942 ms 6240 KB Output is correct
70 Correct 900 ms 6388 KB Output is correct
71 Correct 844 ms 6388 KB Output is correct
72 Correct 906 ms 6388 KB Output is correct
73 Correct 905 ms 6464 KB Output is correct
74 Correct 163 ms 6464 KB Output is correct
75 Correct 159 ms 6464 KB Output is correct
76 Correct 164 ms 6464 KB Output is correct
77 Correct 160 ms 6464 KB Output is correct
78 Correct 167 ms 6464 KB Output is correct