首页 技术 正文
技术 2022年11月6日
0 收藏 755 点赞 994 浏览 1274 个字

思路:乍一看好像和线性代数没什么关系。我们用一个数组B表示第i个位置的灯变了没有,然后假设我用u[i] = 1表示动开关i,mp[i][j] = 1表示动了i之后j也会跟着动,那么第i个开关的最终状态为:u[1]*mp[1][i]^u[2]*mp[2][i]….^u[n]*mp[n][i](或者改为相加 % 2)。显然,前式等于B[i],所以,问题转化为了求u的解个数:MP*U = B。注意MP矩阵的写法。

关于矩阵:

r(A) = r(A,b)           有解

r(A) = r(A,b) = n     有唯一解     (n是未知量的个数,即A的列数)

r(A) = r(A,b) < n     有无穷多解

参考:开关问题 POJ – 1830 高斯消元

代码:

#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = + ;
const int seed = ;
const ll MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
using namespace std;
int A[maxn][maxn], B[maxn], n;
void Gauss(){
ll R = ;
int row = ;
for(int i = ; i <= n && row <= n; i++,row++){
int max_r = row;
for(int j = row + ; j <= n; j++){
if(A[j][i] > A[row][i]){
max_r = j;break;
}
}
if(max_r != row){
for(int k = i; k <= n + ; k++)
swap(A[max_r][k], A[row][k]);
}
if(A[row][i] == ){
row--;
continue;
}
R++;
for(int j = row + ; j <= n; j++){
if(A[j][i]){
for(int k = i; k <= n + ; k++)
A[j][k] = (A[j][k] - A[row][k] + ) % ;
}
}
}
for(int i = row; i <= n; i++){
if(A[i][n + ]){
printf("Oh,it's impossible~!!\n");
return;
}
}
R = n - R;
R = << R;
printf("%lld\n", R);
}
int main(){
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
memset(A, , sizeof(A));
for(int i = ; i <= n; i++)
scanf("%d", &B[i]);
for(int i = ; i <= n; i++){
int v;
scanf("%d", &v);
A[i][n + ] = B[i] ^ v;
A[i][i] = ;
}
int u, v;
while(scanf("%d%d", &u , &v) && u + v){
A[v][u] = ;
}
Gauss();
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298