A - AtCoder Quiz 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

AtCoder 王国では、競技プログラミングの実力を測る検定試験が実施されています。

試験は 100 点満点であり、点数が高ければ高いほど、高いランクが認定されます。
ランクは以下のように定められています。

  • 0 点以上 40 点未満のとき、初級
  • 40 点以上 70 点未満のとき、中級
  • 70 点以上 90 点未満のとき、上級
  • 90 点以上のとき、エキスパート

高橋君は、この検定試験を受験し、X 点を取りました。

高橋君が認定されたランクより一つ高いランクとなるためには最低であと何点必要か求めてください。ただし、高橋君がエキスパートと認定された場合、それより高いランクは存在しないため expert と出力してください。

制約

  • 0 \leq X \leq 100
  • X は整数

入力

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

X

出力

答えを出力せよ。


入力例 1

56

出力例 1

14

高橋君は 56 点を取り、中級と認定されました。一つ高いランクである上級となるためには、最低であと 14 点必要です。


入力例 2

32

出力例 2

8

入力例 3

0

出力例 3

40

入力例 4

100

出力例 4

expert

高橋君は満点を取り、エキスパートと認定されました。これより高いランクは存在しないため、expert と出力します。

Score : 100 points

Problem Statement

In the Kingdom of AtCoder, there is a standardized test of competitive programming proficiency.

An examinee will get a score out of 100 and obtain a rank according to the score, as follows:

  • Novice, for a score not less than 0 but less than 40;
  • Intermediate, for a score not less than 40 but less than 70;
  • Advanced, for a score not less than 70 but less than 90;
  • Expert, for a score not less than 90.

Takahashi took this test and got X points.

Find the minimum number of extra points needed to go up one rank higher. If, however, his rank was Expert, print expert, as there is no higher rank than that.

Constraints

  • 0 \leq X \leq 100
  • X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the answer.


Sample Input 1

56

Sample Output 1

14

He got 56 points and was certified as Intermediate. In order to reach the next rank of Advanced, he needs at least 14 more points.


Sample Input 2

32

Sample Output 2

8

Sample Input 3

0

Sample Output 3

40

Sample Input 4

100

Sample Output 4

expert

He got full points and was certified as Expert. There is no rank higher than that, so print expert.

B - A Unique Letter

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

長さ 3 の文字列 S が与えられます。
S1 度だけ含まれる文字を 1 つ出力してください。
但し、そのような文字が存在しない場合は代わりに -1 と出力してください。

制約

  • S は英小文字のみからなる 3 文字の文字列

入力

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

S

出力

答えを出力せよ。正解が複数ある場合、どれを出力してもよい。


入力例 1

pop

出力例 1

o

popo1 度だけ含まれます。


入力例 2

abc

出力例 2

a

abca, b, c はどれも 1 度だけ含まれるので、どれを出力しても構いません。


入力例 3

xxx

出力例 3

-1

xxx1 度だけ含まれる文字はありません。

Score : 100 points

Problem Statement

You are given a string S of length 3.
Print a character that occurs only once in S.
If there is no such character, print -1 instead.

Constraints

  • S is a string of length 3 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer. If multiple solutions exist, you may print any of them.


Sample Input 1

pop

Sample Output 1

o

o occurs only once in pop.


Sample Input 2

abc

Sample Output 2

a

a, b, and c occur once each in abc, so you may print any of them.


Sample Input 3

xxx

Sample Output 3

-1

No character occurs only once in xxx.

C - Broken Rounding

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

非負整数 X に対し、 i=0,1,\dots,K-1 の順に次の操作を行ったとき、操作を全て終えた時点での X を求めてください。

  • X10^i の位以下を四捨五入する。
    • 厳密には、 X を「 |Y-X| が最小となる 10^{i+1} の倍数のうち最大のもの」である Y に置き換える。
    • 具体例を挙げる。
      • 27310^1 の位以下を四捨五入すれば 300 となる。
      • 99910^2 の位以下を四捨五入すれば 1000 となる。
      • 10010^9 の位以下を四捨五入すれば 0 となる。
      • 101510^0 の位以下を四捨五入すれば 1020 となる。

制約

  • X,K は整数
  • 0 \le X < 10^{15}
  • 1 \le K \le 15

入力

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

X K

出力

答えを整数として出力せよ。


入力例 1

2048 2

出力例 1

2100

操作の過程で、 X2048 \rightarrow 2050 \rightarrow 2100 と変化します。


入力例 2

1 15

出力例 2

0

入力例 3

999 3

出力例 3

1000

入力例 4

314159265358979 12

出力例 4

314000000000000

X32bit 整数型に収まらない可能性があります。

Score : 200 points

Problem Statement

Given a non-negative integer X, perform the following operation for i=1,2,\dots,K in this order and find the resulting X.

  • Round X off to the nearest 10^i.
    • Formally, replace X with Y that is "the largest multiple of 10^i that minimizes |Y-X|."
    • Here are some examples:
      • Rounding 273 off to the nearest 10^2 yields 300.
      • Rounding 999 off to the nearest 10^3 yields 1000.
      • Rounding 100 off to the nearest 10^{10} yields 0.
      • Rounding 1015 off to the nearest 10^1 yields 1020.

Constraints

  • X and K are integers.
  • 0 \le X < 10^{15}
  • 1 \le K \le 15

Input

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

X K

Output

Print the answer as an integer.


Sample Input 1

2048 2

Sample Output 1

2100

X changes as 2048 \rightarrow 2050 \rightarrow 2100 by the operations.


Sample Input 2

1 15

Sample Output 2

0

Sample Input 3

999 3

Sample Output 3

1000

Sample Input 4

314159265358979 12

Sample Output 4

314000000000000

X may not fit into a 32-bit integer type.

D - typo

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

文字列 S, T が与えられます。以下の操作を高々 1行うことで、ST と一致させることができるかを判定してください。

  • S の隣り合う 2 文字を選び、入れ替える。

なお、上記の操作を一度も行わないことも可能です。

制約

  • S, T はそれぞれ英小文字のみからなる、長さ 2 以上 100 以下の文字列
  • S の長さと T の長さは等しい

入力

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

S
T

出力

問題文中の操作を高々 1 回行うことで ST と一致させることができるなら Yes を、できないなら No を出力せよ。


入力例 1

abc
acb

出力例 1

Yes

S2 文字目と 3 文字目を入れ替えることで、ST と一致させることができます。


入力例 2

aabb
bbaa

出力例 2

No

どのように操作を行っても、ST と一致させることはできません。


入力例 3

abcde
abcde

出力例 3

Yes

ST は既に一致しています。

Score : 200 points

Problem Statement

You are given two strings S and T. Determine whether it is possible to make S and T equal by doing the following operation at most once:

  • choose two adjacent characters in S and swap them.

Note that it is allowed to choose not to do the operation.

Constraints

  • Each of S and T is a string of length between 2 and 100 (inclusive) consisting of lowercase English letters.
  • S and T have the same length.

Input

Input is given from Standard Input in the following format:

S
T

Output

If it is possible to make S and T equal by doing the operation in Problem Statement at most once, print Yes; otherwise, print No.


Sample Input 1

abc
acb

Sample Output 1

Yes

You can swap the 2-nd and 3-rd characters of S to make S and T equal.


Sample Input 2

aabb
bbaa

Sample Output 2

No

There is no way to do the operation to make S and T equal.


Sample Input 3

abcde
abcde

Sample Output 3

Yes

S and T are already equal.

E - Bingo 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 行、横 N 列のマス目があり、上から i 行目、左から j 列目のマスには整数 N\times (i-1)+j が書かれています。

今から T ターンにわたって相異なる整数が宣言されます。i ターン目には A_i が宣言され、A_i が書かれたマスに印をつけます。初めてビンゴを達成するのは何ターン目か求めてください。ただし、T ターンの中でビンゴを達成しない場合は -1 を出力してください。

ここで、ビンゴを達成するとは以下のいずれかのうち少なくとも一つ満たされることを言います。

  • マス目の横の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する
  • マス目の縦の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する
  • マス目の対角線の列であって、列に含まれる N 個のマスすべてに印がついているものが存在する

制約

  • 2\leq N\leq 2\times 10^3
  • 1\leq T\leq \min(N^2,2\times 10^5)
  • 1\leq A_i\leq N^2
  • i\neq j ならば A_i\neq A_j
  • 入力は全て整数

入力

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

N T
A_1 A_2 \ldots A_T

出力

T ターンの中でビンゴを達成するならば初めてビンゴを達成するターンを、そうでないならば -1 を出力せよ。


入力例 1

3 5
5 1 8 9 7

出力例 1

4

マス目の状態は以下のように変化します。初めてビンゴを達成するのは 4 ターン目です。


