A - Wanna go back home

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

配点 : 200

問題文

高橋君は無限に広い 2 次元平面上に住んでいて、N 日間の旅行をします。 高橋君の旅程は長さ N の文字列 S であり、はじめは家にいます。i(1 ≦ i ≦ N) 日目には、

  • Si 文字目が N なら北に
  • Si 文字目が W なら西に
  • Si 文字目が S なら南に
  • Si 文字目が E なら東に

正の距離だけ移動します。

高橋君は、各日の移動距離は決めていません。各日の移動距離をうまく決めることで、 高橋君が N 日間の旅程をすべて消化したときに家にいるようにできるかどうか判定してください。

制約

  • 1 ≦ | S | ≦ 1000
  • S は文字 N, W, S, E のみからなる。

入力

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

S

出力

高橋君が旅程をすべて消化したときに家にいるようにできる場合は Yes, そうでない場合は No を出力せよ。


入力例 1

SENW

出力例 1

Yes

毎日距離 1 ずつ進めばよいです。


入力例 2

NSNNSNSN

出力例 2

Yes

入力例 3

NNEW

出力例 3

No

入力例 4

W

出力例 4

No

Score : 200 points

Problem Statement

Snuke lives on an infinite two-dimensional plane. He is going on an N-day trip. At the beginning of Day 1, he is at home. His plan is described in a string S of length N. On Day i(1 ≦ i ≦ N), he will travel a positive distance in the following direction:

  • North if the i-th letter of S is N
  • West if the i-th letter of S is W
  • South if the i-th letter of S is S
  • East if the i-th letter of S is E

He has not decided each day's travel distance. Determine whether it is possible to set each day's travel distance so that he will be back at home at the end of Day N.

Constraints

  • 1 ≦ | S | ≦ 1000
  • S consists of the letters N, W, S, E.

Input

The input is given from Standard Input in the following format:

S

Output

Print Yes if it is possible to set each day's travel distance so that he will be back at home at the end of Day N. Otherwise, print No.


Sample Input 1

SENW

Sample Output 1

Yes

If Snuke travels a distance of 1 on each day, he will be back at home at the end of day 4.


Sample Input 2

NSNNSNSN

Sample Output 2

Yes

Sample Input 3

NNEW

Sample Output 3

No

Sample Input 4

W

Sample Output 4

No
B - Simplified mahjong

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

配点 : 400

問題文

高橋君は 1 から N までの整数のうちのどれかが書かれたカードをたくさん持っています。 高橋君は整数 i が書かれたカードを A_i 枚持っています。

2 枚のカードについて、それらに書かれた整数の差の絶対値が 1 以下のとき、これらをペアにすることができます。

高橋君は、同じカードが複数のペアに使われないように、できるだけ多くのペアを作りたいです。高橋君が作れるペアの個数の最大値を求めてください。

制約

  • 1 ≦ N ≦ 10^5
  • 0 ≦ A_i ≦ 10^9 (1 ≦ i ≦ N)
  • 入力はすべて整数である。

入力

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

N
A_1
:
A_N

出力

高橋君が作れるペアの個数の最大値を出力せよ。


入力例 1

4
4
0
3
2

出力例 1

4

一例として、(1,1),(1,1),(3,4),(3,4)4 つのペアをつくることができます。


入力例 2

8
2
0
1
6
0
8
2
1

出力例 2

9

Score : 400 points

Problem Statement

Snuke has a large collection of cards. Each card has an integer between 1 and N, inclusive, written on it. He has A_i cards with an integer i.

Two cards can form a pair if the absolute value of the difference of the integers written on them is at most 1.

Snuke wants to create the maximum number of pairs from his cards, on the condition that no card should be used in multiple pairs. Find the maximum number of pairs that he can create.

Constraints

  • 1 ≦ N ≦ 10^5
  • 0 ≦ A_i ≦ 10^9 (1 ≦ i ≦ N)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum number of pairs that Snuke can create.


Sample Input 1

4
4
0
3
2

Sample Output 1

4

For example, Snuke can create the following four pairs: (1,1),(1,1),(3,4),(3,4).


Sample Input 2

8
2
0
1
6
0
8
2
1

Sample Output 2

9
C - BBuBBBlesort!

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

配点 : 600

問題文

高橋君は、誕生日に長さ N の数列をもらいました。i(1 ≦ i ≦ N) 番目の要素は整数 A_i です。どの 2 つの要素も、互いに異なります。 高橋君はこの数列を単調増加になるように並べ替えたいです。 高橋君は超能力者なので、以下の 2 つの操作が任意のタイミングでできます。

  • 操作1: 数列の連続する 2 つの要素を選び、その 2 つの順番を反転する。
  • 操作2: 数列の連続する 3 つの要素を選び、その 3 つの順番を反転する。

