NC213947. RikkawithGame
描述
输入描述
The first line contains a single integer , representing the number of players.
Then n lines follow. Each line contains an 01-string of length n. if and only if player i and player j are friends.
The input guarantees that and .
输出描述
Output a single line with a 01-string res of length n. if and only if the game will end immediately in the first turn if player i is selected as the first dragon.
示例1
输入:
2 00 00
输出:
00
说明:
Whatever which player is selected, the other player will choose to attack: He/she will get 100 points if he chooses to attack, otherwise, he/she can get only 10 points. Therefore, the game continues to the second turn.示例2
输入:
3 000 000 000
输出:
111
说明:
Without loss of the generality, suppose the third player is selected as the first dragon. Then:示例3
输入:
4 0111 1000 1000 1000
输出:
1111
示例4
输入:
4 0101 1010 0101 1010
输出:
0000
C++(clang++11) 解法, 执行用时: 185ms, 内存消耗: 2680K, 提交时间: 2021-01-18 23:39:51
#include <bits/stdc++.h> using namespace std; const int N = 505; const int mod = 998244353; inline int qp(int a, int b) { int res = 1; while (b) { if (b & 1) res = 1ll * res * a % mod; a = 1ll * a * a % mod; b >>= 1; } return res; } mt19937 rng(1145141); int n, a[N][N * 2], cnt; char str[N]; inline void Gauss() { for (int i = 1, j, k; i <= n; i++) { for (j = cnt + 1; j <= n; j++) if (a[j][i]) break; if (j > n) continue; cnt++; if (j != cnt) for (k = 1; k <= n * 2; k++) swap(a[j][k], a[cnt][k]); int iv = qp(a[cnt][i], mod - 2); for (j = cnt + 1; j <= n; j++) { int t = 1ll * (mod - a[j][i]) * iv % mod; for (k = i; k <= n * 2; k++) a[j][k] = (a[j][k] + 1ll * a[cnt][k] * t) % mod; } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf(" %s", str + 1); for (int j = i + 1; j <= n; j++) { if (str[j] == '0') { int x = uniform_int_distribution<int>(1, mod - 1)(rng); a[i][j] = x; a[j][i] = mod - x; } } } for (int i = 1; i <= n; i++) a[i][i + n] = uniform_int_distribution<int>(1, mod - 1)(rng); Gauss(); for (int s = 1; s <= n; s++) { int flag = 0; for (int i = cnt + 1; i <= n; i++) if (a[i][n + s]) { flag = 1; break; } putchar(flag ? '1' : '0'); } putchar('\n'); return 0; }