[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;
}
[CQOI2014]和谐矩阵的更多相关文章
- BZOJ 3503: [Cqoi2014]和谐矩阵( 高斯消元 )
偶数个相邻, 以n*m个点为变量, 建立异或方程组然后高斯消元... O((n*m)^3)复杂度看起来好像有点大...但是压一下位的话就是O((n*m)^3 / 64), 常数小, 实际也跑得很快. ...
- 【高斯消元】BZOJ3503 [Cqoi2014]和谐矩阵
3503: [Cqoi2014]和谐矩阵 Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1197 Solved: ...
- BZOJ_3503_[Cqoi2014]和谐矩阵_高斯消元
BZOJ_3503_[Cqoi2014]和谐矩阵_高斯消元 题意: 我们称一个由0和1组成的矩阵是和谐的,当且仅当每个元素都有偶数个相邻的1.一个元素相邻的元素包括它本身,及他上下左右的4个元素(如果 ...
- bzoj千题计划105:bzoj3503: [Cqoi2014]和谐矩阵(高斯消元法解异或方程组)
http://www.lydsy.com/JudgeOnline/problem.php?id=3503 b[i][j] 表示i对j是否有影响 高斯消元解异或方程组 bitset优化 #include ...
- 3503: [Cqoi2014]和谐矩阵
3503: [Cqoi2014]和谐矩阵 链接 分析: 对于每个点,可以列出一个方程a[i][j]=a[i][j-1]^a[i][j+1]^a[i-1][j]^a[i+1][j],于是可以列出n*m个 ...
- P3164 [CQOI2014]和谐矩阵
P3164 [CQOI2014]和谐矩阵 乱写能AC,暴力踩标程(雾 第一眼 诶这题能暴力枚举2333!!! 第二眼 诶这题能高斯消元!那只需要把每个位置的数给设出来就能够列方程了!然后就可以\(O( ...
- BZOJ3503:[CQOI2014]和谐矩阵(高斯消元,bitset)
Description 我们称一个由0和1组成的矩阵是和谐的,当且仅当每个元素都有偶数个相邻的1.一个元素相邻的元素包括它本 身,及他上下左右的4个元素(如果存在). 给定矩阵的行数和列数,请计算并输 ...
- Luogu3164 CQOI2014 和谐矩阵 异或高斯消元
传送门 题意:给出$N,M$,试构造一个$N \times M$的非全$0$矩阵,其中所有格子都满足:它和它上下左右四个格子的权值之和为偶数.$N , M \leq 40$ 可以依据题目中的条件列出有 ...
- bzoj 3503: [Cqoi2014]和谐矩阵【高斯消元】
如果确定了第一行,那么可以推出来整个矩阵,矩阵合法的条件是n+1行全是0 所以推出来n+1行和1行的关系,然后用异或高斯消元来解即可 #include<iostream> #include ...
- P3164 [CQOI2014]和谐矩阵(高斯消元 + bitset)
题意:构造一个$n*m$矩阵 使得每个元素和上下左右的xor值=0 题解:设第一行的每个元素值为未知数 可以依次得到每一行的值 然后把最后一行由题意条件 得到$m$个方程 高斯消元解一下 bitset ...
随机推荐
- web站点和windows服务项目发布时如何排除指定文件
在发布asp.net站点和windows服务项目时,有的时候这样的需求:msbuild编译之后发布到服务器指定目录时要排除指定文件,比如通过jenkins构建时,不希望覆盖原来的Web.config和 ...
- Python 语法笔记
1.else与while结合 while a>0: pass else: pass #当a<=0时执行 2.with语法,无需关闭文件,python自动关闭 with open('a.tx ...
- 深入理解SpringCloud与微服务构建
旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/81742534 目录 一.SpringClou ...
- PHP常用函数总结(二)
PHP常用函数总结 数学函数 1.abs(): 求绝对值 $abs = abs(-4.2); //4.2 数字绝对值数字 2.ceil(): 进一法取整 echo ceil(9.999); // 10 ...
- struts2框架-----Action
控制器Action Action对象是struts2框架的核心,每个URL映射到特定的Action,其提供处理来自用户的请求所需要的处理逻辑.Action有两个重要的功能,即将数据从请求传递到视图和协 ...
- webpack安装
npm install -g webpack webpack-dev-server
- git pull遇到错误:error: Your local changes to the following files would be overwritten by merge:
方法1:如果你想保留刚才本地修改的代码,并把git服务器上的代码pull到本地(本地刚才修改的代码将会被暂时封存起来) git stash git pull origin master git sta ...
- Windows 10文件夹Shirt+鼠标右键出现“在此处打开命令窗口”
Windows 10文件夹Shirt+鼠标右键出现“在此处打开命令窗口” Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directo ...
- python 标准类库-并行执行之subprocess-子进程管理
标准类库-并行执行之subprocess-子进程管理 by:授客QQ:1033553122 1.使用subprocess模块 以下函数是调用子进程的推荐方法,所有使用场景它们都能处理.也可用Popen ...
- android开发——Android开发中的47个小知识
1.判断sd卡是否存在 boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environme ...