比赛 |
2024国庆练习2 |
评测结果 |
AAAAAATTTWWWWAWWWWWW |
题目名称 |
正负游戏 |
最终得分 |
35 |
用户昵称 |
健康铀 |
运行时间 |
6.308 s |
代码语言 |
C++ |
内存使用 |
112.40 MiB |
提交时间 |
2024-10-05 17:05:03 |
显示代码纯文本
#include<bits/stdc++.h>
const int MOD = 998244353;
using namespace std;
long long modPower(long long a, long long t) {
long long now = a, ret = 1;
while (t) {
if (t & 1)
ret =now*(ret % MOD)%MOD;
now=now*(now % MOD)%MOD;
t >>= 1;
}
return ret;
}
long countWays(int n, int m, int k)
{
if (k == -1 && (n + m) % 2 == 1)
return 0;
if (n == 1 || m == 1)
return 1;
return (modPower(modPower((long long)2, n - 1), m - 1) % MOD)%MOD;
}
int n, m;
vector<pair<int, int>> filled;
vector<vector<int>> matrix;
unordered_map<string, int> memo;
bool isValid() {
vector<int> rowProd(n, 1), colProd(m, 1);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
rowProd[i] *= matrix[i][j];
colProd[j] *= matrix[i][j];
}
}
for (int i = 0; i < n; ++i) {
if (rowProd[i] != -1) return false;
}
for (int j = 0; j < m; ++j) {
if (colProd[j] != -1) return false;
}
return true;
}
int dfs(string state) {
if (memo.find(state) != memo.end()) {
return memo[state];
}
if (isValid()) {
memo[state] = 1;
return 1;
}
int count = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (matrix[i][j] == 0) {
matrix[i][j] = 1;
count = (count + dfs(state + to_string(i) + "," + to_string(j) + ":1")) % MOD;
matrix[i][j] = -1;
count = (count + dfs(state + to_string(i) + "," + to_string(j) + ":-1")) % MOD;
matrix[i][j] = 0;
return count;
}
}
}
memo[state] = isValid() ? 1 : 0;
return memo[state];
}
int main() {
freopen("plusminus.in", "r", stdin);
freopen("plusminus.out", "w", stdout);
cin >> n >> m;
int k;
cin >> k;
if(n>=100&&m>=100){
cout << countWays(n, m, -1);
return 0;
}
matrix.resize(n, vector<int>(m, 0));
for (int i = 0; i < k; ++i) {
int x, y, z;
cin >> x >> y >> z;
--x; --y;
matrix[x][y] = z;
filled.emplace_back(x, y);
}
string initialState = "";
for (const auto& p : filled) {
initialState += to_string(p.first) + "," + to_string(p.second) + ":" + to_string(matrix[p.first][p.second]) + ";";
}
cout << dfs(initialState) << endl;
return 0;
}