入力例 2

3 5
4 2 9 7 5

出力例 2

-1

5 ターンの中でビンゴを達成できないので -1 を出力してください。


入力例 3

4 12
13 9 6 5 2 7 16 14 8 3 10 11

出力例 3

9

Score : 300 points

Problem Statement

There is an N \times N grid, where the cell at the i-th row from the top and the j-th column from the left contains the integer N \times (i-1) + j.

Over T turns, integers will be announced. On Turn i, the integer A_i is announced, and the cell containing A_i is marked. Determine the turn on which Bingo is achieved for the first time. If Bingo is not achieved within T turns, print -1.

Here, achieving Bingo means satisfying at least one of the following conditions:

  • There exists a row in which all N cells are marked.
  • There exists a column in which all N cells are marked.
  • There exists a diagonal line (from top-left to bottom-right or from top-right to bottom-left) in which all N cells are marked.

Constraints

  • 2 \leq N \leq 2 \times 10^3
  • 1 \leq T \leq \min(N^2, 2 \times 10^5)
  • 1 \leq A_i \leq N^2
  • A_i \neq A_j if i \neq j.
  • All input values are integers.

Input

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

N T
A_1 A_2 \ldots A_T

Output

If Bingo is achieved within T turns, print the turn number on which Bingo is achieved for the first time; otherwise, print -1.


Sample Input 1

3 5
5 1 8 9 7

Sample Output 1

4

The state of the grid changes as follows. Bingo is achieved for the first time on Turn 4.


Sample Input 2

3 5
4 2 9 7 5

Sample Output 2

-1

Bingo is not achieved within five turns, so print -1.


Sample Input 3

4 12
13 9 6 5 2 7 16 14 8 3 10 11

Sample Output 3

9
F - Collision 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

xy 座標平面上に N 人の人がいます。人 i(X_i, Y_i) にいます。すべての人は異なる地点にいます。

L, R からなる長さ N の文字列 S があります。
iS_i = R ならば右向きに、S_i = L ならば左向きに、一斉に同じ速度で歩き始めます。ここで、右は x 軸の正の向き、左は x 軸の負の向きです。

たとえば (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL の場合は次の図のように動きます。

image

反対の向きに歩いている人同士が同じ地点に来ることを「衝突」と呼びます。すべての人が歩き続けたとき、衝突は発生しますか?

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq X_i \leq 10^9
  • 0 \leq Y_i \leq 10^9
  • i \neq j ならば (X_i, Y_i) \neq (X_j, Y_j) である。
  • X_i, Y_i はすべて整数である。
  • SL および R からなる長さ N の文字列である。

入力

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
S

出力

衝突が発生するならば Yes を、発生しないならば No を出力せよ。


入力例 1

3
2 3
1 1
4 1
RRL

出力例 1

Yes

この入力は問題文にある例と同じケースです。
すべての人が歩き続けると人 2 と人 3 が衝突します。よって Yes を出力します。


入力例 2

2
1 1
2 1
RR

出力例 2

No

1 と人 2 は同じ向きに歩いているので衝突することはありません。


入力例 3

10
1 3
1 4
0 0
0 2
0 4
3 1
2 4
4 2
4 4
3 3
RLRRRLRLRR

出力例 3

Yes

Score : 300 points

Problem Statement

There are N people in an xy-plane. Person i is at (X_i, Y_i). The positions of all people are different.

We have a string S of length N consisting of L and R.
If S_i = R, Person i is facing right; if S_i = L, Person i is facing left. All people simultaneously start walking in the direction they are facing. Here, right and left correspond to the positive and negative x-direction, respectively.

For example, the figure below shows the movement of people when (X_1, Y_1) = (2, 3), (X_2, Y_2) = (1, 1), (X_3, Y_3) =(4, 1), S = RRL.

image

We say that there is a collision when two people walking in opposite directions come to the same position. Will there be a collision if all people continue walking indefinitely?

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq X_i \leq 10^9
  • 0 \leq Y_i \leq 10^9
  • (X_i, Y_i) \neq (X_j, Y_j) if i \neq j.
  • All X_i and Y_i are integers.
  • S is a string of length N consisting of L and R.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
S

Output

If there will be a collision, print Yes; otherwise, print No.


Sample Input 1

3
2 3
1 1
4 1
RRL

Sample Output 1

Yes

This input corresponds to the example in the Problem Statement.
If all people continue walking, Person 2 and Person 3 will collide. Thus, Yes should be printed.


Sample Input 2

2
1 1
2 1
RR

Sample Output 2

No

Since Person 1 and Person 2 walk in the same direction, they never collide.


Sample Input 3

10
1 3
1 4
0 0
0 2
0 4
3 1
2 4
4 2
4 4
3 3
RLRRRLRLRR

Sample Output 3

Yes
G - 8 Puzzle on Graph

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は道端であるパズルを拾いました。
このパズルは、9 個の頂点と M 本の辺からなる無向グラフ、および、8 つのコマで構成されます。

グラフの 9 つの頂点はそれぞれ頂点 1、頂点 2\ldots、頂点 9 と呼ばれ、 i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結んでいます。
8 つのコマはそれぞれコマ 1、コマ 2\ldots、コマ 8 と呼ばれ、 j = 1, 2, \ldots, 8 について、コマ j は頂点 p_j に置かれています。 ここで、すべてのコマはそれぞれ異なる頂点に置かれていることが保証されます。 コマが置かれていない「空の頂点」がただ一つ存在することに注意してください。

高橋君はこのパズルに対して下記の操作を好きな回数( 0 回でもよい)だけ行うことができます。

空の頂点に隣接する頂点に置かれたコマを 1 つ選び、選んだコマを空の頂点に移動する。

高橋君は上記の操作を繰り返して、このパズルを「完成」させようとしています。 パズルは、下記の状態を満たしたときに完成となります。

j = 1, 2, \ldots, 8 について、コマ j は 頂点 j に置かれている。

高橋君がパズルを完成させることが可能かどうかを判定し、可能な場合はそのために必要な操作回数の最小値を出力してください。

制約

  • 0 \leq M \leq 36
  • 1 \leq u_i, v_i \leq 9
  • 与えられるグラフは多重辺、自己ループを持たない
  • 1 \leq p_j \leq 9
  • j \neq j' \Rightarrow p_j \neq p_{j'}
  • 入力はすべて整数

