[CQOI2014]和谐矩阵
嘟嘟嘟
遇到这种(看似)构造的题,我好像一般都做不出来……
然而这题正解是高斯消元解异或方程组……
首先我们容易列出式子a[i][j] ^ a[i - 1][j] ^ a[i + 1][j] ^ a[i][j - 1] ^ a[i][j + 1] = 0。于是我们列出所有像这样的\(n * m\)个式子,然后\(O((nm) ^ 3)\)高斯消元加bitset优化就过了。
讲真我还不会高斯消元解异或方程组,就现学了一下。其实就是把运算改成了异或,然后bitset可以把一个一个消改成一行和一行消。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<ctime>
#include<bitset>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1605;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m;
const int dx[] = {-1, 0, 1, 0, 0}, dy[] = {0, 1, 0, -1, 0};
bitset<maxn> f[maxn];
In int num(int x, int y)
{
return (x - 1) * m + y;
}
int ans[maxn];
In void Gauss(int n)
{
for(int i = 1; i <= n; ++i)
{
int pos = i;
while(pos <= n && !f[pos][i]) ++pos;
if(pos == n + 1) continue;
swap(f[i], f[pos]);
for(int j = i + 1; j <= n; ++j) if(f[j][i]) f[j] ^= f[i];
}
for(int i = n; i; --i)
if(!f[i][i]) ans[i] = 1;
else for(int j = i + 1; j <= n; ++j) if(f[i][j]) ans[i] ^= ans[j];
}
int main()
{
n = read(), m = read();
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
for(int k = 0; k <= 4; ++k)
{
int x = i + dx[k], y = j + dy[k];
if(x < 1 || x > n || y < 1 || y > m) continue;
f[num(i, j)][num(x, y)] = 1;
}
// for(int i = 1; i <= n; ++i) cout << f[i].to_string() << endl;
Gauss(n * m);
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j) write(ans[num(i, j)]), space;
enter;
}
return 0;
}