C - pushpush

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 300

問題文

長さ n の数列 a_1, ... , a_n が与えられます。 空の数列 b に対して、以下の操作を n 回行うことを考えます。

i 回目には

  1. 数列の i 番目の要素 a_ib の末尾に追加する。
  2. b を逆向きに並び替える。

この操作をしてできる数列 b を求めて下さい。

制約

  • 1 \leq n \leq 2\times 10^5
  • 0 \leq a_i \leq 10^9
  • n,a_i は整数である。

入力

入力は以下の形式で標準入力から与えられる。

n
a_1 a_2 ... a_n

出力

n 個の整数を空白区切りで 1 行に出力せよ。 i 番目には、 b_i を出力せよ。


入力例 1

4
1 2 3 4

出力例 1

4 2 1 3

1 回目の操作 1 の後、 b1 となります。
1 回目の操作 2 の後、 b1 となります。
2 回目の操作 1 の後、 b1, 2 となります。
2 回目の操作 2 の後、 b2, 1 となります。
3 回目の操作 1 の後、 b2, 1, 3 となります。
3 回目の操作 2 の後、 b3, 1, 2 となります。
4 回目の操作 1 の後、 b3, 1, 2, 4 となります。
4 回目の操作 2 の後、 b4, 2, 1, 3 となります。 よって、答えは 4 2 1 3 です。


入力例 2

3
1 2 3

出力例 2

3 1 2

出力例 1 の説明の通り、 3 回目の操作 2 の後、 b3, 1, 2 となるので、 答えは 3 1 2 です。


入力例 3

1
1000000000

出力例 3

1000000000

入力例 4

6
0 6 7 6 7 0

出力例 4

0 6 6 0 7 7

Score : 300 points

Problem Statement

You are given an integer sequence of length n, a_1, ..., a_n. Let us consider performing the following n operations on an empty sequence b.

The i-th operation is as follows:

  1. Append a_i to the end of b.
  2. Reverse the order of the elements in b.

Find the sequence b obtained after these n operations.

Constraints

  • 1 \leq n \leq 2\times 10^5
  • 0 \leq a_i \leq 10^9
  • n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print n integers in a line with spaces in between. The i-th integer should be b_i.


Sample Input 1

4
1 2 3 4

Sample Output 1

4 2 1 3
  • After step 1 of the first operation, b becomes: 1.
  • After step 2 of the first operation, b becomes: 1.
  • After step 1 of the second operation, b becomes: 1, 2.
  • After step 2 of the second operation, b becomes: 2, 1.
  • After step 1 of the third operation, b becomes: 2, 1, 3.
  • After step 2 of the third operation, b becomes: 3, 1, 2.
  • After step 1 of the fourth operation, b becomes: 3, 1, 2, 4.
  • After step 2 of the fourth operation, b becomes: 4, 2, 1, 3.

Thus, the answer is 4 2 1 3.


Sample Input 2

3
1 2 3

Sample Output 2

3 1 2

As shown above in Sample Output 1, b becomes 3, 1, 2 after step 2 of the third operation. Thus, the answer is 3 1 2.


Sample Input 3

1
1000000000

Sample Output 3

1000000000

Sample Input 4

6
0 6 7 6 7 0

Sample Output 4

0 6 6 0 7 7
D - 11

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 600

問題文

1,...,nn 個の整数からなる長さ n+1 の数列 a_1,a_2,...,a_{n+1} が与えられます。 この数列には 1,...,n のどの整数もかならず 1 回以上出現することが分かっています。

k=1,...,n+1 のそれぞれについて、与えられた数列の長さ k の(連続とは限らない)部分列の個数を求め、 10^9+7 で割ったあまりを出力して下さい。

注意

  • 2 つの部分列が数列として同じであれば、元の数列での位置が異なっていたとしても、1 通りと数えます。

  • 数列 a の長さ k の部分列とは、a の要素のうち k 個を選んで、 それらを順番を変えずに取り出して並べた数列のことを指します。 例えば、数列 1,2,3,4,5 の長さ 3 の部分列には、 1,3,51,2,3 などがあります。 一方で、3,1,21,10,100 はこの数列の部分列ではありません。