入力

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

M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
p_1 p_2 \ldots p_8

出力

高橋君がパズルを完成させることが可能な場合は、そのために必要な操作回数の最小値を出力せよ。 高橋君がパズルを完成させることが不可能な場合は、-1 を出力せよ。


入力例 1

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

出力例 1

5

下記の手順によって、5 回の操作でパズルを完成させることができます。

  1. コマ 2 を頂点 9 から頂点 1 に移動する。
  2. コマ 3 を頂点 2 から頂点 9 に移動する。
  3. コマ 2 を頂点 1 から頂点 2 に移動する。
  4. コマ 1 を頂点 3 から頂点 1 に移動する。
  5. コマ 3 を頂点 9 から頂点 3 に移動する。

一方、5 回未満の操作でパズルを完成させることはできません。よって、5 を出力します。
与えられるグラフは連結とは限らないことに注意してください。


入力例 2

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

出力例 2

0

パズルは初めから完成しています。よって、完成させるために必要な操作回数の最小値は 0 回です。


入力例 3

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

出力例 3

-1

操作の繰り返しによってパズルを完成させることができないので、-1 を出力します。


入力例 4

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

出力例 4

16

Score : 400 points

Problem Statement

Takahashi found a puzzle along some road.
It is composed of an undirected graph with nine vertices and M edges, and eight pieces.

The nine vertices of the graph are called Vertex 1, Vertex 2, \ldots, Vertex 9. For each i = 1, 2, \ldots, M, the i-th edge connects Vertex u_i and Vertex v_i.
The eight pieces are called Piece 1, Piece 2, \ldots, Piece 8. For each j = 1, 2, \ldots, 8, Piece j is on Vertex p_j.
Here, it is guaranteed that all pieces are on distinct vertices. Note that there is exactly one empty vertex without a piece.

Takahashi can do the following operation on the puzzle any number of times (possibly zero).

Choose a piece on a vertex adjacent to the empty vertex, and move it to the empty vertex.

By repeating this operation, he aims to complete the puzzle. The puzzle is considered complete when the following holds.

  • For each j = 1, 2, \ldots, 8, Piece j is on Vertex j.

Determine whether it is possible for Takahashi to complete the puzzle. If it is possible, find the minimum number of operations needed to do so.

