Poj 1830 高斯消元

來源:互聯網
上載者:User

標籤:des   style   blog   color   strong   os   

開關問題
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5418 Accepted: 2022

Description

有N個相同的開關,每個開關都與某些開關有著聯絡,每當你開啟或者關閉某個開關的時候,其他的與此開關相關聯的開關也會相應地發生變化,即這些相聯絡的開關的狀態如果原來為開就變為關,如果為關就變為開。你的目標是經過若干次開關操作後使得最後N個開關達到一個特定的狀態。對於任意一個開關,最多隻能進行一次開關操作。你的任務是,計算有多少種可以達到指定狀態的方法。(不計開關操作的順序)

Input

輸入第一行有一個數K,表示以下有K組測試資料。 
每組測試資料的格式如下: 
第一行 一個數N(0 < N < 29) 
第二行 N個0或者1的數,表示開始時N個開關狀態。 
第三行 N個0或者1的數,表示操作結束後N個開關的狀態。 
接下來 每行兩個數I J,表示如果操作第 I 個開關,第J個開關的狀態也會變化。每組資料以 0 0 結束。 

Output

如果有可行方法,輸出總數,否則輸出“Oh,it‘s impossible~!!” 不包括引號

Sample Input

230 0 01 1 11 21 32 12 33 13 20 030 0 01 0 11 22 10 0

Sample Output

4Oh,it‘s impossible~!!

Hint

第一組資料的說明: 
一共以下四種方法: 
操作開關1 
操作開關2 
操作開關3 
操作開關1、2、3 (不記順序)  
 1 /************************************************************************* 2     > File Name: Poj_1830.cpp 3     > Author: Stomach_ache 4     > Mail: [email protected] 5     > Created Time: 2014年07月10日 星期四 09時57分40秒 6     > Propose:  7  ************************************************************************/ 8  9 #include <cmath>10 #include <string>11 #include <cstdio>12 #include <vector>13 #include <fstream>14 #include <cstring>15 #include <iostream>16 #include <algorithm>17 using namespace std;18 19 const int MAX_N = 35;20 const double EPS = 1E-8;21 22 int A[MAX_N][MAX_N], S[MAX_N], E[MAX_N];23 24 int gauss_jordan(int n, int m) {25       int i, j; //i維護矩陣的秩26       for (i =0, j = 0; i < n && j < m; i++, j++) {27           int pivot = i;28         for (int k = i+1; k < n; k++) {29               if (abs(A[k][j] > abs(A[pivot][j]))) pivot = k;30         }31         if (abs(A[pivot][j]) < EPS) {32               i--;33             continue; 34         }// 放棄這一行, 直接處理下一行35         if (pivot != i) for (int k = 0; k <= m; k++) swap(A[i][k], A[pivot][k]);36         37         // 把正在處理的未知數係數變為138         for (int k = i+1; k <= m; k++) A[i][k] /= A[i][j];39         for (int r = 0; r < n; r++) if (i != r) {40               for (int k = j+1; k <= m; k++) 41                   // A[j][k] -= A[j][i] * A[i][k];42                   A[r][k] = (A[r][k] - A[r][j] * A[i][k] + 2) % 2;43         }44     }45     //無解46     for (int k = i; k < n; k++) if (A[k][m] != 0) return -1;47     if (i == n) return 1;48     //自由變元的個數為 n - i,每個變元有兩種狀態49     return 1<<(n-i);50 }51 52 int main(void) {53     int K, N;54       scanf("%d", &K);55     while (K--) {56           scanf("%d", &N);57         for (int i  = 0; i < N; i++) scanf("%d", S+i);58         for (int i  = 0; i < N; i++) scanf("%d", E+i);59         int i, j;60         memset(A, 0, sizeof(A));61         while (scanf("%d %d", &i, &j) && i+j) A[j-1][i-1] = 1;62         // 構造增廣矩陣63         for (int i = 0; i < N; i++) {64               A[i][N] = S[i] ^ E[i];65             A[i][i] = 1;66         }67         int ans = gauss_jordan(N, N);68         if (ans == -1) puts("Oh,it‘s impossible~!!");69         else printf("%d\n", ans);70     }71 72     return 0;73 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.