click below for solution

Saturday, 29 January 2022

Permutation Xor Sum solution|Codesheff lunchtime 2022 for div 3

For a permutation P of integers from 1 to N, let's define its value as (1P1)+(2P2)++(NPN).

Given N, find the maximum possible value of the permutation of integers from 1 to N.

As a reminder,  denotes the bitwise XOR operation

Input Format

The first line of the input contains a single integer T  the number of test cases. The description of test cases follows.

The only line of each test case contains a single integer N.

Output Format

For each test case, output the maximum possible value of the permutation of integers from 1 to N.

Constraints

  • 1T105
  • 1N109.

Subtasks

Subtask 1 (60 points): The sum of N over all test cases doesn't exceed 106Subtask 2 (40 points): No additional constraints.

Sample Input 1 

5
1
2
3
4
29012022

Sample Output 1 

0
6
6
20
841697449540506

Explanation

For N=1, the only such permutation is P=(1), its value is 11=0.

For N=2, the permutation with the best value is P=(2,1), with value 12+21=6.

For N=3, the permutation with the best value is P=(2,1,3), with value 12+21+33=6.

For N=4, the permutation with the best value is P=(2,1,4,3), with value 12+21+34+43=20.

Solution:

This question solution is updated within contest time. please click on follow button and subscribe our channel.if you follow and subscribe our channel then get notification of answer as soon as possible.


No comments:

Post a Comment