Constraints

  • 0 \leq M \leq 36
  • 1 \leq u_i, v_i \leq 9
  • The given graph has no multi-edges or self-loops.
  • 1 \leq p_j \leq 9
  • j \neq j' \Rightarrow p_j \neq p_{j'}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
p_1 p_2 \ldots p_8

Output

If it is possible for Takahashi to complete the puzzle, find the minimum number of operations needed to do so. Otherwise, print -1.


Sample Input 1

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

Sample Output 1

5

The following procedure completes the puzzle in five operations.

  1. Move Piece 2 from Vertex 9 to Vertex 1.
  2. Move Piece 3 from Vertex 2 to Vertex 9.
  3. Move Piece 2 from Vertex 1 to Vertex 2.
  4. Move Piece 1 from Vertex 3 to Vertex 1.
  5. Move Piece 3 from Vertex 9 to Vertex 3.

On the other hand, it is impossible to complete the puzzle in less than five operations. Thus, we should print 5.
Note that the given graph may not be connected.


Sample Input 2

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

Sample Output 2

0

The puzzle is already complete from the beginning. Thus, the minimum number of operations needed to complete the puzzle is 0.


Sample Input 3

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

Sample Output 3

-1

No sequence of operations can complete the puzzle, so we should print -1.


Sample Input 4

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

Sample Output 4

16
H - LCM on Whiteboard

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 個の整数 a_1,\ldots,a_N が白板に書かれています。
ここで、a_im_i 個の素数 p_{i,1} \lt \ldots \lt p_{i,m_i} と正整数 e_{i,1},\ldots,e_{i,m_i} を用いて a_i = p_{i,1}^{e_{i,1}} \times \ldots \times p_{i,m_i}^{e_{i,m_i}} と表せる整数です。
あなたは N 個の整数から 1 つ選んで 1 に書き換えます。
書き換えた後の N 個の整数の最小公倍数としてあり得る値の個数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq m_i
  • \sum{m_i} \leq 2 \times 10^5
  • 2 \leq p_{i,1} \lt \ldots \lt p_{i,m_i} \leq 10^9
  • p_{i,j} は素数
  • 1 \leq e_{i,j} \leq 10^9
  • 入力はすべて整数

入力

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

N
m_1
p_{1,1} e_{1,1}
\vdots
p_{1,m_1} e_{1,m_1}
m_2
p_{2,1} e_{2,1}
\vdots
p_{2,m_2} e_{2,m_2}
\vdots
m_N
p_{N,1} e_{N,1}
\vdots
p_{N,m_N} e_{N,m_N}

出力

答えを出力せよ。


入力例 1

4
1
7 2
2
2 2
5 1
1
5 1
2
2 1
7 1

出力例 1

3

白板に書かれている整数は a_1 =7^2=49, a_2=2^2 \times 5^1 = 20, a_3 = 5^1 = 5, a_4=2^1 \times 7^1 = 14 です。
a_11 に書き換えると白板に書かれている整数は 1,20,5,14 となり、これらの最小公倍数は 140 です。
a_21 に書き換えると白板に書かれている整数は 49,1,5,14 となり、これらの最小公倍数は 490 です。
a_31 に書き換えると白板に書かれている整数は 49,20,1,14 となり、これらの最小公倍数は 980 です。
a_41 に書き換えると白板に書かれている整数は 49,20,5,1 となり、これらの最小公倍数は 980 です。
以上より、書き換えた後の N 個の整数の最小公倍数としてあり得る値は 140,490,980 であり、この入力における答えが 3 と分かります。


入力例 2

1
1
998244353 1000000000

出力例 2

1

白板に書かれている整数はとても大きい場合があります。

Score : 500 points

Problem Statement

There are N integers a_1,\ldots,a_N written on a whiteboard.
Here, a_i can be represented as a_i = p_{i,1}^{e_{i,1}} \times \ldots \times p_{i,m_i}^{e_{i,m_i}} using m_i prime numbers p_{i,1} \lt \ldots \lt p_{i,m_i} and positive integers e_{i,1},\ldots,e_{i,m_i}.
You will choose one of the N integers to replace it with 1.
Find the number of values that can be the least common multiple of the N integers after the replacement.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq m_i
  • \sum{m_i} \leq 2 \times 10^5
  • 2 \leq p_{i,1} \lt \ldots \lt p_{i,m_i} \leq 10^9
  • p_{i,j} is prime.
  • 1 \leq e_{i,j} \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