高橋君は操作2は好きですが、操作1は嫌いです。この 2 種類の操作を使って数列を単調増加になるように並び替えるときの、操作1の最小回数を求めてください。

制約

  • 1 ≦ N ≦ 10^5
  • 0 ≦ A_i ≦ 10^9
  • i ≠ j ならば A_i ≠ A_j
  • 入力はすべて整数である。

入力

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

N
A_1
:
A_N

出力

操作1の最小回数を出力せよ。


入力例 1

4
2
4
3
1

出力例 1

1

以下の操作で、単調増加な数列にすることができます。

  • まず、後ろの 3 つの要素を反転する。数列は 2,1,3,4 となる。
  • 次に、前の 2 つの要素を反転する。数列は 1,2,3,4 となる。

この操作列において、連続する 2 つの要素を入れ替える操作の回数は 1 です。これより少ない回数で単調増加な数列は作れないので、1 を出力します。


入力例 2

5
10
8
5
3
2

出力例 2

0

Score : 600 points

Problem Statement

Snuke got an integer sequence of length N from his mother, as a birthday present. The i-th (1 ≦ i ≦ N) element of the sequence is a_i. The elements are pairwise distinct. He is sorting this sequence in increasing order. With supernatural power, he can perform the following two operations on the sequence in any order:

  • Operation 1: choose 2 consecutive elements, then reverse the order of those elements.
  • Operation 2: choose 3 consecutive elements, then reverse the order of those elements.

Snuke likes Operation 2, but not Operation 1. Find the minimum number of Operation 1 that he has to perform in order to sort the sequence in increasing order.

Constraints

  • 1 ≦ N ≦ 10^5
  • 0 ≦ A_i ≦ 10^9
  • If i ≠ j, then A_i ≠ A_j.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of times Operation 1 that Snuke has to perform.


Sample Input 1

4
2
4
3
1

Sample Output 1

1

The given sequence can be sorted as follows:

  • First, reverse the order of the last three elements. The sequence is now: 2,1,3,4.
  • Then, reverse the order of the first two elements. The sequence is now: 1,2,3,4.

In this sequence of operations, Operation 1 is performed once. It is not possible to sort the sequence with less number of Operation 1, thus the answer is 1.


Sample Input 2

5
10
8
5
3
2

Sample Output 2

0
D - Anticube

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

配点 : 1100

問題文

高橋君は誕生日にお母さんから正の整数 s_1,...,s_N をもらいました。ただし、要素の重複は許されます。 高橋君は、これらのN個の整数のうちのいくつかを丸で囲みます。

高橋君は立方数が嫌いなので、s_i,s_j(i ≠ j)の両方が丸で囲まれているなら、その積s_is_jは立方数とならないようにしたいです。 例えば、s_1=1,s_2=1,s_3=2,s_4=4のとき、s_1s_2を同時に丸で囲むことはできません。また、s_3s_4を同時に丸で囲むこともできません。

高橋君が丸で囲むことができる整数の個数の最大値を求めてください。

制約

  • 1 ≦ N ≦ 10^5
  • 1 ≦ s_i ≦ 10^{10}
  • 入力はすべて整数である。

入力

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

N
s_1
:
s_N

出力

高橋君が丸で囲むことができる整数の個数の最大値を表す整数を出力せよ。


入力例 1

8
1
2
3
4
5
6
7
8

出力例 1

6

1,2,3,5,6,7 を丸で囲むことができます。


入力例 2

6
2
4
8
16
32
64

出力例 2

3

入力例 3

10
1
10
100
1000000007
10000000000
1000000009
999999999
999
999
999

出力例 3

9

Score : 1100 points

Problem Statement

Snuke got positive integers s_1,...,s_N from his mother, as a birthday present. There may be duplicate elements.

He will circle some of these N integers. Since he dislikes cubic numbers, he wants to ensure that if both s_i and s_j (i ≠ j) are circled, the product s_is_j is not cubic. For example, when s_1=1,s_2=1,s_3=2,s_4=4, it is not possible to circle both s_1 and s_2 at the same time. It is not possible to circle both s_3 and s_4 at the same time, either.

Find the maximum number of integers that Snuke can circle.

Constraints

  • 1 ≦ N ≦ 10^5
  • 1 ≦ s_i ≦ 10^{10}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
