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

#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;
}

  

HDU3364 Lanterns(求矩阵的秩)的更多相关文章

  1. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

  2. POJ 开关问题 1830【高斯消元求矩阵的秩】

    Language: Default 开关问题 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6656   Accepted: ...

  3. paper 127:机器学习中的范数规则化之(二)核范数与规则项参数选择

    机器学习中的范数规则化之(二)核范数与规则项参数选择 zouxy09@qq.com http://blog.csdn.net/zouxy09 上一篇博文,我们聊到了L0,L1和L2范数,这篇我们絮叨絮 ...

  4. Breeze库API总结(Spark线性代数库)(转载)

    导入 import breeze.linalg._ import breeze.numerics._ Spark Mllib底层的向量.矩阵运算使用了Breeze库,Breeze库提供了Vector/ ...

  5. 给深度学习入门者的Python快速教程 - numpy和Matplotlib篇

    始终无法有效把word排版好的粘贴过来,排版更佳版本请见知乎文章: https://zhuanlan.zhihu.com/p/24309547 实在搞不定博客园的排版,排版更佳的版本在: 给深度学习入 ...

  6. UVA11542 Square(高斯消元 异或方程组)

    建立方程组消元,结果为2 ^(自由变元的个数) - 1 采用高斯消元求矩阵的秩 方法一: #include<cstdio> #include<iostream> #includ ...

  7. L0、L1与L2范数、核范数(转)

    L0.L1与L2范数.核范数 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化.我们先简单的来理解下常用的L0.L1.L2和核范数规则化.最后聊下规则化项参数的选择问题.这里因为篇幅比较庞大 ...

  8. Matlab - 线性方程组求解

    常用函数:det 计算矩阵的行列式的值inv 求矩阵的逆阵rank 求矩阵的秩[V D]=eig(A) 求矩阵A的特征值和特征向量poly 求矩阵的特征多项式rref 用初等变换将矩阵化成行阶梯形nu ...

  9. 高斯消元 & 线性基【学习笔记】

    高斯消元 & 线性基 本来说不写了,但还是写点吧 [update 2017-02-18]现在发现真的有好多需要思考的地方,网上很多代码感觉都是错误的,虽然题目通过了 [update 2017- ...

随机推荐

  1. hibernate query.list() 返回的数据类型

    在hibernate中,用hql语句查询实体类,采用list方法的返回结果为一个List,该List中封装的对象分为以下三种情况: 1.查询全部字段的情况下,如"from 实体类" ...

  2. python self introspection

    http://www.ibm.com/developerworks/cn/linux/l-pyint/index1.html

  3. centos 6.5 git 服务器的配置(入门级)

    参考:https://www.digitalocean.com/community/tutorials/how-to-set-up-a-private-git-server-on-a-vps http ...

  4. hosts 本机DNS域名解析

    一. Hosts文件的位置很多用户都知道在Window系统中有个Hosts文件(没有后缀名),在Windows 98系统下该文件在Windows文件夹.在Windows 2000/XP系统中位于\%S ...

  5. jq获取元素

    <tr><td><div id="add"></div></td></tr>$("#add&quo ...

  6. PDF.js

    http://www.linuxidc.com/Linux/2015-06/118728.htm http://blog.csdn.net/xiangcns/article/details/42089 ...

  7. centos vim配置高亮语法和格式化粘贴

    centos vim配置高亮语法和格式化粘贴 设置vim别名和高亮grep词语 echo -e "\nalias vi=vim\nalias grep='grep --color'\n&qu ...

  8. ios nsdataformatter奇怪的问题

    用nsdataformatter在中文格式下测试, a 标识的是上午,下午,不是AM,pm. 我在24小时格式的机器上测试,存入了一个 时间,却解析不出来了! static NSString *Hom ...

  9. Django~automated tests

    def xx(): 冒号下一行要缩进 ATD http://blog.csdn.net/doupei2006/article/details/7657547 http://www.jb51.net/a ...

  10. css 命名规范

    网站头部:    head/header(头部) top(顶部)    导航:   nanv 导航具体区分:topnav(顶部导航).mainnav(主导航).mininav(迷你导航).textna ...