zoukankan      html  css  js  c++  java
  • Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值

    版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/u011686226/article/details/32337735

    题目来源:Light OJ 1272 Maximum Subset Sum

    题意:选出一些数 他们的抑或之后的值最大

    思路:每一个数为一个方程 高斯消元 从最高位求出上三角 消元前k个a[i]异或和都能有消元后的异或和组成

    消元前

    k

    a[i]

    a[i]异或和都能有消元后的

    异或和都能有消元后的

    p

    a[i]

    a[i]的异或

    的异或

    保证每一列仅仅有一个1 消元后全部A[i]抑或起来就是答案

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int maxn = 110;
    typedef long long LL;
    typedef LL Matrix[maxn];
    LL x[maxn];
    void gauss(Matrix A, int n)
    {
    	int i = 0, j = 63, k, u, r;
    	while(i < n && j >= 0)
    	{
    		int r = i;
    		for(k = i; k < n; k++)
    			if((A[k]>>j)&1)
    			{
    				r = k;
    				break;
    			}
    		if((A[r]>>j)&1)
    		{
    			if(r != i)
    				swap(A[i], A[r]);
    			for(u = 0; u < n; u++)
    			{
    				if(u != i && (A[u]>>j)&(A[i]>>j))
    					A[u] ^= A[i];
    			}
    			i++;
    		}
    		j--;
    	}
    }
    
    Matrix A;
    int main()
    {
    	int cas = 1;
    	int T;
    	scanf("%d", &T);
    	while(T--)
    	{
    		int n;
    		scanf("%d", &n);
    		for(int i = 0; i < n; i++)
    			scanf("%lld", &A[i]);
    		gauss(A, n);
    		LL ans = 0;
    		for(int i = 0; i < n; i++)
    			ans ^= A[i];
    		printf("Case %d: %lld
    ", cas++, ans);
    	}
    	return 0;
    }


查看全文
  • 相关阅读:
    Linq in
    wp7中应用程序清单(WMAppManifest.xml)详细说明
    wp7 给TextBox设置圆角边框
    js 中的闭包
    远程控制PPT软件的帮助
    wp7三种图标大小配置
    在英文版的sqlserver下用LIKE语句不能查询中文
    程序员版《那些年我们一起追过的女孩》(2)
    程序员版《那些年我们一起追过的女孩》(3)
    webbrowser 请求的资源在使用中。 (异常来自 HRESULT:0x800700AA)
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10539249.html
  • Copyright © 2011-2022 走看看