fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. // Function to calculate the value of k for a given permutation
  8. int calculateK(const vector<int>& p) {
  9. int k = 0;
  10. int n = p.size();
  11. for (int i = 0; i < n; ++i) {
  12. if ((i + 1) % 2 == 1) { // i+1 is odd
  13. k &= p[i];
  14. } else { // i+1 is even
  15. k |= p[i];
  16. }
  17. }
  18. return k;
  19. }
  20.  
  21. int main() {
  22. int t;
  23. cin >> t;
  24.  
  25. while (t--) {
  26. int n;
  27. cin >> n;
  28.  
  29. vector<int> perm(n);
  30. for (int i = 0; i < n; ++i) {
  31. perm[i] = i + 1;
  32. }
  33.  
  34. int maxK = 0;
  35. vector<int> bestPerm = perm;
  36.  
  37. // Try all permutations
  38. do {
  39. int currentK = calculateK(perm);
  40. if (currentK > maxK) {
  41. maxK = currentK;
  42. bestPerm = perm;
  43. }
  44. } while (next_permutation(perm.begin(), perm.end()));
  45.  
  46. // Output the maximum value of k
  47. cout << maxK << endl;
  48. // Output the permutation that gives the maximum k
  49. for (int i = 0; i < n; ++i) {
  50. cout << bestPerm[i] << " ";
  51. }
  52. cout << endl;
  53. }
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 5284KB
stdin
1
13
stdout
0 1 2 3 4 5 6 7 8 9 10 11 12 13 
1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101
12