POJ 1830 開關問題 高斯消元,poj1830

來源:互聯網
上載者:User

POJ 1830 開關問題 高斯消元,poj1830

題目大意:給出燈的一些關係,求有多少種方法從始狀態到終狀態。


思路:其實根據燈的這些關係就可以列出一系列方程,然後用高斯消元就可以求出自由元的數量,答案就是2^自由元的數量。


CODE:


#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 50using namespace std;int cnt;int a[MAX][MAX];inline int Gauss(){int re = 0,now = 1;for(int j,i = 1; i <= cnt; ++i) {for(j = now; j <= cnt && !a[j][i]; ++j);if(j > cnt) {++re;continue;}for(int k = 1; k <= cnt + 1; ++k)swap(a[now][k],a[j][k]);for(j = now + 1; j <= cnt; ++j)if(a[j][i])for(int k = i; k <= cnt + 1; ++k)a[j][k] ^= a[now][k];++now;}for(int i = now; i <= cnt; ++i)if(a[i][cnt + 1])return -1;return 1 << re;}int main(){int T;for(cin >> T; T--;) {memset(a,0,sizeof(a));scanf("%d",&cnt);for(int i = 1; i <= cnt; ++i)scanf("%d",&a[i][cnt + 1]);for(int x,i = 1; i <= cnt; ++i) {scanf("%d",&x);a[i][cnt + 1] ^= x;}int x,y;while(scanf("%d%d",&x,&y),x + y)a[y][x] = 1;for(int i = 1; i <= cnt; ++i)a[i][i] = 1;int ans = Gauss();if(ans == -1)puts("Oh,it's impossible~!!");elseprintf("%d\n",ans);}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.