[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;
}

转载于:https://www.cnblogs.com/mrclr/p/10461668.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值