s_1
:
s_N

Output

Print the maximum number of integers that Snuke can circle.


Sample Input 1

8
1
2
3
4
5
6
7
8

Sample Output 1

6

Snuke can circle 1,2,3,5,6,7.


Sample Input 2

6
2
4
8
16
32
64

Sample Output 2

3

Sample Input 3

10
1
10
100
1000000007
10000000000
1000000009
999999999
999
999
999

Sample Output 3

9
E - Sequential operations on Sequence

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

配点 : 1400

問題文

高橋君はお母さんから数列をもらいました。この数列の長さは N で、i(1 ≦ i ≦ N) 項目の要素は i です。 高橋君は、この数列に以下の操作を合計で Q 回行いました。i 番目の操作は、パラメータ q_i であらわされ、以下のように行われます。

  • いまの数列を無限回繰り返した数列の先頭 q_i 項をとって、新たな数列とする。

Q 回の操作後、この数列に 1 から N までの各々の数が何回ずつ現れるかを求めてください。

制約

  • 1 ≦ N ≦ 10^5
  • 0 ≦ Q ≦ 10^5
  • 1 ≦ q_i ≦ 10^{18}
  • 入力はすべて整数である。

入力

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

N Q
q_1
:
q_Q

出力

N 行出力せよ。i(1 ≦ i ≦ N) 行目には、Q 回の操作後の数列にあらわれる数 i の個数を表す整数ひとつを出力せよ。


入力例 1

5 3
6
4
11

出力例 1

3
3
3
2
0

1 回目の操作で、数列は 1,2,3,4,5,1 となります。

2 回目の操作で、数列は 1,2,3,4 となります。

3 回目の操作で、数列は 1,2,3,4,1,2,3,4,1,2,3 となります。 この数列には 1,2,3,4,5 がそれぞれ 3,3,3,2,0 個含まれているので、上のように出力します。


入力例 2

10 10
9
13
18
8
10
10
9
19
22
27

出力例 2

7
4
4
3
3
2
2
2
0
0

Score : 1400 points

Problem Statement

Snuke got an integer sequence from his mother, as a birthday present. The sequence has N elements, and the i-th of them is i. Snuke performs the following Q operations on this sequence. The i-th operation, described by a parameter q_i, is as follows:

  • Take the first q_i elements from the sequence obtained by concatenating infinitely many copy of the current sequence, then replace the current sequence with those q_i elements.

After these Q operations, find how many times each of the integers 1 through N appears in the final sequence.

Constraints

  • 1 ≦ N ≦ 10^5
  • 0 ≦ Q ≦ 10^5
  • 1 ≦ q_i ≦ 10^{18}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N Q
q_1
:
q_Q

Output

Print N lines. The i-th line (1 ≦ i ≦ N) should contain the number of times integer i appears in the final sequence after the Q operations.


Sample Input 1

5 3
6
4
11

Sample Output 1

3
3
3
2
0

After the first operation, the sequence becomes: 1,2,3,4,5,1.

After the second operation, the sequence becomes: 1,2,3,4.

After the third operation, the sequence becomes: 1,2,3,4,1,2,3,4,1,2,3.

In this sequence, integers 1,2,3,4,5 appear 3,3,3,2,0 times, respectively.


Sample Input 2

10 10
9
13
18
8
10
10
9
19
22
27

Sample Output 2

7
4
4
3
3
2
2
2
0
0
F - Fraction of Fractal

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

配点 : 1700

問題文

高橋君はお母さんからグリッドをもらいました。このグリッドは縦 H マス×横 W マスからなり、各マスは黒か白で塗られています。 黒いマス全体は、縦横にひとつながりになっています。

このグリッドの ij(1 ≦ i ≦ H, 1 ≦ j ≦ W) のマスの情報は、縦横に並んだ文字 s_{ij} であらわされ、 s_{ij}# のときこのマスが黒く、 . のとき白く塗られていることを表します。少なくともひとつのマスが黒く塗られています。

レベル 0 のフラクタルとは黒いマスひとつからなる 1 × 1 のグリッドであり、 レベル k+1 のフラクタルとは、レベル k のフラクタルを、お母さんからもらったグリッドの全ての黒いマスに相当する位置に並べ、白いマスに相当する位置は白いマスで埋めたものを指します。

お母さんからもらったグリッドの情報と整数 K が与えられるので、レベル K のフラクタルに黒いマスからなる連結成分がいくつあるかを 10^9+7 で割ったあまりを求めてください。

