#P9573. Maximizing Resonance in Permutations

    ID: 22720 Type: Default 1000ms 256MiB

Maximizing Resonance in Permutations

Maximizing Resonance in Permutations

Given two positive integers \(p\) and \(n\), consider a permutation of the numbers \(1, 2, \dots, n\). We say that two adjacent numbers \(a\) and \(b\) in the permutation are in resonance if and only if \(a+b\) is a multiple of \(p\) (i.e. \(a+b\equiv0\pmod{p}\)).

Your task is to construct a permutation of \(1 \sim n\) which maximizes the number of resonant adjacent pairs. If there are multiple answers, output any one of them.

Observation: In a segment where every adjacent pair is resonant, if the segment has length \(L\) then it contributes \(L-1\) resonant pairs. Note that if a permutation is fully resonant (i.e. every adjacent pair is resonant), then for a permutation \(a_1, a_2, \dots, a_n\) we must have \(a_1+a_2\equiv0,\, a_2+a_3\equiv0,\, \dots,\, a_{n-1}+a_n\equiv0\pmod{p}\). This forces an alternation: if \(a_1\equiv r\) then \(a_2\equiv -r\), \(a_3\equiv r\) and so on. In many cases one can pick a complementary pair of residue classes \(r\) and \(-r\) (mod \(p\)) and use as many numbers from these two classes as possible (arranged in alternating order) so that all adjacent pairs in that block are resonant. Then, the remaining numbers can be arranged arbitrarily.

inputFormat

The input consists of a single line containing two positive integers \(p\) and \(n\) separated by spaces.

\(p\) represents the divisor used for checking resonance, and \(n\) is the number of elements in the permutation.

outputFormat

Output a permutation of \(1, 2, \dots, n\) (separated by spaces) that maximizes the number of resonant adjacent pairs (i.e. adjacent pairs \(a, b\) with \(a+b\equiv0\pmod{p}\)).

sample

3 4
1 2 4 3