eolymp
bolt
Try our new interface for solving problems
Problems

Gondola

Gondola

Time limit 1 second
Memory limit 128 MiB

Mao-Kong Gondola is a famous attraction in Taipei. The gondola system consists of a circular rail, a single station, and n gondolas numbered consecutively from 1 to n running around the rail in a fixed direction. After gondola i passes the station, the next gondola to pass the station will be gondola n + 1 if i < n, or gondola 1 if i = n.

Gondolas may break down. Luckily we have an infinite supply of spare gondolas, which are numbered (n + 1), (n + 2), and so on. When a gondola breaks down we replace it (in the same position on the track) with the first available spare gondola, that is, the one with the lowest number. For example, if there are five gondolas and gondola 1 breaks down, then we will replace it with gondola 6.

You like to stand at the station and watch the gondolas as they pass by. A gondola sequence is a sequence of n numbers of gondolas that pass the station. It is possible that one or more gondolas broke down (and were replaced) before you arrived, but none of the gondolas break down while you are watching.

Note that the same configuration of gondolas on the rail can give multiple gondola sequences, depending on which gondola passes first when you arrive at the station. For example, if none of the gondolas have broken down then both (2, 3, 4, 5, 1) and (4, 5, 1, 2, 3) are possible gondola sequences, but (4, 3, 2, 5, 1) is not (because the gondolas appear in the wrong order).

If gondola 1 breaks down, then we might now observe the gondola sequence (4, 5, 6, 2, 3). If gondola 4 breaks down next, we replace it with gondola 7 and we might observe the gondola sequence (6, 2, 3, 7, 5). If gondola 7 breaks down after this, we replace it with gondola 8 and we may now observe the gondola sequence (3, 8, 5, 6, 2).

prb7763_7.gif

A replacement sequence is a sequence consisting of the numbers of the gondolas that have broken down, in the order in which they break down. In the previous example the replacement sequence is (1, 4, 7). A replacement sequence r produces a gondola sequence g if, after gondolas break down according to the replacement sequence r, the gondola sequence g may be observed.

Gondola Sequence Checking

In the first three subtasks you must check whether an input sequence is a gondola sequence. See the table below for examples of sequences that are and are not gondola sequences. You need to implement a function valid(n, inputSeq), where

  • n is the length of the input sequence;

  • inputSeq is an array of length n, inputSeq[i] is element i of the input sequence, for 0in - 1;

  • the function should return 1 if the input sequence is a gondola sequence, or 0 otherwise.

Subtasks 1, 2, 3

prb7763_8.gif

Examples

The table below shows examples of sequences that are and are not gondola sequences.

prb7763_9.gif

Replacement Sequence

In the next three subtasks you must construct a possible replacement sequence that produces a given gondola sequence. Any such replacement sequence will be accepted. You need to implement a function replacement(n, gondolaSeq, replacementSeq), where

  • n is the length of the gondola sequence;

  • gondolaSeq is an array of length n. It is guaranteed that gondolaSeq is a gondola sequence, and gondolaSeq[i] is element i of the sequence, for 0in - 1;

  • the function should return l, the length of the replacement sequence;

  • replacementSeq is an array that is sufficiently large to store the replacement sequence; you should return your sequence by placing element i of your replacement sequence into replacementSeq[i] for 0il - 1.

Subtasks 4, 5, 6

prb7763_10.gif

Examples

prb7763_11.gif

Count Replacement Sequences

In the next four subtasks you must count the number of possible replacement sequences that produce a given sequence (which may or may not be a gondola sequence), modulo 10^9 + 9. You need to implement a function countReplacement(n, inputSeq).

  • n is the length of the input sequence;

  • inputSeq is an array of length n, inputSeq[i] is element i of the input sequence, for 0in - 1.

  • if the input sequence is a gondola sequence, then count the number of replacement sequences that produce this gondola sequence (which could be extremely large), and return this number modulo 10^9 + 9. If the input sequence is not a gondola sequence, the function should return 0. If the input sequence is a gondola sequence but no gondolas broke down, the function should return 1.

Subtasks 7, 8, 9, 10

prb7763_12.gif

Examples

prb7763_13.gif

Input data

First line contains the subtask number t (1t10) your program intends to solve;

Second line contains the length n of the input sequence;

If t equals to 4, 5 or 6, third line contains gondolaSeq[0], ..., gondolaSeq[n-1]. Otherwise it contains inputSeq[0], ..., inputSeq[n-1].

Output data

For each question a corresponding answer will be printed.

Examples

Input example #1
1
6
3 4 5 6 1 2
Output example #1
1
Input example #2
2
6
3 4 5 6 1 2
Output example #2
1
Input example #3
3
7
1 2 3 4 5 6 5
Output example #3
0
Input example #4
4
2
3 2
Output example #4
1 1
Input example #5
5
14
12 13 14 1 2 3 4 5 6 7 8 9 10 11
Output example #5
0 
Input example #6
6
50
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 51 47 48 49 50 1 2 3
Output example #6
1 46
Input example #7
7
11
4 5 6 7 8 9 10 11 1 2 3
Output example #7
1
Input example #8
8
14
6 94 8 9 10 93 12 13 95 1 2 3 4 5
Output example #8
224280120
Input example #9
9
4
1 2 7 6
Output example #9
2
Input example #10
10
4
4 7 4 7
Output example #10
0
Source 2014 IOI, Day 2