0x35 开关问题 (高斯消元)

x i x_i xi 表示第 i i i 个开关的操作情况, x i = 1 x_i=1 xi=1 表示按下这个开关, x i = 0 x_i=0 xi=0 表示没有按

再统计 a i , j a_{i,j} ai,j 表示第 i i i 个开关的第 j j j 个开关的联系情况, a i , j a_{i,j} ai,j 表示按下 j j j 会影响 i i i 的状态,反之亦然,特别的令 a i , i = 1 a_{i,i}=1 ai,i=1

设开关当前状态为 a i a_i ai,结束时要求为 b i b_i bi,则我们能够通过异或关系得到 n n n 个方程

{ a 1 , 1 x 1   x o r   a 1 , 2 x 2   x o r   . . .   x o r   a 1 , n x n = a 1   x o r   b 1 a 2 , 1 x 1   x o r   a 2 , 2 x 2   x o r   . . .   x o r   a 2 , n x n = a 2   x o r   b 2 ⋯ a n , 1 x 1   x o r   a n , 2 x 2   x o r   . . .   x o r   a n , n x n = a n   x o r   b n \begin{cases} a_{1,1}x_1 \ xor \ a_{1,2}x_2 \ xor \ ... \ xor \ a_{1,n}x_n=a_1\ xor \ b_1 \\ a_{2,1}x_1\ xor \ a_{2,2}x_2 \ xor \ ... \ xor \ a_{2,n}x_n=a_2\ xor \ b_2 \\ &\cdots\\ a_{n,1}x_1\ xor \ a_{n,2}x_2 \ xor \ ... \ xor \ a_{n,n}x_n=a_n\ xor \ b_n \end{cases} a1,1x1 xor a1,2x2 xor ... xor a1,nxn=a1 xor b1a2,1x1 xor a2,2x2 xor ... xor a2,nxn=a2 xor b2an,1x1 xor an,2x2 xor ... xor an,nxn=an xor bn

根据基本的二进制运算常识,我们知道疑惑就是二进制加法,我们仍然能够写出增广矩阵,矩阵中的每个值要么为0,要么为1,在高斯消元的时候,把加减替换成异或,不需要支持乘法((**))

最终把该异或方程组转化成对应的上三角矩阵,

若存在 0 = 1 0=1 0=1则判无解

否则,如果答案就是 2 c n t 2^{cnt} 2cnt c n t cnt cnt 表示自由元个数

#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define loop(s, v, it) for (s::iterator it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (int i = head[x]; i; i = e[i].nxt)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, sum) memset(a, sum, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define iv inline void
#define enter cout << endl
#define siz(x) ((int)x.size())
#define file(s) freopen(s".in", "r", stdin), freopen(s."out", "w", stdout)
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <int, int> pii ;
typedef vector <int> vi ;
typedef vector <pii> vii ;
typedef queue <int> qi ;
typedef set <int> si ;
typedef map <int, int> mii ;
typedef map <string, int> msi ;
const int N = 110 ;
const int INF = 0x3f3f3f3f ;
const int iinf = 1 << 30 ;
const ll linf = 2e18 ;
const int MOD = 1000000007 ;
const double eps = 1e-7 ;
void print(int x) { cout << x << endl ; exit(0) ; }
void PRINT(string x) { cout << x << endl ; exit(0) ; }
void douout(double x){ printf("%lf\n", x + 0.0000000001) ; }

int n, ans ;
int a[N] ;

signed main(){
    int T ; scanf("%d", &T) ;
    while (T--) {
    	scanf("%d", &n) ;
    	rep(i, 1, n) scanf("%d", &a[i]) ;
    	rep(i, 1, n) {
    		int j ; scanf("%d", &j) ;
    		a[i] ^= j ;
    		a[i] |= 1 << i ;
		}
		int x, y ;
		while (scanf("%d%d", &x, &y) != EOF && x && y) a[y] |= 1 << x ;
		ans = 1 ;
		rep(i, 1, n) {
			rep(j, i + 1, n) if (a[j] > a[i]) swap(a[i], a[j]) ;
			if (!a[i]) {
				ans = 1 << (n - i + 1) ;
				break ;
			}
			if (a[i] == 1) {
				ans = 0 ;
				break ;
			}
			per(k, n, 1)
			if (a[i] >> k & 1){
				rep(j, 1, n) if (i != j && (a[j] >> k & 1)) a[j] ^= a[i] ;
				break ;
			}
		}
		if (!ans) puts("Oh,it's impossible~!!") ;
		else printf("%d\n", ans) ;
	}


	return 0 ;
}

/*
写代码时请注意:
	1.ll?数组大小,边界?数据范围?
	2.精度?
	3.特判?
	4.至少做一些
思考提醒:
	1.最大值最小->二分?
	2.可以贪心么?不行dp可以么
	3.可以优化么
	4.维护区间用什么数据结构?
	5.统计方案是用dp?模了么?
	6.逆向思维?
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值