• poj1830 开关问题



    (a_{i,j}) 表示第 (j) 个开关对第 (i) 号开关产生的影响,(x_i) 为对第 (i) 个开关的操作,则

    [egin{cases} a_{1,1}x_1 mathrm{xor} a_{1,2}x_2 mathrm{xor} cdots mathrm{xor} a_{1,n}x_n=start_1 mathrm{xor} end_1 \ a_{2,1}x_1 mathrm{xor} a_{2,2}x_2 mathrm{xor} cdots mathrm{xor} a_{2,n}x_n=start_2 mathrm{xor} end_2 \ cdots \ a_{n,1}x_1 mathrm{xor} a_{n,2}x_2 mathrm{xor} cdots mathrm{xor} a_{n,n}x_n=start_n mathrm{xor} end_n \ end{cases}]

    解就是了

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    using namespace std;
    int T, a[35], sta, end, n, uu, vv;
    int gauss(){
    	for(int i=1; i<=n; i++){
    		int maxi=i;
    		for(int j=i+1; j<=n; j++)
    			if(a[j]>a[maxi])
    				maxi = j;
    		swap(a[maxi], a[i]);
    		if(a[i]==0)	return 1<<(n-i+1);
    		if(a[i]==1)	return -1;
    		for(int j=n; j; j--)
    			if(a[i]&(1<<j)){
    				for(int k=1; k<=n; k++)
    					if(k!=i && a[k]&(1<<j))
    						a[k] ^= a[i];
    				break;
    			}
    	}
    	return 1;
    }
    int main(){
    	cin>>T;
    	while(T--){
    		memset(a, 0, sizeof(a));
    		sta = end = 0;
    		scanf("%d", &n);
    		for(int i=1; i<=n; i++){
    			scanf("%d", &uu);
    			a[i] ^= uu;
    		}
    		for(int i=1; i<=n; i++){
    			scanf("%d", &uu);
    			a[i] ^= uu;
    			a[i] |= 1<<i;
    		}
    		while(scanf("%d %d", &uu, &vv)!=EOF){
    			if(!uu && !vv)	break;
    			a[vv] |= 1<<uu;
    		}
    		int re=gauss();
    		if(re<0)	printf("Oh,it's impossible~!!
    ");
    		else	printf("%d
    ", re);
    	}
    	return 0;
    }
    
  • 相关阅读:
    关于jquery判断对象是否为空
    php设计模式学习之观察者模式
    php设计模式学习之单例模式
    Nginx+phpfastcgi下flush输出问题
    php设计模式学习之工厂模式
    centos设置ssh免密码登陆
    关于yii2自带验证码功能不显示问题
    html原样输出html代码
    centos7安装iptables
    centos下配置nginx支持php
  • 原文地址:https://www.cnblogs.com/poorpool/p/8520357.html
Copyright © 2020-2023  润新知