TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

Given a permutation of n elements (1, 2, ..., n): A = (a1, a2, . . . , an). We define a sequence
P (A) = (p1, p2, . . . , pn−1) where pi = 0 if ai > ai+1 and pi = 1 if ai < ai+1. Given a permutation
B, find the number of all permutations C where P (C) = P (B) including the permutation B
itself.

Input Format

The input text file contains several tests, each on a separate line. The first number in the test
is n (0 < n ≤ 1024) followed by n numbers representing the permutation all of them separated
by a single space. The last line in the input contains only 0 and should not be processed.

Output Format

The output should be written on the standard output. For each line in the input (excluding the
last one, 0) you should write the result i.e. the number of the permutations having the same
value for P (A) when given the permutation A.
You may assume the result is less than 232.

Sample Input 1

2 1 2
4 1 3 2 4
0

Sample Output 1

1
5

Hints

Problem Source

Migrated from old NTUJ.

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 10000 65536 200