मुख्य कंटेंट तक स्किप करें

farmpiggie and Subset Sum

Madhav
EditReport

Description:

For a permutation pp of even length, you can do the following process:

Initialize a counter c=0c = 0. For each ii from 11 to nn, either add ipii \cdot p_i to cc, subtract ipii \cdot p_i from cc, or do nothing. Let the final value of the counter be cfinalc_{\text{final}}. Formally, for each i{1,,n}i \in \{1, \dots, n\}, consider the set Si={ipi,0,ipi}S_i = \{-i \cdot p_i, 0, i \cdot p_i\} and choose some xiSix_i \in S_i. Set cfinal=i=1nxic_{\text{final}} = \sum_{i=1}^n x_i.

You are given a single even integer nn. Find any permutation of length nn so that regardless of the operations chosen, the final value cfinalc_{\text{final}} will not be 1.

Input Each test contains multiple test cases. The first line contains the number of test cases tt (1t251 \le t \le 25). The description of the test cases follows.

The first and only line of each test case contains a single even integer nn (2n502 \le n \le 50) — the length of the desired permutation.

Output For each test case, output nn integers p1,,pnp_1, \dots, p_n (1pin1 \le p_i \le n) — a permutation satisfying the conditions.

If there are multiple solutions, print any of them.


Video Explanation:


Approaches:

1. Parity Observation (Optimal)

To ensure that the final sum cfinalc_{\text{final}} can never be exactly 1, we can leverage parity. The number 1 is an odd number. If we can force every possible term xix_i to be an even number, then any sum or difference of these terms will always result in an even number. Since an even number can never equal 1, the condition will be perfectly satisfied.

The term we are adding or subtracting is ipii \cdot p_i. For a product to be even, at least one of its factors (ii or pip_i) must be even.

  • If the index ii is odd, we must make pip_i even.
  • If the index ii is even, pip_i can be anything (but to make it a valid permutation, we will just use the odd numbers here).

Because nn is always guaranteed to be an even number, there are exactly n/2n/2 even indices and n/2n/2 odd indices. We can just swap adjacent elements! By making p=[2,1,4,3,6,5,,n,n1]p = [2, 1, 4, 3, 6, 5, \dots, n, n-1], we guarantee:

  • Odd indices (1,3,51, 3, 5\dots) get even values (2,4,62, 4, 6\dots). Product is even.
  • Even indices (2,4,62, 4, 6\dots) get odd values (1,3,51, 3, 5\dots). Product is even.

Complexity

  • Time Complexity: O(N)O(N) per testcase, where NN is the length of the permutation. We simply loop from 11 to NN and print the pairs.
  • Space Complexity: O(1)O(1) auxiliary space.

Solutions:

C++

#include <iostream>

using namespace std;

void solve() {
int n;
cin >> n;

// Swap adjacent elements: 2 1 4 3 6 5 ...
for (int i = 1; i <= n; i += 2) {
cout << i + 1 << " " << i << " ";
}
cout << "\n";
}

int main() {
// Fast I/O
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}

Java

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
int t = scanner.nextInt();
while (t-- > 0) {
solve(scanner);
}
}
scanner.close();
}

private static void solve(Scanner scanner) {
int n = scanner.nextInt();

// Swap adjacent elements: 2 1 4 3 6 5 ...
for (int i = 1; i <= n; i += 2) {
System.out.print((i + 1) + " " + i + " ");
}
System.out.println();
}
}

Python

import sys

def solve():
input_data = sys.stdin.read().split()
if not input_data:
return

t = int(input_data[0])
idx = 1

for _ in range(t):
n = int(input_data[idx])
idx += 1

ans = []
# Swap adjacent elements: 2 1 4 3 6 5 ...
for i in range(1, n + 1, 2):
ans.append(i + 1)
ans.append(i)

print(*(ans))

if __name__ == "__main__":
solve()

JavaScript

const fs = require('fs');

function main() {
const input = fs.readFileSync(0, 'utf-8').trim().split(/\s+/);
if (input.length === 0 || input[0] === '') return;

let t = parseInt(input[0], 10);
let idx = 1;

for (let i = 0; i < t; i++) {
let n = parseInt(input[idx++], 10);

let ans = [];
// Swap adjacent elements: 2 1 4 3 6 5 ...
for (let j = 1; j <= n; j += 2) {
ans.push(j + 1);
ans.push(j);
}

console.log(ans.join(' '));
}
}

main();
Telemetry Integration

Completed working through this block? Sync progress to workspace.