답안 #927847

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
927847 2024-02-15T11:59:13 Z math_rabbit_1028 Chess Rush (CEOI20_chessrush) C++14
28 / 100
2000 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll MOD = 1'000'000'007;

int R, C, Q;
ll inv[1010];

typedef vector<vector<ll>> matrix;
matrix A, E;
matrix prod(matrix A, matrix B) {
    matrix D = vector<vector<ll>> (C+1, vector<ll>(C+1, 0));
    for (int i = 1; i <= C; i++) {
        for (int j = 1; j <= C; j++) {
            for (int k = 1; k <= C; k++) {
                D[i][j] += A[i][k] * B[k][j];
                D[i][j] %= MOD;
            }
        }
    }
    return D;
}

matrix myPow(matrix A, int x) {
    if (x == 0) return E;
    matrix B = myPow(A, x/2);
    B = prod(B, B);
    if (x % 2 == 0) return B;
    else return prod(B, A);
}
int myPow(int a, int x) {
    if (x == 0) return 1;
    ll b = myPow(a, x/2);
    b = b*b%MOD;
    if (x % 2 == 0) return b;
    else return b*a%MOD;
}
void init() {
    A = vector<vector<ll>> (C+1, vector<ll>(C+1, 0));
    E = vector<vector<ll>> (C+1, vector<ll>(C+1, 0));
    for (int i = 1; i <= C; i++) {
        for (int j = 1; j <= C; j++) {
            if (abs(i-j) <= 1) A[i][j] = 1;
            if (i == j) E[i][j] = 1;
        }
    }
    A = myPow(A, R-1);

    for (int i = 1; i <= C; i++) {
        inv[i] = myPow(i, MOD-2);
    }
}

pll PAWN(int a, int b) {
    if (a == b) return {R-1, 1};
    return {0, 0};
}
pll ROOK(int a, int b) {
    if (a == b) return {1, 1};
    return {2, 2};
}
pll QUEEN(int a, int b) {
    if (a == b || abs(a-b) == R-1) return {1, 1};
    vector<pll> st, ed;
    for (int j = 1; j <= C; j++) if (j != a) st.push_back({1, j});
    for (int i = 2; ; i++) {
        int j = a + i-1;
        if (j > C) break;
        st.push_back({i, j});
    }
    for (int i = 2; ; i++) {
        int j = a + 1-i;
        if (j < 1) break;
        st.push_back({i, j});
    }
    for (int j = 1; j <= C; j++) if (j != b) ed.push_back({R, j});
    for (int i = 2; ; i++) {
        int j = b + i-1;
        if (j > C) break;
        ed.push_back({R-i+1, j});
    }
    for (int i = 2; ; i++) {
        int j = b + 1-i;
        if (j < 1) break;
        ed.push_back({R-i+1, j});
    }

    int cnt = 0;
    for (auto &[x, y] : st) if (y == b) cnt++;
    for (auto &[x, y] : ed) if (y == a) cnt++;
    cnt += st.size() + ed.size();
    st.insert(st.end(), ed.begin(), ed.end());
    sort(st.begin(), st.end());
    st.erase(unique(st.begin(), st.end()), st.end());
    cnt -= st.size();
    return {2, cnt};
}

pll solve(int a, int b) {
    ll t1 = ceil((double)(R-a-b+1)/(2*C-2));
    ll t2 = ceil((double)(R-a-1+b)/(2*C-2));
    pll ans;
    if (t1 < t2) ans = {2*t1+2, 2*t1*(C-1) - (R-a-b+1)};
    else ans = {2*t2+1, 2*t2*(C-1) - (R-a-1+b)};
    ll cnt = 1;
    for (int i = 1; i <= ans.second; i++) {
        cnt *= (ans.first-2+i);
        cnt %= MOD;
        cnt *= inv[i];
        cnt %= MOD;
    }
    return {ans.first, cnt};
}
pll BISHOP(int a, int b) {
    if ((1+a)%2 != (R+b)%2) return {0, 0};
    pll A = solve(a, b), B = solve(C+1-a, C+1-b);
    if (A.first == B.first) return {A.first, A.second + B.second};
    else if (A.first < B.first) return A;
    else return B;
}

pll KING(int a, int b) {
    return {R-1, A[a][b]};
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie();

    cin >> R >> C >> Q;

    init();

    while (Q--) {
        char T; int a, b; pll res;
        cin >> T >> a >> b;
        if (T == 'P') res = PAWN(a, b);
        else if (T == 'R') res = ROOK(a, b);
        else if (T == 'Q') res = QUEEN(a, b);
        else if (T == 'B') res = BISHOP(a, b);
        else if (T == 'K') res = KING(a, b);
        cout << res.first << " " << res.second << "\n";
    }

    return 0;
}

Compilation message

chessrush.cpp: In function 'pll QUEEN(int, int)':
chessrush.cpp:90:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   90 |     for (auto &[x, y] : st) if (y == b) cnt++;
      |                ^
chessrush.cpp:91:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   91 |     for (auto &[x, y] : ed) if (y == a) cnt++;
      |                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Execution timed out 2057 ms 131668 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Execution timed out 2087 ms 19660 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 352 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 352 KB Output is correct
2 Incorrect 1 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 580 KB Output is correct
2 Correct 42 ms 1372 KB Output is correct
3 Correct 27 ms 1116 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 580 KB Output is correct
2 Correct 42 ms 1372 KB Output is correct
3 Correct 27 ms 1116 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 4 ms 348 KB Output is correct
7 Correct 29 ms 1116 KB Output is correct
8 Correct 43 ms 1368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 580 KB Output is correct
2 Correct 42 ms 1372 KB Output is correct
3 Correct 27 ms 1116 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 4 ms 348 KB Output is correct
7 Correct 29 ms 1116 KB Output is correct
8 Correct 43 ms 1368 KB Output is correct
9 Correct 5 ms 600 KB Output is correct
10 Correct 8 ms 604 KB Output is correct
11 Correct 215 ms 3348 KB Output is correct
12 Correct 192 ms 3440 KB Output is correct
13 Correct 6 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 580 KB Output is correct
2 Correct 42 ms 1372 KB Output is correct
3 Correct 27 ms 1116 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 3 ms 348 KB Output is correct
6 Correct 4 ms 348 KB Output is correct
7 Correct 29 ms 1116 KB Output is correct
8 Correct 43 ms 1368 KB Output is correct
9 Correct 5 ms 600 KB Output is correct
10 Correct 8 ms 604 KB Output is correct
11 Correct 215 ms 3348 KB Output is correct
12 Correct 192 ms 3440 KB Output is correct
13 Correct 6 ms 604 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 7 ms 604 KB Output is correct
16 Correct 8 ms 604 KB Output is correct
17 Runtime error 128 ms 262144 KB Execution killed with signal 9
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Execution timed out 2057 ms 131668 KB Time limit exceeded
3 Halted 0 ms 0 KB -