Editorial for DMOPC '21 Contest 5 P4 - Permutations & Primes


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: zhouzixiang2004

Subtask 1

Note that it is not possible for there to be only one distinct difference since 1 is not prime. Thus we can conjecture that the smallest possible number of distinct differences is 2. Let's try to build a permutation that uses only the differences 2 and 3. One way of doing this is to repeat the pattern 1,3,5,2,4,6,8,10,7,9, (repeating the differences 2,2,3,2,2 periodically) which works whenever N is a multiple of 5.

Time Complexity: O(N) for each test case.

Subtask 2

From here on, we will use 0-indexing for all indices and values of pi as that will turn out to be more convenient.

Again, we can conjecture that the smallest possible number of distinct differences is 2 for all N that is not too small.

Let's find two distinct prime numbers p and q with p+q=N. By Goldbach's conjecture, we expect that such p and q will exist whenever N is even and not too small. Goldbach's conjecture is unproven and doesn't guarantee that the two primes are distinct, but it is possible to verify using a computer that such pq exists for all even N between 8 and 106. Cases where N is small (N=2,4,6) may need to be handled separately, and we can find an answer for these small N by hand and hardcode them.

Now consider setting pi=(ipmodN) for all 0iN1. We have that pi+1pi+p(modN), so either pi+1=pi+p (if pi+p<N) or pi+1=pi+pN=piq (if pi+pN) for all 0iN2. Hence this sequence only has two distinct adjacent differences (p and q) as needed. Furthermore, this is a permutation since gcd(p,N)=gcd(p,p+q)=gcd(p,q)=1 (because p and q are distinct primes), and this means that if pi=pj, then pipj(ij)p0(modN)ij0(modN)i=j.

If we implement this solution by sieving all the primes up to N, then the time complexity is O(NloglogN). It is also possible to obtain O(N) behaviour by trying p in increasing order and checking if p and q=Np are both prime (in O(N) time for each p), as we will intuitively find a good p and q way before checking O(N) numbers.

Time Complexity: O(NloglogN) (or O(N) behaviour) for each test case.

Subtask 3

First, we handle the small N separately, which now includes all N between 1 and 7.

If N is even, we can do the construction as in subtask 2. Note that the above construction in fact gives a cyclic sequence with only p and q as adjacent differences (p0pN1=p or q as well). Thus if N is odd, we can perform the above construction for N1 to obtain a permutation p0,p1,,pN2 of 0,1,,N2, cyclically shift it so that pN2=Np1, and set pN1=N1 (in fact, if we use the exact same formula as shown above, we do not even need to do a cyclic shift as pN2(N2)ppq(modN1)pN2=q=Np1).

Time Complexity: O(NloglogN) (or O(N) behaviour) for each test case.

Remark: The properties of the prime numbers this problem uses are:

  • Goldbach's conjecture, allowing us to find primes p and q with p+q=N or p+q=N1.
  • Coprimeness of distinct primes, allowing the sequence ipmodN to "cover" all residues modulo N.

Comments

There are no comments at the moment.