답안 #415732

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
415732 2021-06-01T12:18:56 Z dolphingarlic Chess Rush (CEOI20_chessrush) C++14
100 / 100
1200 ms 226048 KB
#include <bits/stdc++.h>

#include "arithmetics.h"
using namespace std;

pair<int, int> solve_bishop(int x, int y, int r, int c) {
	int moves = 1 + (r - x + c - 2) / (c - 1);
	x--, y--;
	int end_col = 1 + (r - x + c - 3) % (c - 1);
	if (moves & 1) y = c - y - 1;
	int rem;
	if (end_col <= y)
		rem = (y - end_col) / 2;
	else {
		moves++;
		rem = c - 1 - (end_col + y) / 2;
	}
	int ways = 1;
	for (int i = 1; i <= rem; i++)
		ways = Div(Mul(ways, moves + rem - 1 - i), i);
	return {moves, ways};
}

int dp[1002][1002], nxt[1002][1002];

void precompute_king(int r, int c) {
	vector<vector<int>> nxt(c + 2, vector<int>(c + 2, 0));
	if (r == 2) {
		for (int i = 1; i <= c; i++)
			nxt[i][i - 1] = nxt[i][i] = nxt[i][i + 1] = 1;
		for (int i = 1; i <= c; i++)
			nxt[i][0] = nxt[i][c + 1] = nxt[0][i] = nxt[c + 1][i] = 0;
	} else if (r & 1) {
		precompute_king(r / 2 + 1, c);
		for (int j = 1; j <= c; j++)
			for (int k = 1; k <= c; k++) {
				nxt[1][j] = Add(nxt[1][j], Mul(dp[1][k], dp[k][j]));
				nxt[j][1] = nxt[1][j];
			}
		for (int i = 2; i <= c; i++)
			for (int j = 2; j <= c; j++)
				if (i + j <= c + 1)
					nxt[i][j] = Add(nxt[i - 1][j - 1], nxt[1][i + j - 1]);
		for (int i = 2; i <= c; i++)
			for (int j = 2; j <= c; j++)
				if (i + j > c + 1)
					nxt[i][j] = nxt[c + 1 - i][c + 1 - j];
	} else {
		precompute_king(r - 1, c);
		for (int i = 1; i <= c; i++)
			for (int j = 1; j <= c; j++)
				nxt[i][j] = Add(dp[i][j - 1], Add(dp[i][j], dp[i][j + 1]));
	}
	for (int i = 1; i <= c; i++)
		for (int j = 1; j <= c; j++)
			dp[i][j] = nxt[i][j];
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	int r, c, q;
	cin >> r >> c >> q;
	precompute_king(r, c);
	while (q--) {
		char p;
		int x, y;
		cin >> p >> x >> y;
		switch (p) {
			case 'P':
				if (x == y)
					cout << r - 1 << " 1\n";
				else
					cout << "0 0\n";
				break;
			case 'R':
				if (x == y)
					cout << "1 1\n";
				else
					cout << "2 2\n";
				break;
			case 'Q':
				if (x == y || x + r - 1 == y || x - r + 1 == y)
					cout << "1 1\n";
				else {
					int ways = 4;
					// Extremities
					if (r == c && (y == 1 || y == r || x == 1 || x == r))
						ways++;
					if (x + y + r & 1) {
						// Double diagonal
						if (r - x < y) ways++;
						if (r - c + x - 1 < c - y + 1) ways++;
					}
					cout << "2 " << ways << '\n';
				}
				break;
			case 'B':
				if (x + y + r & 1) {
					if (r == c && ((x == 1 && y == c) || (x == c && y == 1)))
						cout << "1 1\n";
					else {
						pair<int, int> move_left = solve_bishop(x, y, r, c);
						pair<int, int> move_right =
							solve_bishop(c - x + 1, c - y + 1, r, c);
						if (move_left.first > move_right.first)
							move_left = move_right;
						else if (move_left.first == move_right.first) {
							move_left.second = Add(move_left.second, move_right.second);
						}
						cout << move_left.first << ' ' << move_left.second
							 << '\n';
					}
				} else
					cout << "0 0\n";
				break;
			case 'K':
				cout << r - 1 << ' ' << dp[x][y] << '\n';
				break;
		}
	}
	return 0;
}

Compilation message

chessrush.cpp: In function 'int main()':
chessrush.cpp:89:16: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   89 |      if (x + y + r & 1) {
      |          ~~~~~~^~~
chessrush.cpp:98:15: warning: suggest parentheses around '+' in operand of '&' [-Wparentheses]
   98 |     if (x + y + r & 1) {
      |         ~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 448 ms 82184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 50 ms 9036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 404 ms 71528 KB Output is correct
6 Correct 214 ms 33356 KB Output is correct
7 Correct 4 ms 656 KB Output is correct
8 Correct 1186 ms 198240 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 15 ms 2212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 3 ms 1100 KB Output is correct
3 Correct 3 ms 1100 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 3 ms 1100 KB Output is correct
3 Correct 3 ms 1100 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 3 ms 972 KB Output is correct
8 Correct 6 ms 1132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 3 ms 1100 KB Output is correct
3 Correct 3 ms 1100 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 3 ms 972 KB Output is correct
8 Correct 6 ms 1132 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 3 ms 716 KB Output is correct
11 Correct 14 ms 3164 KB Output is correct
12 Correct 14 ms 2892 KB Output is correct
13 Correct 2 ms 588 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 3 ms 1100 KB Output is correct
3 Correct 3 ms 1100 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 1 ms 460 KB Output is correct
7 Correct 3 ms 972 KB Output is correct
8 Correct 6 ms 1132 KB Output is correct
9 Correct 2 ms 588 KB Output is correct
10 Correct 3 ms 716 KB Output is correct
11 Correct 14 ms 3164 KB Output is correct
12 Correct 14 ms 2892 KB Output is correct
13 Correct 2 ms 588 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 3 ms 716 KB Output is correct
16 Correct 3 ms 588 KB Output is correct
17 Correct 1054 ms 198252 KB Output is correct
18 Correct 1124 ms 225968 KB Output is correct
19 Correct 843 ms 164548 KB Output is correct
20 Correct 888 ms 170564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 448 ms 82184 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 50 ms 9036 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 404 ms 71528 KB Output is correct
12 Correct 214 ms 33356 KB Output is correct
13 Correct 4 ms 656 KB Output is correct
14 Correct 1186 ms 198240 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 15 ms 2212 KB Output is correct
17 Correct 1 ms 460 KB Output is correct
18 Correct 3 ms 1100 KB Output is correct
19 Correct 3 ms 1100 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 2 ms 460 KB Output is correct
22 Correct 1 ms 460 KB Output is correct
23 Correct 3 ms 972 KB Output is correct
24 Correct 6 ms 1132 KB Output is correct
25 Correct 2 ms 588 KB Output is correct
26 Correct 3 ms 716 KB Output is correct
27 Correct 14 ms 3164 KB Output is correct
28 Correct 14 ms 2892 KB Output is correct
29 Correct 2 ms 588 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 3 ms 716 KB Output is correct
32 Correct 3 ms 588 KB Output is correct
33 Correct 1054 ms 198252 KB Output is correct
34 Correct 1124 ms 225968 KB Output is correct
35 Correct 843 ms 164548 KB Output is correct
36 Correct 888 ms 170564 KB Output is correct
37 Correct 1018 ms 190312 KB Output is correct
38 Correct 1200 ms 226048 KB Output is correct
39 Correct 1032 ms 202220 KB Output is correct
40 Correct 2 ms 332 KB Output is correct
41 Correct 916 ms 178312 KB Output is correct
42 Correct 2 ms 332 KB Output is correct