制約

  • 1 \leq n \leq 10^5
  • 1 \leq a_i \leq n
  • 1,...,n のどの整数も必ず数列に出現する。
  • n,a_i は整数である。

入力

入力は以下の形式で標準入力から与えられる。

n
a_1 a_2 ... a_{n+1}

出力

答えを n+1 行に出力せよ。 k 行目には、長さ k の部分列の個数を 10^9+7 で割ったあまりを出力せよ。


入力例 1

3
1 2 1 3

出力例 1

3
5
4
1

長さ 1 の部分列は 1233 通りです。

長さ 2 の部分列は 1,11,21,32,12,35 通りです。

長さ 3 の部分列は 1,1,31,2,11,2,32,1,34 通りです。

長さ 4 の部分列は 1,2,1,31 通りです。


入力例 2

1
1 1

出力例 2

1
1

長さ 1 の部分列は 11 通りです。 長さ 2 の部分列は 1,11 通りです。


入力例 3

32
29 19 7 10 26 32 27 4 11 20 2 8 16 23 5 14 6 12 17 22 18 30 28 24 15 1 25 3 13 21 19 31 9

出力例 3

32
525
5453
40919
237336
1107568
4272048
13884156
38567100
92561040
193536720
354817320
573166440
818809200
37158313
166803103
166803103
37158313
818809200
573166440
354817320
193536720
92561040
38567100
13884156
4272048
1107568
237336
40920
5456
528
33
1

10^9+7 で割ったあまりを出力することに注意して下さい。

Score : 600 points

Problem Statement

You are given an integer sequence of length n+1, a_1,a_2,...,a_{n+1}, which consists of the n integers 1,...,n. It is known that each of the n integers 1,...,n appears at least once in this sequence.

For each integer k=1,...,n+1, find the number of the different subsequences (not necessarily contiguous) of the given sequence with length k, modulo 10^9+7.

Notes

  • If the contents of two subsequences are the same, they are not separately counted even if they originate from different positions in the original sequence.

  • A subsequence of a sequence a with length k is a sequence obtained by selecting k of the elements of a and arranging them without changing their relative order. For example, the sequences 1,3,5 and 1,2,3 are subsequences of 1,2,3,4,5, while 3,1,2 and 1,10,100 are not.

Constraints

  • 1 \leq n \leq 10^5
  • 1 \leq a_i \leq n
  • Each of the integers 1,...,n appears in the sequence.
  • n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_{n+1}

Output

Print n+1 lines. The k-th line should contain the number of the different subsequences of the given sequence with length k, modulo 10^9+7.


Sample Input 1

3
1 2 1 3

Sample Output 1

3
5
4
1

There are three subsequences with length 1: 1 and 2 and 3.

There are five subsequences with length 2: 1,1 and 1,2 and 1,3 and 2,1 and 2,3.

There are four subsequences with length 3: 1,1,3 and 1,2,1 and 1,2,3 and 2,1,3.

There is one subsequence with length 4: 1,2,1,3.


Sample Input 2

1
1 1

Sample Output 2

1
1

There is one subsequence with length 1: 1.

There is one subsequence with length 2: 1,1.


Sample Input 3

32
29 19 7 10 26 32 27 4 11 20 2 8 16 23 5 14 6 12 17 22 18 30 28 24 15 1 25 3 13 21 19 31 9

Sample Output 3

32
525
5453
40919
237336
1107568
4272048
13884156
38567100
92561040
193536720
354817320
573166440
818809200
37158313
166803103
166803103
37158313
818809200
573166440
354817320
193536720
92561040
38567100
13884156
4272048
1107568
237336
40920
5456
528
33
1

Be sure to print the numbers modulo 10^9+7.

E - guruguru

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 700

問題文

snuke 君は明るさを m 段階に切り替えられる照明を買いに来ました。 この照明の 明るさは 1 以上 m 以下の整数で表され、 リモコンに付いた 2 種類のボタンで明るさを切り替えます。