m_1
p_{1,1} e_{1,1}
\vdots
p_{1,m_1} e_{1,m_1}
m_2
p_{2,1} e_{2,1}
\vdots
p_{2,m_2} e_{2,m_2}
\vdots
m_N
p_{N,1} e_{N,1}
\vdots
p_{N,m_N} e_{N,m_N}

Output

Print the answer.


Sample Input 1

4
1
7 2
2
2 2
5 1
1
5 1
2
2 1
7 1

Sample Output 1

3

The integers on the whiteboard are a_1 =7^2=49, a_2=2^2 \times 5^1 = 20, a_3 = 5^1 = 5, a_4=2^1 \times 7^1 = 14.
If you replace a_1 with 1, the integers on the whiteboard become 1,20,5,14, whose least common multiple is 140.
If you replace a_2 with 1, the integers on the whiteboard become 49,1,5,14, whose least common multiple is 490.
If you replace a_3 with 1, the integers on the whiteboard become 49,20,1,14, whose least common multiple is 980.
If you replace a_4 with 1, the integers on the whiteboard become 49,20,5,1, whose least common multiple is 980.
Therefore, the least common multiple of the N integers after the replacement can be 140, 490, or 980, so the answer is 3.


Sample Input 2

1
1
998244353 1000000000

Sample Output 2

1

There may be enormous integers on the whiteboard.

I - Blocked Roads

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 頂点 M 辺の有向グラフが与えられます。頂点には 1 から N の番号、辺には 1 から M の番号がついています。辺 i\,(1 \leq i \leq M) は頂点 s_i から頂点 t_i に向かう長さ 1 の辺です。

i\,(1 \leq i \leq M) について、辺 i のみ通れないときの頂点 1 から頂点 N までの最短距離を求めてください。ただし、頂点 1 から頂点 N にたどり着けない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 400
  • 1 \leq M \leq N(N-1)
  • 1 \leq s_i,t_i \leq N
  • s_i \neq t_i
  • (s_i,t_i) \neq (s_j,t_j) (i \neq j)
  • 入力は全て整数である。

入力

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

N M
s_1 t_1
s_2 t_2
\vdots
s_M t_M

出力

M 行出力せよ。

i 行目には、辺 i のみ通れないときの頂点 1 から頂点 N までの最短距離を出力せよ。ただし、頂点 1 から頂点 N にたどり着けない場合は -1 を出力せよ。


入力例 1

3 3
1 2
1 3
2 3

出力例 1

1
2
1

入力例 2

4 4
1 2
2 3
2 4
3 4

出力例 2

-1
2
3
2

1 のみ通れないとき、頂点 1 から頂点 N にたどり着けないので -1 を出力します。


入力例 3

5 10
1 2
1 4
1 5
2 1
2 3
3 1
3 2
3 5
4 2
4 3

出力例 3

1
1
3
1
1
1
1
1
1
1

入力例 4

4 1
1 2

出力例 4

-1

Score : 500 points

Problem Statement

You are given a directed graph with N vertices and M edges. The vertices are numbered 1 through N, and the edges are numbered 1 through M. Edge i (1 \leq i \leq M) goes from Vertex s_i to Vertex t_i and has a length of 1.

For each i (1 \leq i \leq M), find the shortest distance from Vertex 1 to Vertex N when all edges except Edge i are passable, or print -1 if Vertex N is unreachable from Vertex 1.

Constraints

  • 2 \leq N \leq 400
  • 1 \leq M \leq N(N-1)
  • 1 \leq s_i,t_i \leq N
  • s_i \neq t_i
  • (s_i,t_i) \neq (s_j,t_j) (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
s_1 t_1
s_2 t_2
\vdots
s_M t_M

Output

Print M lines.

The i-th line should contain the shortest distance from Vertex 1 to Vertex N when all edges except Edge i are passable, or -1 if Vertex N is unreachable from Vertex 1.


Sample Input 1

3 3
1 2
1 3
2 3

Sample Output 1

1
2
1

Sample Input 2

4 4
1 2
2 3
2 4
3 4

Sample Output 2

-1
2
3
2

Vertex N is unreachable from Vertex 1 when all edges except Edge 1 are passable, so the corresponding line contains -1.


Sample Input 3

5 10
1 2
1 4
1 5
2 1
2 3
3 1
3 2
3 5
4 2
4 3

Sample Output 3

1
1
3
1
1
1
1
1
1
1

Sample Input 4

4 1
1 2

Sample Output 4

-1