http://poj.org/problem?id=1830

如果开关s1操作一次,则会有s1(记住自己也会变)、和s1连接的开关都会做一次操作。

那么设矩阵a[i][j]表示按下了开关j,开关i会被操作一次,记得a[i][i] = 1是必须的,因为开关i操作一次,本身肯定会变化一次。

所以有n个开关,就有n条方程,

每个开关的操作次数总和是:a[i][1] + a[i][2] + ... + a[i][n]

那么sum % 2就代表它的状态,需要和(en[i] - be[i] + 2) % 2相等。就是操作次数的奇偶性要相等。

那么来一个高斯消元,就知道是否有可能有解。

在有解的情况下,可能存在自由变量,什么意思呢?

就是假设有k个自由变量,那么就是前n - k个变量有固定的操作,使得能变成最终结果,那么这k个自由变量就可以任取值了,一共有2^k种不同的取值方案。因为方案不要求有顺序,所以那个灯取那个状态,都是相同的一种解。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e2 + ;
class GaussMatrix { //复杂度O(n3)
public:
int a[maxn][maxn];
int equ, val; //方程(行)个数,和变量(列)个数,其中第val个是b值,不能取
void init() {
for (int i = ; i <= equ; ++i) {
for (int j = ; j <= val; ++j) {
a[i][j] = ;
}
}
}
void swapRow(int rowOne, int rowTwo) {
for (int i = ; i <= val; ++i) {
swap(a[rowOne][i], a[rowTwo][i]);
}
}
void swapCol(int colOne, int colTwo) {
for (int i = ; i <= equ; ++i) {
swap(a[i][colOne], a[i][colTwo]);
}
}
// bool same(double x, double y) {
// return fabs(x - y) < eps;
// }
int guass() {
int k, col; // col,当前要处理的列, k当前处理的行
for (k = , col = ; k <= equ && col < val; ++k, ++col) { //col不能取到第val个
int maxRow = k; //选出列最大值所在的行,这样使得误差最小。(没懂)
for (int i = k + ; i <= equ; ++i) {
if (abs(a[i][col]) > abs(a[maxRow][col])) {
maxRow = i;
}
}
if (a[maxRow][col] == ) { //如果在第k行以后,整一列都是0
--k; //则这个变量就是一个自由变量。
continue;
}
if (maxRow != k) swapRow(k, maxRow); // k是当前的最大行了
for (int i = col + ; i <= val; ++i) { //整一列约去系数
a[k][i] /= a[k][col];
}
a[k][col] = ; //第一个就要变成1了,然后它下面和上面的变成0
for (int i = ; i <= equ; ++i) {
if (i == k) continue; //当前这行,不操作
for (int j = col + ; j <= val; ++j) {
a[i][j] = (a[i][j] - a[i][col] * a[k][j] + ) % ;
}
a[i][col] = ;
}
// debug();
}
for (int res = k; res <= equ; ++res) {
if (a[res][val] != ) return -; //方程无解
}
return val - k; //自由变量个数
}
void debug() {
for (int i = ; i <= equ; ++i) {
for (int j = ; j <= val; ++j) {
cout << a[i][j] << " ";
}
printf("\n");
}
printf("*******************************************\n\n");
}
}arr;
int be[maxn], en[maxn];
void work() {
int n;
cin >> n;
for (int i = ; i <= n; ++i) cin >> be[i];
for (int i = ; i <= n; ++i) cin >> en[i];
arr.init();
// memset(&arr, 0, sizeof arr);
arr.equ = n, arr.val = n + ;
do {
int x, y;
cin >> x >> y;
if (x == && y == ) break;
arr.a[x][x] = ;
arr.a[y][x] = ;
} while (true);
for (int i = ; i <= n; ++i) {
arr.a[i][n + ] = (en[i] - be[i] + ) % ;
arr.a[i][i] = ; //这个是必须的。
}
// arr.debug();
int res = arr.guass();
if (res == -) {
cout << "Oh,it's impossible~!!" << endl;
} else cout << ( << res) << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) work();
return ;
}