1 つめのボタンは「順送り」ボタンで、 明るさを 1 増やすことができます。ただし、ボタンを押す前の明るさが最大の m である場合には、 明るさは 1 になります。

2 つめのボタンは「お気に入り」ボタンで、 購入時に決めたお気に入りの明るさ x に切り替えることが出来ます。

snuke 君はお気に入りの明るさ x を、できるだけ効率的に明るさが切り替えられるように設定しようと考えました。 snuke 君は今後 n-1 回明るさを切り替える予定で、i 回目には明るさ a_i から 明るさ a_{i+1} に切り替えようと計画しています。 最初、明るさは a_1 です。 ボタンを押す回数の合計が最小になるようにお気に入りの明るさ x を決めた時の ボタンを押す回数を求めて下さい。

制約

  • 2 \leq n,m \leq 10^5
  • 1 \leq a_i\leq m
  • a_i \neq a_{i+1}
  • n,m,a_i は整数である。

入力

入力は以下の形式で標準入力から与えられる。

n m
a_1 a_2a_n

出力

ボタンを押す回数の合計の最小値を出力せよ。


入力例 1

4 6
1 5 1 4

出力例 1

5

お気に入りの明るさを 1,2,3,4,5,6 のそれぞれに設定したときのボタンを押す最小回数はそれぞれ 8,9,7,5,6,9 回です。 よって、お気に入りの明るさを 4 に設定したときにボタンを押す回数の合計を最小に出来ます。 お気に入りの明るさを 4 に設定したときの切り替え方は以下のとおりです。

  • 1 回目には、お気に入りボタンを 1 回押した後、順送りボタンを 1 回押します。
  • 2 回目には、順送りボタンを 2 回押します。
  • 3 回目には、お気に入りボタンを 1 回押します。

入力例 2

10 10
10 9 8 7 6 5 4 3 2 1

出力例 2

45

Score : 700 points

Problem Statement

Snuke is buying a lamp. The light of the lamp can be adjusted to m levels of brightness, represented by integers from 1 through m, by the two buttons on the remote control.

The first button is a "forward" button. When this button is pressed, the brightness level is increased by 1, except when the brightness level is m, in which case the brightness level becomes 1.

The second button is a "favorite" button. When this button is pressed, the brightness level becomes the favorite brightness level x, which is set when the lamp is purchased.

Snuke is thinking of setting the favorite brightness level x so that he can efficiently adjust the brightness. He is planning to change the brightness n-1 times. In the i-th change, the brightness level is changed from a_i to a_{i+1}. The initial brightness level is a_1. Find the number of times Snuke needs to press the buttons when x is set to minimize this number.

Constraints

  • 2 \leq n,m \leq 10^5
  • 1 \leq a_i\leq m
  • a_i \neq a_{i+1}
  • n, m and a_i are integers.

Input

Input is given from Standard Input in the following format:

n m
a_1 a_2a_n

Output

Print the minimum number of times Snuke needs to press the buttons.


Sample Input 1

4 6
1 5 1 4

Sample Output 1

5

When the favorite brightness level is set to 1, 2, 3, 4, 5 and 6, Snuke needs to press the buttons 8, 9, 7, 5, 6 and 9 times, respectively. Thus, Snuke should set the favorite brightness level to 4. In this case, the brightness is adjusted as follows:

  • In the first change, press the favorite button once, then press the forward button once.
  • In the second change, press the forward button twice.
  • In the third change, press the favorite button once.

Sample Input 2

10 10
10 9 8 7 6 5 4 3 2 1

Sample Output 2

45
F - SS

実行時間制限: 2 sec / メモリ制限: 256 MB

配点 : 1100

問題文

同じ文字列を 2 つ並べてできる文字列のことを偶文字列と呼ぶことにします。 例えば、 xyzxyzaaaaaa は偶文字列ですが、abababxyzxy は偶文字列ではありません。