制約

  • 1 ≦ H,W ≦ 1000
  • 0 ≦ K ≦ 10^{18}
  • s_{ij}#. のいずれかである。
  • # のマス全体は縦横に連結である。
  • # のマスは少なくともひとつ存在する。

入力

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

H W K
s_{11} .. s_{1W}
:
s_{H1} .. s_{HW}

出力

レベル K のフラクタルにある黒いマスからなる連結成分の個数を 10^9+7 で割ったあまりを一行に出力せよ。


入力例 1

3 3 3
.#.
###
#.#

出力例 1

20

この入力例で作られるフラクタルは、以下のようなものです。この黒マスからなる連結成分の数は 20 なので、20 を出力します。

.............#.............
............###............
............#.#............
..........#..#..#..........
.........#########.........
.........#.##.##.#.........
..........#.....#..........
.........###...###.........
.........#.#...#.#.........
....#........#........#....
...###......###......###...
...#.#......#.#......#.#...
.#..#..#..#..#..#..#..#..#.
###########################
#.##.##.##.##.##.##.##.##.#
.#.....#..#.....#..#.....#.
###...######...######...###
#.#...#.##.#...#.##.#...#.#
....#.................#....
...###...............###...
...#.#...............#.#...
.#..#..#...........#..#..#.
#########.........#########
#.##.##.#.........#.##.##.#
.#.....#...........#.....#.
###...###.........###...###
#.#...#.#.........#.#...#.#

入力例 2

3 3 3
###
#.#
###

出力例 2

1

入力例 3

11 15 1000000000000000000
.....#.........
....###........
....####.......
...######......
...#######.....
..##.###.##....
..##########...
.###.....####..
.####...######.
###############
#.##..##..##..#

出力例 3

301811921

Score : 1700 points

Problem Statement

Snuke got a grid from his mother, as a birthday present. The grid has H rows and W columns. Each cell is painted black or white. All black cells are 4-connected, that is, it is possible to traverse from any black cell to any other black cell by just visiting black cells, where it is only allowed to move horizontally or vertically.

The color of the cell at the i-th row and j-th column (1 ≦ i ≦ H, 1 ≦ j ≦ W) is represented by a character s_{ij}. If s_{ij} is #, the cell is painted black. If s_{ij} is ., the cell is painted white. At least one cell is painted black.

We will define fractals as follows. The fractal of level 0 is a 1 × 1 grid with a black cell. The fractal of level k+1 is obtained by arranging smaller grids in H rows and W columns, following the pattern of the Snuke's grid. At a position that corresponds to a black cell in the Snuke's grid, a copy of the fractal of level k is placed. At a position that corresponds to a white cell in the Snuke's grid, a grid whose cells are all white, with the same dimensions as the fractal of level k, is placed.

You are given the description of the Snuke's grid, and an integer K. Find the number of connected components of black cells in the fractal of level K, modulo 10^9+7.

Constraints

  • 1 ≦ H,W ≦ 1000
  • 0 ≦ K ≦ 10^{18}
  • Each s_{ij} is either # or ..
  • All black cells in the given grid are 4-connected.
  • There is at least one black cell in the given grid.

Input

The input is given from Standard Input in the following format:

H W K
s_{11} .. s_{1W}
:
s_{H1} .. s_{HW}

Output

Print the number of connected components of black cells in the fractal of level K, modulo 10^9+7.


Sample Input 1

3 3 3
.#.
###
#.#

Sample Output 1

20

The fractal of level K=3 in this case is shown below. There are 20 connected components of black cells.

.............#.............
............###............
............#.#............
..........#..#..#..........
.........#########.........
.........#.##.##.#.........
..........#.....#..........
.........###...###.........
.........#.#...#.#.........
....#........#........#....
...###......###......###...
...#.#......#.#......#.#...
.#..#..#..#..#..#..#..#..#.
###########################
#.##.##.##.##.##.##.##.##.#
.#.....#..#.....#..#.....#.
###...######...######...###
#.#...#.##.#...#.##.#...#.#
....#.................#....
...###...............###...
...#.#...............#.#...
.#..#..#...........#..#..#.
#########.........#########
#.##.##.#.........#.##.##.#
.#.....#...........#.....#.
###...###.........###...###
#.#...#.#.........#.#...#.#

Sample Input 2

3 3 3
###
#.#
###

Sample Output 2

1

Sample Input 3

11 15 1000000000000000000
.....#.........
....###........
....####.......
...######......
...#######.....
..##.###.##....
..##########...
.###.....####..
.####...######.
###############
#.##..##..##..#

Sample Output 3

301811921