求矩阵的秩,及判断有无解

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 60, INF = 0x3F3F3F3F;
const double eps = 1e-8;

template<typename T>
int gauss_jordan(T A[N][N], int n, int m){
    int i, c;
    for(i = 0, c = 0; i < n && c < m; i++, c++){
        int r = i;
        for(int j = i + 1; j < n; j++){
            if(A[j][c]){
                r = j;
                break;
            }
        }
        if(A[r][c] == 0){
            i--;
            continue;
        }
        if(r != i){
            for(int j = 0; j <= m; j++){
                swap(A[r][j], A[i][j]);
            }
        }
        for(int k = 0; k < n; k++){
            if(k != i && A[k][c]){
                for(int j = m; j >= c; j--){
                    A[k][j] ^= A[i][j];
                }
            }
        }
    }
    for(int j = i ; j < n; j++){
        if(A[j][m]){
            return -1;
        }
    }
    return i;
}

int a[N][N], b[N][N];
int main(){
    int t;
    cin>>t;
    for(int cas = 1; cas <= t; cas++){
        int n, m;
        scanf("%d %d", &n, &m);
        memset(b, 0, sizeof(b));
        for(int i = 0; i < m; i++){
            int cnt;
            scanf("%d", &cnt);
            while(cnt--){
                int v;
                scanf("%d", &v);
                v--;
                b[v][i] = 1;
            }
        }
        int q;
        scanf("%d", &q);
        printf("Case %d:\n", cas);
        while(q--){
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    a[i][j] = b[i][j];
                }
            }
            for(int i = 0; i < n; i++){
                scanf("%d", &a[i][m]);
            }
            int ans = gauss_jordan(a, n, m);
            if(ans == -1){
                printf("0\n");
            }else{
                printf("%I64d\n", 1ll << (m - ans));
            }

        }

    }

    return 0;
}

  

最新文章

  1. .NET开源进行时:消除误解、努力前行(本文首发于《程序员》2015第10A期的原始版本)
  2. 在.net中使用GAC
  3. jquery插件-表单验证插件-validator对象
  4. ulimit命令
  5. RuntimeWarning: invalid value encountered in divide
  6. 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856
  7. BZOJ-1968 COMMON 约数研究 数论+奇怪的姿势
  8. Oracle下的IF EXISTS()
  9. Where is the ActiveX Project Type for Delphi 10.1 Berlin
  10. psl/sql本地与远程连接配置
  11. 设计模式学习一:strategyPattern
  12. c# 委托详解
  13. Oracle Sql优化之Merge 改写优化Update
  14. 微信小程序(有始有终,全部代码)开发--- 新增模块: 图片选取以及拍照功能
  15. 为WebClient增加Cookie的支持
  16. [转]vs2010用 boost.python 编译c++类库 供python调用
  17. vue中输入框聚焦,自动跳转下一个输入框
  18. MySql 存储过程 光标只循环一次
  19. Spark的运行模式(2)--Yarn-Cluster和Yarn-Client
  20. JDBC PrepareStatement对象执行批量处理实例

热门文章

  1. Python自动化之线程进阶篇(二)
  2. Citrix运行检测出错
  3. ndk学习5: ndk中使用c++
  4. 1. Smalidea无源码调试android应用
  5. Docker与LXC的区别
  6. C#之Textbox实现自动提示容、自动补齐内容
  7. python装饰器初探
  8. C++基础知识面试精选100题系列(21-30)[C++ basics]
  9. Android Volley入门到精通:定制自己的Request
  10. 【leetcode】Reverse Nodes in k-Group (hard)☆