hdu3949 XOR (线性基求第k小的数)详解

Problem Description

XOR is a kind of bit operator, we define that as follow: for two binary base number A and B, let C=A XOR B, then for each bit of C, we can get its value by check the digit of corresponding position in A and B. And for each digit, 1 XOR 1 = 0, 1 XOR 0 = 1, 0 XOR 1 = 1, 0 XOR 0 = 0. And we simply write this operator as ^, like 3 ^ 1 = 2,4 ^ 3 = 7. XOR is an amazing operator and this is a question about XOR. We can choose several numbers and do XOR operatorion to them one by one, then we get another number. For example, if we choose 2,3 and 4, we can get 2^3^4=5. Now, you are given N numbers, and you can choose some of them(even a single number) to do XOR on them, and you can get many different numbers. Now I want you tell me which number is the K-th smallest number among them.

 

 

Input

First line of the input is a single integer T(T<=30), indicates there are T test cases.
For each test case, the first line is an integer N(1<=N<=10000), the number of numbers below. The second line contains N integers (each number is between 1 and 10^18). The third line is a number Q(1<=Q<=10000), the number of queries. The fourth line contains Q numbers(each number is between 1 and 10^18) K1,K2,......KQ.

 

 

Output

For each test case,first output Case #C: in a single line,C means the number of the test case which is from 1 to T. Then for each query, you should output a single line contains the Ki-th smallest number in them, if there are less than Ki different numbers, output -1.

 

这题是我刚开始学线性基时做的题,关于线性基,不懂的同学可以看这位大佬的博客(大学线代可有好好听的基本都能看懂)

https://blog.sengxian.com/algorithms/linear-basis

这题在这位大佬的博客里也有讲,不过我在他的代码的基础上加了详细的注释,有兴趣的同学可以看看

#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <stdio.h>
#include <map>

#include<ctype.h>
using namespace std;
typedef long long LL;

const int MAX_N = 100000 + 50, MAX_BASE = 60;

int n, fg0 = 0; // fg0标记可不可以异或出0

LL a[MAX_N], b[MAX_BASE + 3]; // a存原数组,b存对角矩阵

vector<LL> mymap; //将不为0的b[i]加进去

void prepare() {
    int cnt = 0;
    memset(b, 0, sizeof(b));
    for (int i = 0; i < n; ++i){
        for (int j = MAX_BASE; j >= 0; --j){
            if (a[i] >> j & 1) {
                if (b[j]){
                    a[i] ^= b[j];
                } else {
                    b[j] = a[i];
                    cnt++;
                    for (int k = j - 1; k >= 0; --k){
                        if (b[k] && ((b[j] >> k) & 1)){
                            b[j] ^= b[k];
                        }
                    }
                    for (int k = j + 1; k <= MAX_BASE; ++k){
                        if ((b[k] >> j) & 1){
                            b[k] ^= b[j];
                        }
                    }
                    break;
                }
            }
        }
    }
    if(cnt != n){ // 对角矩阵中1的个数小于n,说明存在线性相关组,所以可异或出0
        fg0 = 1;
    }

    mymap.clear();
    for (int i = 0; i <= MAX_BASE; ++i){
        if (b[i]){ //第i上不为0则放入数组
            mymap.push_back(b[i]);
        }
    }
}

LL Query(LL k){
    if(fg0){
        k--;
    }
    int len = mymap.size();
    if(k >= (1LL << len)){ //对角矩阵中有n个1,则该基可异或出2^n - 1个数,所以当k >= (2 ^ n - 1)时,结果不存在, 注意1一定要强转为LL,当初因为这个wa了好几发
        return -1;
    }  
    LL ans = 0;
    for(int i = 0; i < len; i++){ // 注意!!mymap中的数不一定是1,2,4,8这样连续的,可能是1,2,8,16这样(在对角矩阵中1的个数小于n的情况下)
        if((k >> i) & 1){
            ans ^= mymap[i];
        }
    }
    
    return ans;
}

int main() {
    int t;
    scanf("%d", &t);
    for(int m = 1; m <= t; m++){
        fg0= 0;
        scanf("%d", &n);
        for(int i = 0; i < n; i++){
            scanf("%lld", &a[i]);
        }

        prepare();
        int q;
        scanf("%d", &q);
        printf("Case #%d:\n", m);
        while(q--){
            LL k;
            scanf("%lld", &k);
            printf("%lld\n", Query(k));
        }
    }
    return 0;
}
    

 

 

 

 

 

 

 

 

 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于 HDU4992 所有原根的问题,我可以给出以下解答: 1. 首先,我们需要知道什么是原根。原根是指模 n 意义下,所有与 n 互质的数 a 的最小正整数 k 次幂对 n 取模的值覆盖了所有与 n 互质的数。简单来说,就是如果一个数 a 是模 n 意义下的原根,那么 a 的任何正整数次幂对 n 取模的值都不相同,且能覆盖所有与 n 互质的数。 2. 为了模 n 意义下的所有原根,我们需要先出与 n 互质的数的个数 phi(n)。phi(n) 可以使用欧拉函数出。 3. 然后,我们需要枚举模 n 意义下的所有数,判断它是否是原根。具体来说,对于每个 a,我们需要判断 a 的每个小于 phi(n) 的正整数次幂对 n 取模的值是否都不相同,且能覆盖所有与 n 互质的数。如果是,那么 a 就是模 n 意义下的原根。 4. 代码实现可以参考以下 Java 代码: ``` import java.util.*; public class Main { static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int phi(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res = res / i * (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res = res / n * (n - 1); } return res; } static int pow(int a, int b, int mod) { int res = 1; while (b > 0) { if ((b & 1) != 0) { res = res * a % mod; } a = a * a % mod; b >>= 1; } return res; } static boolean check(int a, int n, int phi) { for (int i = 1, j = pow(a, i, n); i <= phi; i++, j = j * a % n) { if (j == 1) { return false; } } return true; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int phi = phi(n); List<Integer> ans = new ArrayList<>(); for (int i = 1; i < n; i++) { if (gcd(i, n) == 1 && check(i, n, phi)) { ans.add(i); } } Collections.sort(ans); for (int x : ans) { System.out.print(x + " "); } System.out.println(); } } } ``` 其中,gcd 函数用于最大公约数,phi 函数用于欧拉函数,pow 函数用于快速幂模,check 函数用于判断一个数是否是原根。在主函数中,我们依次读入每个 n,出 phi(n),然后枚举模 n 意义下的所有数,判断它是否是原根,将所有原根存入一个 List 中,最后排序输出即可。 希望我的回答能够帮到你,如果你有任何问题,欢迎随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值