HDU 3364 Lanterns 高斯消元(水 异或方程

题目链接:点击打开链接

题意:

给定n盏灯,m个开关

下面m行给出每个开关可以控制哪些灯(即按下此开关,这些灯的状态会改变)

下面q个询问:一行一个询问,一个询问n个数字表示灯的最终状态

问从全暗到这个状态的方案数(一个开关只能按一次)

n条方程,等式右边就是输入的灯的状态。

m个未知数,表示每个开关是否按下,系数就是这个开关能否影响到那盏灯

解完方程后首先判断系数矩阵的秩是否和增广矩阵的秩相同,若相同则必有解,否则则无解。

因为求的是方案数,所以求出解完方程后的自由元数量即可。

方案数=2^(自由元数量)

import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.text.DecimalFormat;import java.util.ArrayDeque;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.Comparator;import java.util.Deque;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.Map;import java.util.PriorityQueue;import java.util.Scanner;import java.util.Stack;import java.util.StringTokenizer;import java.util.TreeMap;import java.util.TreeSet;import java.util.Queue;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;public class Main {int[][] A = new int[N][N], init = new int[N][N];int n, m;long Gauss(int[][] mat, int row, int col){int r, c, i, j;for(r = c = 0; r < row && c < col; r++, c++){for(i = r; i < row; i++)if(mat[i][c]>0)break;if(i == row){r–; continue;}if(i!=r)for(j = c; j <= col; j++){int tmp = mat[r][j]; mat[r][j] = mat[i][j]; mat[i][j] = tmp;}for(i = r+1; i < row; i++)if(mat[i][c]>0)for(j = c; j <= col; j++)mat[i][j] ^= mat[r][j];}for(i = r; i < row; i++)if(mat[i][col]!=0)return 0L;return 1L<<(col-r);}void work() throws Exception {int T = Int(), Cas = 1;while (T–>0){n = Int(); m = Int();for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)init[i][j] = 0;for(int i = 0, j, num; i < m; i++){num = Int();while(num–>0)init[Int()-1][i] = 1;}int q = Int();out.println("Case "+(Cas++)+":");while(q–>0){for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)A[i][j] = init[i][j];for(int i = 0; i < n; i++)A[i][m] = Int();out.println(Gauss(A, n, m));}}}public static void main(String[] args) throws Exception {Main wo = new Main();in = new BufferedReader(new InputStreamReader(System.in));out = new PrintWriter(System.out);// in = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));// out = new PrintWriter(new File("output.txt"));wo.work();out.close();}static int N = 56;static int M = N * 2;DecimalFormat df = new DecimalFormat("0.0000");static int inf = (int) 1e9;static long inf64 = (long) 1e18;static double eps = 1e-8;static double Pi = Math.PI;static int mod = (int) 1e9 + 7;private String Next() throws Exception {while (str == null || !str.hasMoreElements())str = new StringTokenizer(in.readLine());return str.nextToken();}private int Int() throws Exception {return Integer.parseInt(Next());}private long Long() throws Exception {return Long.parseLong(Next());}private double Double() throws Exception {return Double.parseDouble(Next());}StringTokenizer str;static Scanner cin = new Scanner(System.in);static BufferedReader in;static PrintWriter out;class Edge{int from, to, dis, nex;Edge(){} Edge(int from, int to, int dis, int nex){this.from = from; this.to = to; this.dis = dis; this.nex =nex; }} Edge[] edge = new Edge[M<<1]; int[] head = new int[N]; int edgenum; void init_edge(){ for(int i = 0; i < N; i++)head[i] = -1; edgenum = 0;} void add(int u, int v, int dis){ edge[edgenum] = new Edge(u, v, dis, head[u]); head[u] = edgenum++; } /* */int upper_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;int pos = r;r–;while (l <= r) {int mid = (l + r) >> 1;if (A[mid] <= val) {l = mid + 1;} else {pos = mid;r = mid – 1;}}return pos;}int lower_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;int pos = r;r–;while (l <= r) {int mid = (l + r) >> 1;if (A[mid] < val) {l = mid + 1;} else {pos = mid;r = mid – 1;}}return pos;}int Pow(int x, int y) {int ans = 1;while (y > 0) {if ((y & 1) > 0)ans *= x;y >>= 1;x = x * x;}return ans;}double Pow(double x, int y) {double ans = 1;while (y > 0) {if ((y & 1) > 0)ans *= x;y >>= 1;x = x * x;}return ans;}int Pow_Mod(int x, int y, int mod) {int ans = 1;while (y > 0) {if ((y & 1) > 0)ans *= x;ans %= mod;y >>= 1;x = x * x;x %= mod;}return ans;}long Pow(long x, long y) {long ans = 1;while (y > 0) {if ((y & 1) > 0)ans *= x;y >>= 1;x = x * x;}return ans;}long Pow_Mod(long x, long y, long mod) {long ans = 1;while (y > 0) {if ((y & 1) > 0)ans *= x;ans %= mod;y >>= 1;x = x * x;x %= mod;}return ans;}int gcd(int x, int y) {if (x > y) {int tmp = x;x = y;y = tmp;}while (x > 0) {y %= x;int tmp = x;x = y;y = tmp;}return y;}int max(int x, int y) {return x > y ? x : y;}int min(int x, int y) {return x < y ? x : y;}double max(double x, double y) {return x > y ? x : y;}double min(double x, double y) {return x < y ? x : y;}long max(long x, long y) {return x > y ? x : y;}long min(long x, long y) {return x < y ? x : y;}int abs(int x) {return x > 0 ? x : -x;}double abs(double x) {return x > 0 ? x : -x;}long abs(long x) {return x > 0 ? x : -x;}boolean zero(double x) {return abs(x) < eps;}double sin(double x) {return Math.sin(x);}double cos(double x) {return Math.cos(x);}double tan(double x) {return Math.tan(x);}double sqrt(double x) {return Math.sqrt(x);}double fabs(double x){return x>0?x:-x;}}

因为求的是方案数,所以求出解完方程后的自由元数量即可。

,人生就是要感受美丽的、善良的,丑恶的、病态的。

HDU 3364 Lanterns 高斯消元(水 异或方程

相关文章:

你感兴趣的文章:

标签云: