A - Lucky Direction

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

8 種類の方角(北、東、西、南、北東、北西、南東、南西)のいずれかを表す文字列 D が与えられます。方角とそれを表す文字列は以下のように対応しています。

  • 北:N
  • 東:E
  • 西:W
  • 南:S
  • 北東:NE
  • 北西:NW
  • 南東:SE
  • 南西:SW

D が表す方角の反対の方角を表す文字列を出力してください。

制約

  • DN, E, W, S, NE, NW, SE, SW のいずれか

入力

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

D

出力

答えを出力せよ。


入力例 1

N

出力例 1

S

北の反対の方角である南を表す S を出力してください。


入力例 2

SE

出力例 2

NW

南東の反対の方角である北西を表す NW を出力してください。

Score : 100 points

Problem Statement

You are given a string D representing one of the eight directions (north, east, west, south, northeast, northwest, southeast, southwest). The correspondence between the directions and their representing strings is as follows.

  • North: N
  • East: E
  • West: W
  • South: S
  • Northeast: NE
  • Northwest: NW
  • Southeast: SE
  • Southwest: SW

Print the string representing the direction opposite to the direction denoted by D.

Constraints

  • D is one of N, E, W, S, NE, NW, SE, SW.

Input

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

D

Output

Print the answer.


Sample Input 1

N

Sample Output 1

S

Print S, which represents south, the direction opposite to north.


Sample Input 2

SE

Sample Output 2

NW

Print NW, which represents northwest, the direction opposite to southeast.

B - Digit Machine

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

1 桁の数字が表示される画面と、ボタンからなる機械があります。

画面に数字 k が表示されているとき、ボタンを 1 回押すと画面の数字が a_k に変わります。

0 が表示されている状態からボタンを 3 回押すと、画面には何が表示されますか?

制約

  • 0\leq a_i \leq 9
  • 入力は全て整数である

入力

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

a_0 a_1 \dots a_9

出力

答えを出力せよ。


入力例 1

9 0 1 2 3 4 5 6 7 8

出力例 1

7

画面に表示される数字は、0 \rightarrow 9 \rightarrow 8 \rightarrow 7 と変化します。


入力例 2

4 8 8 8 0 8 8 8 8 8

出力例 2

4

画面に表示される数字は、0 \rightarrow 4 \rightarrow 0 \rightarrow 4 と変化します。


入力例 3

0 0 0 0 0 0 0 0 0 0

出力例 3

0

Score : 100 points

Problem Statement

There is a device with a screen that shows a single-digit number, and a button.

When the screen is showing a number k, pressing the button once changes the number on the screen to a_k.

The device currently shows 0. After pressing the button 3 times, what will be shown on the screen?

Constraints

  • 0\leq a_i \leq 9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

a_0 a_1 \dots a_9

Output

Print the answer.


Sample Input 1

9 0 1 2 3 4 5 6 7 8

Sample Output 1

7

The number on the screen transitions as 0 \rightarrow 9 \rightarrow 8 \rightarrow 7.


Sample Input 2

4 8 8 8 0 8 8 8 8 8

Sample Output 2

4

The number on the screen transitions as 0 \rightarrow 4 \rightarrow 0 \rightarrow 4.


Sample Input 3

0 0 0 0 0 0 0 0 0 0

Sample Output 3

0
C - 326-like Numbers

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

3 桁の正整数であって、百の位の数と十の位の数の積が一の位の数と等しいものを 326-like number と呼びます。

例えば 326,400,144 は 326-like number であり、623,777,429 は 326-like number ではありません。

整数 N が与えられるので、N 以上の最小の 326-like number を求めてください。なお、制約の条件下で答えは必ず存在します。

制約

  • 100 \leq N \leq 919
  • N は整数である

入力

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

N

出力

答えを出力せよ。


入力例 1

320

出力例 1

326

320,321,322,323,324,325 は 326-like number ではなく、326 は 326-like number です。


入力例 2

144

出力例 2

144

144 は 326-like number です。


入力例 3

516

出力例 3

600

Score : 200 points

Problem Statement

A 326-like number is a three-digit positive integer where the product of the hundreds and tens digits equals the ones digit.

For example, 326,400,144 are 326-like numbers, while 623,777,429 are not.

Given an integer N, find the smallest 326-like number greater than or equal to N. It always exists under the constraints.

Constraints

  • 100 \leq N \leq 919
  • N is an integer.

Input

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

N

Output

Print the answer.


Sample Input 1

320

Sample Output 1

326

320,321,322,323,324,325 are not 326-like numbers, while 326 is a 326-like number.


Sample Input 2

144

Sample Output 2

144

144 is a 326-like number.


Sample Input 3

516

Sample Output 3

600
D - 11/11

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

AtCoder 国では、1 年が N か月からなる暦を使っています。 i(1\leq i\leq N) は、i1 日から iD _ i 日までの D _ i 日からなります。

AtCoder 国において、1 年のうち日付がゾロ目になる日が何日あるか求めてください。

ただし、ij(1\leq i\leq N,1\leq j\leq D _ i) の日付がゾロ目になるとは、1 種類の数字だけを用いて ij を十進法で表すことができることをいいます。

制約

  • 1\leq N\leq100
  • 1\leq D _ i\leq100\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N
D _ 1 D _ 2 \ldots D _ N

出力

答えを出力せよ。


入力例 1

12
31 29 31 30 31 30 31 31 30 31 30 31

出力例 1

13

AtCoder 国では、 11 日、111 日、22 日、222 日、33 日、44 日、55 日、66 日、77 日、88 日、99 日、111 日、1111 日の合計 13 日の日付がゾロ目になります。


入力例 2

10
10 1 2 3 4 5 6 7 8 100

出力例 2

1

AtCoder 国では、11 日のみが日付がゾロ目になります。


入力例 3

30
73 8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32

出力例 3

15

Score : 200 points

Problem Statement

AtCoder Kingdom uses a calendar whose year has N months. Month i (1\leq i\leq N) has D _ i days, from day 1 of month i to day D _ i of month i.

How many days in a year of AtCoder have "repdigits" dates?

Here, day j of month i (1\leq i\leq N,1\leq j\leq D _ i) is said to have a repdigit date if and only if all digits in the decimal notations of i and j are the same.

Constraints

  • 1\leq N\leq100
  • 1\leq D _ i\leq100\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N
D _ 1 D _ 2 \ldots D _ N

Output

Print the answer.


Sample Input 1

12
31 29 31 30 31 30 31 31 30 31 30 31

Sample Output 1

13

In AtCoder Kingdom, the days that have repdigit dates are January 1, January 11, February 2, February 22, March 3, April 4, May 5, June 6, July 7, August 8, September 9, November 1, and November 11, for a total of 13 days.


Sample Input 2

10
10 1 2 3 4 5 6 7 8 100

Sample Output 2

1

In AtCoder Kingdom, only January 1 has a repdigit date.


Sample Input 3

30
73 8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32

Sample Output 3

15
E - Slot Strategy 2 (Easy)

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

この問題は G 問題の簡易版です。

3 個のリールからなるスロットがあります。
i 番目のリールの配列は文字列 S_i によって表されます。ここで、S_i は数字のみからなる長さ M の文字列です。

それぞれのリールには対応するボタンがついています。高橋君は各非負整数 t について、スロットが回り始めてからちょうど t 秒後にボタンを 1 つ選んで押す、または何もしないことができます。
スロットが回り始めてから t 秒後に i 番目のリールに対応するボタンを押すと、i 番目のリールは S_i(t \bmod M)+1 文字目を表示して止まります。
ただし、t \bmod MtM で割ったあまりを表します。

高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。
そのようなことが不可能であればそのことを報告してください。

