Tsinsen-A1488 : 魔法波【高斯消元+异或方程组】
高斯消元。
自己只能想出来把每一个点看成一个变量,用Xi表示其状态,这样必定TLE,n^2 个变量,再加上3次方的高斯消元(当然,可以用bitset压位)。
正解如下:
我们把地图划分成一个个的横条和竖条,对于点i,我们用Li,Ri分别表示横着和竖着穿过它的,显然,对于每一个点,有且仅有一个L块和R块穿过。
得到第一个方程 YLi = sigma(Xp) p属于Li,YRi = sigma(Xp) p属于Ri --> sigma(Xp) xor Yi = 0.
接着我们考虑, Si xor YLi xor YRi xor Xi = 1 这是第二个方程,对其移项,那么变成了 Xi = 1 xor Si xor YLi xor YRi.
将其回带到第一个式子中即可,对于每一个点,放在不同的两个方程里,一个横的,一个竖的即可。
TLE50 我将枚举自由变量的语句删除后,发现没有TLE了,但是最多只有2000个障碍物,最多是O(2000^2)的复杂度,可能是数据加强了吧。
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define drep(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define clr(x) memset(x, 0, sizeof(x));
#define xx first
#define yy second
using namespace std;
typedef long long i64;
typedef pair<int, int> pii;
const int inf = ~0U>>;
const i64 INF = ~0ULL>>;
//***************************
const int maxn = ;
char ma[maxn][maxn];
pii no[maxn][maxn];
int n, cnt_x, cnt_eq;
bitset<> eq[];
void init() {
rep(i, , n) {
if (ma[i][] != 'X') no[i][].xx = ++cnt_x;
rep(j, , n) {
if (ma[i][j] == 'X') continue;
if (no[i][j - ].xx) no[i][j].xx = no[i][j - ].xx;
else no[i][j].xx = ++cnt_x;
}
}
rep(i, , n) {
if (ma[][i] != 'X') no[][i].yy = ++cnt_x;
rep(j, , n) {
if (ma[j][i] == 'X') continue;
if (no[j - ][i].yy) no[j][i].yy = no[j - ][i].yy;
else no[j][i].yy = ++cnt_x;
}
}
cnt_eq = cnt_x;
rep(i, , cnt_x) eq[i].set(i);
rep(i, , n) rep(j, , n) if (no[i][j].xx) {
int l = no[i][j].xx, r = no[i][j].yy;
eq[l].flip(l), eq[l].flip(r), eq[r].flip(l), eq[r].flip(r);
if ( ^ (ma[i][j] - '')) eq[l].flip(cnt_x + ), eq[r].flip(cnt_x + );
}
}
void gauss() {
rep(i, , cnt_x) {
rep(j, i, cnt_eq) if (eq[j][i] > eq[i][i]) { swap(eq[j], eq[i]); break; }
rep(j, , cnt_eq) if (i != j && eq[j][i]) eq[j] ^= eq[i];
}
}
int choice[];
int main() {
scanf("%d", &n);
rep(i, , n) scanf("%s", ma[i] + );
init();
gauss();
drep(i, cnt_x, ) {
if (eq[i][i]) {
choice[i] = eq[i][cnt_x + ];
drep(j, cnt_x, i + ) if (eq[i][j]) choice[i] ^= choice[j];
}
else choice[i] = ;
}
rep(i, , n) {
rep(j, , n)
if (ma[i][j] == 'X') printf("");
else printf("%d", xor (ma[i][j] - '') xor choice[no[i][j].xx] xor choice[no[i][j].yy]);
puts("");
}
return ;
}
Tsinsen-A1488 : 魔法波【高斯消元+异或方程组】的更多相关文章
- BZOJ.1923.[SDOI2010]外星千足虫(高斯消元 异或方程组 bitset)
题目链接 m个方程,n个未知量,求解异或方程组. 复杂度比较高,需要借助bitset压位. 感觉自己以前写的(异或)高斯消元是假的..而且黄学长的写法都不需要回代. //1100kb 324ms #i ...
- UVA11542 Square(高斯消元 异或方程组)
建立方程组消元,结果为2 ^(自由变元的个数) - 1 采用高斯消元求矩阵的秩 方法一: #include<cstdio> #include<iostream> #includ ...
- [TS-A1488][2013中国国家集训队第二次作业]魔法波[高斯消元]
暴力直接解异或方程组,O(n^6)无法接受,那么我们考虑把格子分块,横着和竖着分别分为互不影响的块,这样因为障碍物最多不超过200个,那么块的个数最多为2*(800+200)=2000个,最后用bit ...
- UVa 11542 (高斯消元 异或方程组) Square
书上分析的太清楚,我都懒得写题解了.=_=|| #include <cstdio> #include <cstring> #include <cmath> #inc ...
- POJ.1830.开关问题(高斯消元 异或方程组)
题目链接 显然我们需要使每个i满足\[( ∑_{j} X[j]*A[i][j] ) mod\ 2 = B[i]\] 求这个方程自由元Xi的个数ans,那么方案数便是\(2^{ans}\) %2可以用^ ...
- UVA 11542 Square 高斯消元 异或方程组求解
题目链接:点击打开链接 白书的例题练练手. . . P161 #include <cstdio> #include <iostream> #include <algori ...
- 【高斯消元解xor方程组】BZOJ2466-[中山市选2009]树
[题目大意] 给出一棵树,初始状态均为0,每反转一个节点的状态,相邻的节点(父亲或儿子)也会反转,问要使状态均为1,至少操作几次? [思路] 一场大暴雨即将来临,白昼恍如黑夜!happy! 和POJ1 ...
- poj1830(高斯消元解mod2方程组)
题目链接:http://poj.org/problem?id=1830 题意:中文题诶- 思路:高斯消元解 mod2 方程组 有 n 个变元,根据给出的条件列 n 个方程组,初始状态和终止状态不同的位 ...
- bzoj2115 [Wc2011] Xor——高斯消元 & 异或线性基
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2115 异或两次同一段路径的权值,就相当于没有走这段路径: 由此可以得到启发,对于不同的走法, ...
随机推荐
- FZU 2091 播放器(栈应用)
栈基础应用 #include<iostream> #include<cstdio> #include<cstring> #include<vector> ...
- 1--OC -- HelloWorld
一.点击Xcode,选择“Create a new Xcode project” 二.左边选择“OS X Application”,右边选择“Command Line Tool”,Next 三.输 ...
- maven认识
在这里普及一下知识: ┣ maven与ant是同类,构建工具 ┣ svn与cvs,css是同类,版本控制工具 1.为什么要用Maven? 在进行软件开发的过程中,无论什么项目,采用何种技术,使用何种编 ...
- angularjs model.service vs provider vs factory?
<!DOCTYPE html> <html ng-app="app"> <head> <script src="http://c ...
- Light OJ - 1058 Parallelogram Counting(判定平行四边形)
Description There are n distinct points in the plane, given by their integer coordinates. Find the n ...
- AutoFac使用方法总结
AutoFac是.net平台下的IOC容器产品,它可以管理类之间的复杂的依赖关系.在使用方面主要是register和resolve两类操作. 这篇文章用单元测试的形式列举了AutoFac的常用使用方法 ...
- 转 Linux进程状态分析
众所周知,现在的分时操作系统能够在一个CPU上运行多个程序,让这些程序表面上看起来是在同时运行的.linux就是这样的一个操作系统.在linux系统中,每个被运行的程序实例对应一个或多个进程.l ...
- ibatis中resultClass与resultMap 的区别
ibatis的resultClass与resultMap还是有很大的区别.以下是我碰到的一个问题. 配置文件写法如下: 1 sqlMap2 typeAlias alias="notice&q ...
- MJExtention
+ (NSDictionary *)mj_objectClassInArray { // key : 属性名 // value : 类名 return @{ @"dogs" : @ ...
- ida调试 android so
C:\Documents and Settings\Administrator>adb shellshell@htc_v2_dtg:/ $ susushell@htc_v2_dtg:/ # cd ...