空でない文字列 S に対して、f(S) を 「S の後ろに 1 文字以上の文字を追加してできる偶文字列のうち 最短の文字列」として定義します。 例えば、 f(abaaba)=abaababaab です。 このような文字列は空でない文字列に対してはちょうど 1 通りに定まることが証明できます。

アルファベットの小文字からなる偶文字列 S が与えられます。 f^{10^{100}} (S)l 文字目から r 文字目までに アルファベットの小文字がそれぞれ何回出現するかを求めて下さい。

f^{10^{100}} (S) というのは、 f(f(f( ... f(S) ... ))) のように、S10^{100}f で写した文字列のことを指します。

制約

  • 2 \leq |S| \leq 2\times 10^5
  • 1 \leq l \leq r \leq 10^{18}
  • S は小文字のアルファベットのみからなる偶文字列である。
  • l,r は整数である。

入力

入力は以下の形式で標準入力から与えられる。

S
l r

出力

26 個の整数を空白区切りで 1 行に出力せよ。 i 番目には、 f^{10^{100}}(S)l 文字目から r 文字目までに i 番目の アルファベットが何回出現するかを出力せよ。


入力例 1

abaaba
6 10

出力例 1

3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

f(abaaba)=abaababaab なので、f^{10^{100}}(S) の最初の 10 文字を取り出したものも やはり abaababaab となっています。よって、6 文字目から 10 文字目は abaab です。 abaab には a3 回、 b2 回使われていて、 c から z までは 1 回も出てこないので、 出力するべき値は 1 番目が 3 で、 2 番目が 2 で、それ以外の 24 個が 0 となります。


入力例 2

xx
1 1000000000000000000

出力例 2

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1000000000000000000 0 0

入力例 3

vgxgpuamkvgxgvgxgpuamkvgxg
1 1000000000000000000

出力例 3

87167725689669676 0 0 0 0 0 282080685775825810 0 0 0 87167725689669676 0 87167725689669676 0 0 87167725689669676 0 0 0 0 87167725689669676 141040342887912905 0 141040342887912905 0 0

Score : 1100 points

Problem Statement

We will call a string that can be obtained by concatenating two equal strings an even string. For example, xyzxyz and aaaaaa are even, while ababab and xyzxy are not.

For a non-empty string S, we will define f(S) as the shortest even string that can be obtained by appending one or more characters to the end of S. For example, f(abaaba)=abaababaab. It can be shown that f(S) is uniquely determined for a non-empty string S.

You are given an even string S consisting of lowercase English letters. For each letter in the lowercase English alphabet, find the number of its occurrences from the l-th character through the r-th character of f^{10^{100}} (S).

Here, f^{10^{100}} (S) is the string f(f(f( ... f(S) ... ))) obtained by applying f to S 10^{100} times.

Constraints

  • 2 \leq |S| \leq 2\times 10^5
  • 1 \leq l \leq r \leq 10^{18}
  • S is an even string consisting of lowercase English letters.
  • l and r are integers.

Input

Input is given from Standard Input in the following format:

S
l r

Output

Print 26 integers in a line with spaces in between. The i-th integer should be the number of the occurrences of the i-th letter in the lowercase English alphabet from the l-th character through the r-th character of f^{10^{100}} (S).


Sample Input 1

abaaba
6 10

Sample Output 1

3 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Since f(abaaba)=abaababaab, the first ten characters in f^{10^{100}}(S) is also abaababaab. Thus, the sixth through the tenth characters are abaab. In this string, a appears three times, b appears twice and no other letters appear, and thus the output should be 3 and 2 followed by twenty-four 0s.


Sample Input 2

xx
1 1000000000000000000

Sample Output 2

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1000000000000000000 0 0

Sample Input 3

vgxgpuamkvgxgvgxgpuamkvgxg
1 1000000000000000000

Sample Output 3

87167725689669676 0 0 0 0 0 282080685775825810 0 0 0 87167725689669676 0 87167725689669676 0 0 87167725689669676 0 0 0 0 87167725689669676 141040342887912905 0 141040342887912905 0 0