码迷,mamicode.com
首页 > 其他好文 > 详细

解题报告: hdu 3949 - 线性基

时间:2017-12-24 20:15:01      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:logs   解题报告   org   向量   row   fine   ace   end   报告   

#include <iostream>
#include <cstdio>
#define LL long long
using namespace std;
const int maxn = 10000;
LL a[maxn];
int n;
//返回row的值
int xorGauss()//可以用来解模二的方程,加快速度
{
    int row = 0;
    for (int i = 63; i >= 0; i--)
    {
        int j;
        for (j = row; j < n; j++)
            if(a[j]&((LL)1<<i))
                break;
        if (j != n)
        {
            swap(a[row], a[j]);
            for (j = 0; j < n; j++)
            {
                if(j == row) continue;
                if(a[j]&((LL)1<<i))
                    a[j] ^= a[row];
            }
            row++;
        }
    }
    return row;
}
LL findK(int row, int k)
{
    if(row < n)
    {
        if(k == 1)
            return 0;
        else
            k--;
    }
    if(k >= (LL)1<<row)
    {
        return -1;
    }
    LL ans = 0;
    //    把k二进制拆开来后,第i位就表示第i小的向量基取不取(1取,0不取)。
    for(int i = 0 ; i < 6; i++)  //  bu tai dong
    {
        if(k&(LL)1<<i)
        {
            ans ^= a[row-1-i];
        }
    }
    return ans;
}
void input()
{
    cin >> n;
    for(int i = 0 ; i < n ; i++)
        cin >> a[i];
}
void work()
{
    int row, q;
    LL k, ans;
    row = xorGauss();
    cin >> q;
    for(int i = 0 ; i < q; i++)
    {
        cin >>k;
        ans = findK(row,k);
        cout << ans << endl;
    }
}
int main()
{
    int T;
    cin >> T;
    for(int times = 0; times < T; times++)
    {
        printf("Case #%d:\n", times+1);
        input();
        work();
    }
    return 0;
}

解题报告: hdu 3949 - 线性基

标签:logs   解题报告   org   向量   row   fine   ace   end   报告   

原文地址:http://www.cnblogs.com/pprp/p/8098723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!