生活随笔
收集整理的這篇文章主要介紹了
[CQOI2014]和谐矩阵
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
嘟嘟嘟
遇到這種(看似)構(gòu)造的題,我好像一般都做不出來……
然而這題正解是高斯消元解異或方程組……
首先我們?nèi)菀琢谐鍪阶觓[i][j] ^ a[i - 1][j] ^ a[i + 1][j] ^ a[i][j - 1] ^ a[i][j + 1] = 0。于是我們列出所有像這樣的\(n * m\)個(gè)式子,然后\(O((nm) ^ 3)\)高斯消元加bitset優(yōu)化就過了。
講真我還不會高斯消元解異或方程組,就現(xiàn)學(xué)了一下。其實(shí)就是把運(yùn)算改成了異或,然后bitset可以把一個(gè)一個(gè)消改成一行和一行消。
#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;
}
轉(zhuǎn)載于:https://www.cnblogs.com/mrclr/p/10461668.html
總結(jié)
以上是生活随笔為你收集整理的[CQOI2014]和谐矩阵的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。