POJ 1830 开关问题 高斯消元,自由变量个数的更多相关文章

  1. POJ 1830 开关问题 (高斯消元)

    题目链接 题意:中文题,和上篇博客POJ 1222是一类题. 题解:如果有解,解的个数便是2^(自由变元个数),因为每个变元都有两种选择. 代码: #include <iostream> ...

  2. POJ 1830 开关问题 [高斯消元XOR]

    和上两题一样 Input 输入第一行有一个数K,表示以下有K组测试数据. 每组测试数据的格式如下: 第一行 一个数N(0 < N < 29) 第二行 N个0或者1的数,表示开始时N个开关状 ...

  3. POJ.1830.开关问题(高斯消元 异或方程组)

    题目链接 显然我们需要使每个i满足\[( ∑_{j} X[j]*A[i][j] ) mod\ 2 = B[i]\] 求这个方程自由元Xi的个数ans,那么方案数便是\(2^{ans}\) %2可以用^ ...

  4. POJ 3185 The Water Bowls 【一维开关问题 高斯消元】

    任意门:http://poj.org/problem?id=3185 The Water Bowls Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  5. POJ - 1681: Painter's Problem (开关问题-高斯消元)

    pro:开关问题,同上一题. 不过只要求输出最小的操作步数,无法完成输出“inf” sol:高斯消元的解对应的一组合法的最小操作步数. #include<bits/stdc++.h> #d ...

  6. POJ - 1222: EXTENDED LIGHTS OUT (开关问题-高斯消元)

    pro:给定5*6的灯的状态,如果我们按下一个灯的开关,它和周围4个都会改变状态.求一种合法状态,使得终状态全为关闭: sol:模2意义下的高斯消元. 终于自己手打了一个初级板子. #include& ...

  7. A - The Water Bowls POJ - 3185 (bfs||高斯消元)

    题目链接:https://vjudge.net/contest/276374#problem/A 题目大意:给你20个杯子,每一次操作,假设当前是对第i个位置进行操作,那么第i个位置,第i+1个位置, ...

  8. POJ 1166 The Clocks 高斯消元 + exgcd(纯属瞎搞)

    依据题意可构造出方程组.方程组的每一个方程格式均为:C1*x1 + C2*x2 + ...... + C9*x9 = sum + 4*ki; 高斯消元构造上三角矩阵,以最后一个一行为例: C*x9 = ...

  9. POJ 2065 SETI(高斯消元)

    题目链接:http://poj.org/problem?id=2065 题意:给出一个字符串S[1,n],字母a-z代表1到26,*代表0.我们用数组C[i]表示S[i]经过该变换得到的数字.给出一个 ...

随机推荐

  1. The import ....cannot be resolved 解决方法

    1:右击项目build path>configure build path>libraries看有没感叹号什么的不正常的lib,移除掉 2:点击项目的build path>confi ...

  2. hadoop集群异常问题总结

    1. Could not find or load main class java.library.path=.opt.hadoop.lib 我的环境上是 hadoopopts变量的配置问题,至于为啥 ...

  3. codeforces B. Multitasking 解题报告

    题目链接:http://codeforces.com/problemset/problem/384/B 题目意思:给出n个数组,每个数组包括m个数字,当k = 0 时,需要把n个数组都按照从小到大的顺 ...

  4. Peach+Fuzzer

    目录 1 Peach是什么....................................................................................... ...

  5. bootstrap 学习笔记(5)---- 图片和响应式工具

    (一)响应式图片: 在 Bootstrap 版本 3 中,通过为图片添加 .img-responsive 类可以让图片支持响应式布局.其实质是为图片设置了 max-width: 100%;. heig ...

  6. BZOJ_1296_[SCOI2009]粉刷匠_DP

    BZOJ_1296_[SCOI2009]粉刷匠_DP Description windy有 N 条木板需要被粉刷. 每条木板被分为 M 个格子. 每个格子要被刷成红色或蓝色. windy每次粉刷,只能 ...

  7. bzoj 1086 [SCOI2005]王室联邦——思路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1086 于是去看了题解. 要回溯的时候再把自己加进栈里判断.这样才能保证剩下的可以通过自己连到 ...

  8. Ubuntu18.04 安装 JDK7

    直接下载jdk压缩包方式安装 1.官网下载JDK      地址: http://www.oracle.com/technetwork/articles/javase/index-jsp-138363 ...

  9. Azure SQL Database (27) 创建Table Partition

    <Windows Azure Platform 系列文章目录> 昨天客户正好提到这个问题,现在记录一下. 我们在使用传统的SQL Server,会使用Table Partition,这个功 ...

  10. vijos1842(火柴排队)

    描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑i=1n(ai−bi)2∑i=1n(ai−bi) ...