制約

  • 1 \leq M \leq 100
  • M は整数
  • S_i は数字のみからなる長さ M の文字列

入力

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

M
S_1
S_2
S_3

出力

全てのリールを止めた上で、表示されている文字が全て同じであるようにすることができないなら -1 を出力せよ。
できるなら、スロットが回り始めてからそのような状態にするまでに最小で何秒かかるか出力せよ。


入力例 1

10
1937458062
8124690357
2385760149

出力例 1

6

高橋君は次のようにそれぞれのリールを止めることでスロットが回り始めてから 6 秒後にリールに表示される文字を 8 で揃えることができます。

  • スロットの回転開始から 0 秒後に 2 番目のリールに対応するボタンを押します。2 番目のリールは S_2(0 \bmod 10)+1=1 文字目である 8 を表示して止まります。
  • スロットの回転開始から 2 秒後に 3 番目のリールに対応するボタンを押します。3 番目のリールは S_3(2 \bmod 10)+1=3 文字目である 8 を表示して止まります。
  • スロットの回転開始から 6 秒後に 1 番目のリールに対応するボタンを押します。1 番目のリールは S_1(6 \bmod 10)+1=7 文字目である 8 を表示して止まります。

5 秒以下で全てのリールに表示されている文字を揃える方法はないため、6 を出力します。


入力例 2

20
01234567890123456789
01234567890123456789
01234567890123456789

出力例 2

20

全てのリールを止めた上で、表示されている文字を揃える必要がある事に注意してください。


入力例 3

5
11111
22222
33333

出力例 3

-1

表示されている文字が全て同じであるようにリールを止めることはできません。
このとき -1 を出力してください。

Score : 300 points

Problem Statement

This problem is an easier version of Problem G.

There is a slot machine with three reels.
The arrangement of symbols on the i-th reel is represented by the string S_i. Here, S_i is a string of length M consisting of digits.

Each reel has a corresponding button. For each non-negative integer t, Takahashi can either choose and press one button or do nothing exactly t seconds after the reels start spinning.
If he presses the button corresponding to the i-th reel exactly t seconds after the reels start spinning, the i-th reel will stop and display the ((t \bmod M)+1)-th character of S_i.
Here, t \bmod M denotes the remainder when t is divided by M.

Takahashi wants to stop all the reels so that all the displayed characters are the same.
Find the minimum possible number of seconds from the start of the spin until all the reels are stopped so that his goal is achieved.
If this is impossible, report that fact.

Constraints

  • 1 \leq M \leq 100
  • M is an integer.
  • S_i is a string of length M consisting of digits.

Input

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

M
S_1
S_2
S_3

Output

If it is impossible to stop all the reels so that all the displayed characters are the same, print -1.
Otherwise, print the minimum possible number of seconds from the start of the spin until such a state is achieved.


Sample Input 1

10
1937458062
8124690357
2385760149

Sample Output 1

6

Takahashi can stop each reel as follows so that 6 seconds after the reels start spinning, all the reels display 8.

  • Press the button corresponding to the second reel 0 seconds after the reels start spinning. The second reel stops and displays 8, the ((0 \bmod 10)+1=1)-st character of S_2.
  • Press the button corresponding to the third reel 2 seconds after the reels start spinning. The third reel stops and displays 8, the ((2 \bmod 10)+1=3)-rd character of S_3.
  • Press the button corresponding to the first reel 6 seconds after the reels start spinning. The first reel stops and displays 8, the ((6 \bmod 10)+1=7)-th character of S_1.

There is no way to make the reels display the same character in 5 or fewer seconds, so print 6.


Sample Input 2

20
01234567890123456789
01234567890123456789
01234567890123456789

Sample Output 2

20

Note that he must stop all the reels and make them display the same character.


Sample Input 3

5
11111
22222
33333

Sample Output 3

-1

It is impossible to stop the reels so that all the displayed characters are the same.
In this case, print -1.