POJ 1830 开关问题 高斯消元

  题目链接: http://poj.org/problem?id=1830

  题目描述: 给出开关的初始, 末状态, 再给出互相影响的开关关系, 输出有几种方式可以从初始到结束状态

  解题思路: 这是个高斯消元题.....题单可能是放错了吧.....解的个数为2^(自由变元的个数)

  代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <stack>
#include <deque>
#include <map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define sca(x) scanf("%d",&x)
#define de printf("=======\n")
typedef long long ll;
using namespace std;

const int maxn = 40;
int a[maxn][maxn];;
int n;
int s[maxn];
int e[maxn];

int gauss() {
    int i;
    int j;
    for( i = 1, j = 1; i <= n && j <= n; j++ ) {
        
            int k = i;
            for( k = i; k <= n; k++ ) {
                if( a[k][j] ) break;
            }
            if( a[k][j] ) {
                for( int r = 1; r <= n+1; r++ ) {
                    swap(a[i][r], a[k][r]);
                }
                for( int r = 1; r <= n; r++ ) {
                    if( r != i && a[r][j] ) {
                        for( int k = 1; k <= n+1; k++ ) {
                            a[r][k] ^= a[i][k];
                        }
                    }
                }
                i++;
            }
        
    }
    for( int j = i; j <= n; j++ ) {
        if( a[j][n+1] ) return -1;
    }
    return 1<<(n-i+1);
}
int main() {
    int t;
    sca(t);
    while( t-- ) {
        sca(n);
        mem0(a);
        for( int i = 1; i <= n; i++ ) {
            sca(s[i]);
        }
        for( int i = 1; i <= n; i++ ) {
            sca(e[i]);
            a[i][n+1] = s[i] ^ e[i];
            a[i][i] = 1;
        }
        int x, y;
        while( scanf( "%d%d", &x, &y ) && x+y ) {
            a[y][x] = 1;
        }
        int ans = gauss();
        if( ans == -1 ) {
            printf( "Oh,it's impossible~!!\n" );
        }
        else {
            printf( "%d\n", ans );
        }
    }
}
View Code

  思考: 有空应该回顾一下高斯消元和fft这种模板的, 等我这两个专题刷完的吧, 不复习有点儿忘了感觉.....是时候应该学习高数了......

posted on 2017-08-28 18:16  FriskyPuppy  阅读(84)  评论(0编辑  收藏  举报

导航