Sorting

Time limit0.3sMemory limit64 MB

Summary
Given a permutation, find all gap sizes X for which repeatedly scanning and swapping positions i and i+X until a pass makes no swap leaves the array sorted.
Level

Medium7 of 10

Topics
Sorting, Array, Math, Implementation
Solved
No attempts yet

Problem

Little P has just learned the shell sort algorithm. He wrote code that is meant to sort an array of NN integers into ascending order. Let AA be the array to be sorted.

gap = X;
do
{  ok = 1;
   for (i = 1; i<= N - gap; i++)
      if (A[i] > A[i+gap])
        {  temp = A[i];
           A[i] = A[i+gap];
           A[i+gap] = temp;
           ok = 0;
        }
   if (gap/2 > 1) gap=gap/2; else gap=1;
}  while (ok == 0);

Here i, N, X, gap, temp, and ok are integers (the int type in C/C++).

While typing the code, Little P forgot to copy line 11 (the line if (gap/2 > 1) gap=gap/2; else gap=1;). Because that line is missing, gap is never reduced: it keeps its initial value X for the whole run, so the loop just repeats the same fixed-gap pass over and over until an entire pass makes no swaps.

You are given the array AA. It has NN distinct elements, each between 11 and NN.

Find every value of XX for which this algorithm (with line 11 missing) still sorts AA correctly. We call such values of XX valid.

Input

The first line contains one integer NN.

The second line contains NN integers separated by single spaces, describing the array AA.

Output

On the first line, print the number of valid values of XX.

On the second line, print all valid values of XX in ascending order, separated by single spaces.

Constraints

  • 1<N<5000001 < N < 500000
  • 1≤X≤N−11 \le X \le N-1
  • AA is a permutation of 1,2,…,N1, 2, \dots, N (all elements are distinct).

Hint

For example, take N=6N = 6 and A=[4,2,6,1,5,3]A = [4, 2, 6, 1, 5, 3]. The valid values of XX are:

  • X=1X = 1: swaps happen at the position pairs (1,2),(3,4),(4,5),(5,6),(2,3),(4,5),(1,2),(3,4)(1,2), (3,4), (4,5), (5,6), (2,3), (4,5), (1,2), (3,4).
  • X=3X = 3: swaps happen at the position pairs (1,4),(3,6)(1,4), (3,6).

Examples1

  1. Example 1

    Input
    6
    4 2 6 1 5 3
    
    Expected output
    2
    1 3