QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#690873#5576. Advertising ICPCohiostatescarlet#TL 0ms3588kbC++172.4kb2024-10-31 07:14:212024-10-31 07:14:21

Judging History

你现在查看的是最新测评结果

  • [2024-10-31 07:14:21]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3588kb
  • [2024-10-31 07:14:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int n, m;
int grid[8][8];
const int MOD = 998244353;
int dp[8][6600];
bool match[6600][6600];
ll COMBOS;

bool valid(int row, int vals) {
    for (int i = 0; i < m; i++) {
        int c = vals % 3;
        if (grid[row][i] != 3 && grid[row][i] != c) return false;
        vals /= 3;
    }
    return true;
}

vector<int> convert(int vals) {
    vector<int> res(m);
    for (int i = 0; i < m; i++) {
        res[i] = vals % 3;
        vals /= 3;
    }
    return res;
}

ll modpow(ll b, ll e) {
    ll ans = 1;
    for (; e; b = b * b % MOD, e /= 2) {
        if (e & 1) ans = ans * b % MOD;
    }
    return ans;
}

void solve(int row) {
    if (row >= n) return;
    for (int j = 0; j < COMBOS; j++) {
        if (!valid(row, j)) continue;
        for (int i = 0; i < COMBOS; i++) {
            if (!match[i][j]) {
                dp[row][j] += dp[row - 1][i];
                if (dp[row][j] >= MOD) dp[row][j] -= MOD;
            }
        }
    }
    solve(row + 1);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin.exceptions(cin.failbit);
    cin >> n >> m;
    COMBOS = modpow(3, m);
    int blanks = 0;
    for (int i = 0; i < n; i++) {
        string s; cin >> s;
        for (int j = 0; j < m; j++) {
            grid[i][j] = string("CIP?").find(s[j]);
            if (grid[i][j] == 3) blanks++;
        }
    }
    for (int i = 0; i < COMBOS; i++) {
        if (valid(0, i)) dp[0][i] = 1;
    }
    vector<vector<int>> precomp;
    for (int i = 0; i < COMBOS; i++) {
        precomp.push_back(convert(i));
    }
    for (int i = 0; i < COMBOS; i++) {
        auto &row = precomp[i];
        for (int j = 0; j < COMBOS; j++) {
            auto &row2 = precomp[j];
            for (int k = 1; k < m; k++) {
                if (row2[k] == 0 && row2[k - 1] == 2 && row[k] == 0 && row[k - 1] == 1) {
                    match[i][j] = true;
                }
            }
        }
    }
    solve(1);
    ll total = modpow(3, blanks);
    for (int i = 0; i < COMBOS; i++) {
        total -= dp[n - 1][i];
        if (total < 0) total += MOD;
    }
    cout << total << '\n';
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3588kb

input:

3 3
???
?I?
???

output:

243

result:

ok single line: '243'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

2 2
IC
PC

output:

1

result:

ok single line: '1'

Test #3:

score: -100
Time Limit Exceeded

input:

8 8
????????
????????
????????
????????
????????
????????
????????
????????

output:


result: