c/c++语言开发共享洛谷P2447 [SDOI2010]外星千足虫(异或方程组)

题意 “题目链接” Sol 异或高斯消元的板子题。 bitset优化一下,复杂度$O(frac{nm}{32})$ 找最优解可以考虑高斯消元的过程,因为异或的特殊性质,每次向下找的时候找到第一个1然后交换就行,这样显然是最优的 cpp include using namespace std; co …


题意

题目链接

sol

异或高斯消元的板子题。

bitset优化一下,复杂度(o(frac{nm}{32}))

找最优解可以考虑高斯消元的过程,因为异或的特殊性质,每次向下找的时候找到第一个1然后交换就行,这样显然是最优的

#include<bits/stdc++.h> using namespace std; const int maxn = 2001; inline int read() {     char c = getchar(); int x = 0, f = 1;     while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}     while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();     return x * f; } int n, m; bitset<maxn> b[maxn]; void gauss() {     int ans = 0;     for(int i = 1; i <= n; i++) {         int j = i;         while(!b[j][i] && j < m + 1)              j++;         if(j == m + 1) {puts("cannot determine"); return ;}         ans = max(ans, j);         swap(b[i], b[j]);         for(int j = 1; j <= m; j++) {             if(i == j || !b[j][i]) continue;             b[j] ^= b[i];         }     }     printf("%dn", ans);     for(int i = 1; i <= n; i++)         puts(!b[i][n + 1] ? "earth" : "?y7m#"); } int main() {     n = read(); m = read();     for(int i = 1; i <= m; i++) {         string s; cin >> s;          b[i][n + 1] = read();         for(int j = 1; j <= n; j++) b[i][j] = (s[j - 1] == '0' ? 0 : 1);     }     gauss();     return 0; }

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/605286.html

(0)
上一篇 2021年5月13日
下一篇 2021年5月13日

精彩推荐