📌  相关文章
📜  元素各自位置在大小N的排列中的按位XOR的最大和

📅  最后修改于: 2021-04-17 16:36:10             🧑  作者: Mango

给定一个正整数N ,对于大小为N且具有[0,N – 1]范围内的元素的任何置换,任务是计算所有元素在其各自位置的按位XOR之和。

例子:

方法:解决此问题的想法是递归生成整数[0,N – 1]的所有可能排列,并计算每个整数的分数,然后在所形成的所有排列中找到最大分数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
// Function to calculate the score
int calcScr(vectorarr){
 
  // Stores the possible score for
  // the current permutation
  int ans = 0;
 
  // Traverse the permutation array
  for(int i = 0; i < arr.size(); i++)
    ans += (i ^ arr[i]);
 
  // Return the final score
  return ans;
}
 
// Function to generate all the possible
// permutation and get the max score
int getMax(vector arr, int ans, vector chosen, int N)
{
 
  // If arr[] length is equal to N
  // process the permutation
  if (arr.size() == N){
    ans = max(ans, calcScr(arr));
    return ans;
 
  }
 
  // Generating the permutations
  for (int i = 0; i < N; i++)
  {
 
    // If the current element is
    // chosen
    if(chosen[i])
      continue;
 
    // Mark the current element
    // as true
    chosen[i] = true;
    arr.push_back(i);
 
    // Recursively call for next
    // possible permutation
    ans = getMax(arr, ans, chosen, N);
 
    // Backtracking
    chosen[i] = false;
    arr.pop_back();
  }
 
  // Return the ans
  return ans;
}
 
// Driver Code
int main()
{
 
  int N = 2;
 
  // Stores the permutation
  vector arr;
 
  // To display the result
  int ans = -1;
  vectorchosen(N,false);
  ans = getMax(arr, ans, chosen, N);
 
  cout << ans << endl;
}
 
// This code is contributed by bgangwar59.


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to calculate the score
static int calcScr(ArrayListarr)
{
 
  // Stores the possible score for
  // the current permutation
  int ans = 0;
 
  // Traverse the permutation array
  for(int i = 0; i < arr.size(); i++)
    ans += (i ^ arr.get(i));
 
  // Return the final score
  return ans;
}
 
// Function to generate all the possible
// permutation and get the max score
static int getMax(ArrayList arr, int ans, ArrayList chosen, int N)
{
 
  // If arr[] length is equal to N
  // process the permutation
  if (arr.size() == N)
  {
    ans = Math.max(ans, calcScr(arr));
    return ans;
 
  }
 
  // Generating the permutations
  for (int i = 0; i < N; i++)
  {
 
    // If the current element is
    // chosen
    if(chosen.get(i))
      continue;
 
    // Mark the current element
    // as true
    chosen.set(i, true);
    arr.add(i);
 
    // Recursively call for next
    // possible permutation
    ans = getMax(arr, ans, chosen, N);
 
    // Backtracking
    chosen.set(i, false);
    arr.remove(arr.size()-1);
  }
 
  // Return the ans
  return ans;
}
 
// Driver Code
public static void main(String[] args)
{
 
  int N = 2;
 
  // Stores the permutation
  ArrayList arr = new ArrayList();
 
  // To display the result
  int ans = -1;
  ArrayList chosen = new ArrayList(Collections.nCopies(N, false));
  ans = getMax(arr, ans, chosen, N);
 
  System.out.print(ans +"\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
 
# Function to generate all the possible
# permutation and get the max score
def getMax(arr, ans, chosen, N):
 
    # If arr[] length is equal to N
    # process the permutation
    if len(arr) == N:
        ans = max(ans, calcScr(arr))
        return ans
 
    # Generating the permutations
    for i in range(N):
       
        # If the current element is
        # chosen
        if chosen[i]:
            continue
             
        # Mark the current element
        # as true
        chosen[i] = True
        arr.append(i)
         
        # Recursively call for next
        # possible permutation
        ans = getMax(arr, ans, chosen, N)
         
        # Backtracking
        chosen[i] = False
        arr.pop()
         
    # Return the ans
    return ans
 
# Function to calculate the score
def calcScr(arr):
     
    # Stores the possible score for
    # the current permutation
    ans = 0
     
    # Traverse the permutation array
    for i in range(len(arr)):
        ans += (i ^ arr[i])
         
    # Return the final score
    return ans
 
 
# Driver Code
N = 2
 
# Stores the permutation
arr = []
 
# To display the result
ans = -1
 
chosen = [False for i in range(N)]
 
ans = getMax(arr, ans, chosen, N)
 
print(ans)


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
 
  // Function to calculate the score
  static int calcScr(Listarr)
  {
 
    // Stores the possible score for
    // the current permutation
    int ans = 0;
 
    // Traverse the permutation array
    for(int i = 0; i < arr.Count; i++)
      ans += (i ^ arr[i]);
 
    // Return the readonly score
    return ans;
  }
 
  // Function to generate all the possible
  // permutation and get the max score
  static int getMax(List arr, int ans, List chosen, int N)
  {
 
    // If []arr length is equal to N
    // process the permutation
    if (arr.Count == N)
    {
      ans = Math.Max(ans, calcScr(arr));
      return ans;
 
    }
 
    // Generating the permutations
    for (int i = 0; i < N; i++)
    {
 
      // If the current element is
      // chosen
      if(chosen[i])
        continue;
 
      // Mark the current element
      // as true
      chosen[i] = true;
      arr.Add(i);
 
      // Recursively call for next
      // possible permutation
      ans = getMax(arr, ans, chosen, N);
 
      // Backtracking
      chosen[i] = false;
      arr.Remove(arr.Count-1);
    }
 
    // Return the ans
    return ans;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    int N = 2;
 
    // Stores the permutation
    List arr = new List();
 
    // To display the result
    int ans = -1;
    List chosen = new List(N);
    for(int i = 0; i < N; i++)
      chosen.Add(false);
    ans = getMax(arr, ans, chosen, N);
 
    Console.Write(ans +"\n");
  }
}
 
// This code is contributed by shikhasingrajput


输出:
2

时间复杂度: O(N * N!)
辅助空间: O(N)