poj 1830 開關問題(數學:高斯消元)

來源:互聯網
上載者:User

感覺還是蠻簡單的

因為高斯消元中i、j下標敲反了,跪了幾發。。。

我們可以建立形似a[i]^x1^x2^...xn = b[i]的方程組

再解方程就可以了

題中給出i,j表明第i個開關會影響第j個燈

也就意味著第j個燈受xi影響,不要弄反了

代碼如下:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAXN 50using namespace std;int a[MAXN], b[MAXN];int c[MAXN][MAXN];void print(int n) {    for(int i=0; i<n; ++i) {        for(int j=0; j<=n; ++j) {            printf("%d ", c[i][j]);        }        puts("");    }}int gauss(int n) {    int i, j, k, r;    for(i=0; i<n; ++i) {        r = i;        for(j=i+1; j<n; ++j)            if(c[j][i] > c[r][i])                r = j;        if(r != i) {            for(k=0; k<=n; ++k)                swap(c[i][k], c[r][k]);        }        for(j=i+1; j<n; ++j) {            if(c[j][i]) {                for(k=0; k<=n; ++k)                    c[j][k] ^= c[i][k];            }        }    }    //print(n);    int cnt = 0;    for(i=0; i<n; ++i) {        for(j=0; j<=n; ++j)            if(c[i][j] == 1) {                if(j == n)                    return -1;                ++cnt;                break;            }    }    return n-cnt;}int main(void) {    int T, n;    scanf("%d", &T);    while(T--) {        scanf("%d", &n);        memset(a, 0, sizeof(a));        memset(b, 0, sizeof(b));        memset(c, 0, sizeof(c));        for(int i=0; i<n; ++i) {            scanf("%d", &a[i]);        }        for(int i=0; i<n; ++i) {            scanf("%d", &b[i]);            b[i] ^= a[i];            c[i][n] = b[i];            c[i][i] = 1;        }        int x, y;        while(scanf("%d%d", &x, &y)) {            if(x==0 && y==0)                break;            --x; --y;            c[y][x] = 1;        }        //print(n);        //puts("");puts("");puts("");        int ans = gauss(n);        if(ans == -1) puts("Oh,it's impossible~!!");        else cout << (1ll<<ans) << endl;    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.