📜  使用从1到n的K个数的最大XOR

📅  最后修改于: 2021-05-25 10:42:23             🧑  作者: Mango

给定正整数n和k。使用最多k个数字求1至n的最大异或。 1到n的Xor之和定义为1 ^ 2 ^ 3 ^…^ n。
例子 :

Input :  n = 4, k = 3
Output : 7
Explanation
Maximum possible xor sum is 1 ^ 2 ^ 4 = 7.

Input : n = 11, k = 1
Output : 11
Explanation
Maximum Possible xor sum is 11.

如果我们有k = 1,那么最大可能的xor和本身就是’n’。现在,对于k> 1,我们始终可以拥有一个数字,其所有位都被设置为“ n”中的最高有效位。

C++
// CPP program to find max xor sum
// of 1 to n using atmost k numbers
#include 
using namespace std;
 
// To return max xor sum of 1 to n
// using at most k numbers
int maxXorSum(int n, int k)
{
    // If k is 1 then maximum
    // possible sum is n
    if (k == 1)
        return n;
 
    // Finding number greater than
    // or equal to n with most significant
    // bit same as n. For example, if n is
    // 4, result is 7. If n is 5 or 6, result
    // is 7
    int res = 1;
    while (res <= n)
        res <<= 1;
 
    // Return res - 1 which denotes
    // a number with all bits set to 1
    return res - 1;
}
 
// Driver program
int main()
{
    int n = 4, k = 3;
    cout << maxXorSum(n, k);
    return 0;
}


Java
// Java program to find max xor sum
// of 1 to n using atmost k numbers
public class Main {
 
    // To return max xor sum of 1 to n
    // using at most k numbers
    static int maxXorSum(int n, int k)
    {
        // If k is 1 then maximum
        // possible sum is n
        if (k == 1)
            return n;
 
        // Finding number greater than
        // or equal to n with most significant
        // bit same as n. For example, if n is
        // 4, result is 7. If n is 5 or 6, result
        // is 7
        int res = 1;
        while (res <= n)
            res <<= 1;
 
        // Return res - 1 which denotes
        // a number with all bits set to 1
        return res - 1;
    }
 
    // Driver program to test maxXorSum()
    public static void main(String[] args)
    {
        int n = 4, k = 3;
        System.out.print(maxXorSum(n, k));
    }
}


Python
# Python3 code to find max xor sum
# of 1 to n using atmost k numbers
 
# To return max xor sum of 1 to n
# using at most k numbers
def maxXorSum( n , k ):
    # If k is 1 then maximum
    # possible sum is n
    if k == 1:
        return n
     
    # Finding number greater than
    # or equal to n with most significant
    # bit same as n. For example, if n is
    # 4, result is 7. If n is 5 or 6, result
    # is 7
    res = 1
    while res <= n:
        res <<= 1
     
    # Return res - 1 which denotes
    # a number with all bits set to 1
    return res - 1
 
# Driver code
n = 4
k = 3
print( maxXorSum(n, k) )
 
# This code is contributed by Abhishek Sharma44.


C#
// C# program to find max xor sum
// of 1 to n using atmost k numbers
using System;
 
public class main {
 
    // To return max xor sum of 1 to n
    // using at most k numbers
    static int maxXorSum(int n, int k)
    {
        // If k is 1 then maximum
        // possible sum is n
        if (k == 1)
            return n;
 
        // Finding number greater than
        // or equal to n with most significant
        // bit same as n. For example, if n is
        // 4, result is 7. If n is 5 or 6, result
        // is 7
        int res = 1;
        while (res <= n)
            res <<= 1;
 
        // Return res - 1 which denotes
        // a number with all bits set to 1
        return res - 1;
    }
 
    // Driver program
    public static void Main()
    {
        int n = 4, k = 3;
        Console.WriteLine(maxXorSum(n, k));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

7