A - wwwvvvvvv

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

配点 : 100 点

問題文

v と w のみからなる文字列 S が与えられます。

S の中に、下に尖っている部分が何箇所あるかを出力してください(入出力例にある図もご参照ください)。

制約

  • S は v と w のみからなる文字列
  • S の長さは 1 以上 100 以下

入力

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

S

出力

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


入力例 1

vvwvw

出力例 1

7

vvwvw

上の画像のように、vvwvw という文字列には下に尖った部分が 7 箇所あります。


入力例 2

v

出力例 2

1

入力例 3

wwwvvvvvv

出力例 3

12

Score : 100 points

Problem Statement

You are given a string S consisting of v and w.

Print the number of "bottoms" in the string S (see the figure at Sample Input/Output).

Constraints

  • S is a string consisting of v and w.
  • The length of S is between 1 and 100, inclusive.

Input

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

S

Output

Print the answer as an integer.


Sample Input 1

vvwvw

Sample Output 1

7

vvwvw

The image above shows the seven "bottoms" in the string vvwvw.


Sample Input 2

v

Sample Output 2

1

Sample Input 3

wwwvvvvvv

Sample Output 3

12
B - Happy Birthday! 4

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

配点 : 100 点

問題文

正整数 X,Y,Z が与えられます。

高橋君と青木君は現在それぞれ X 歳、Y 歳です。

高橋君と青木君は毎年 1 月 1 日を迎えると同時に 1 歳ずつ歳を取ります。

(今年も含めて)これから高橋君の年齢が青木君の年齢のちょうど Z 倍となるタイミングが存在するか判定してください。

制約

  • 1\le X,Y \le 100
  • 2\le Z\le 10
  • 入力される値は全て整数

入力

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

X Y Z

出力

高橋君の年齢が青木君の年齢のちょうど Z 倍となるタイミングが存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

44 20 2

出力例 1

Yes

今から 4 年後に高橋君は 48 歳、青木君は 24 歳になり、高橋君の年齢は青木君の年齢のちょうど 2 倍になります。したがって、 Yes を出力してください。


入力例 2

28 10 3

出力例 2

No

これから高橋君の年齢が青木君の年齢のちょうど 3 倍となるタイミングは存在しません。したがって、No を出力してください。


入力例 3

50 5 10

出力例 3

Yes

現在の高橋君の年齢は青木君の年齢のちょうど 10 倍です。

今年も含めてこれから高橋君の年齢が青木君の年齢のちょうど Z 倍となるタイミングが存在するか判定することに注意してください。


入力例 4

1 100 2

出力例 4

No

Score : 100 points

Problem Statement

You are given positive integers X,Y,Z.

Takahashi and Aoki are currently X years old and Y years old, respectively.

Takahashi and Aoki age by 1 year simultaneously on every January 1st.

Determine whether there is a moment in the future (including this year) when Takahashi's age becomes exactly Z times Aoki's age.

Constraints

  • 1\le X,Y \le 100
  • 2\le Z\le 10
  • All input values are integers.

Input

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

X Y Z

Output

Output Yes if there is a moment in the future when Takahashi's age becomes exactly Z times Aoki's age, and No otherwise.


Sample Input 1

44 20 2

Sample Output 1

Yes

Four years from now, Takahashi will be 48 years old and Aoki will be 24 years old, so Takahashi's age will be exactly twice Aoki's age. Thus, output Yes.


Sample Input 2

28 10 3

Sample Output 2

No

There will never be a moment when Takahashi's age becomes exactly three times Aoki's age. Thus, output No.


Sample Input 3

50 5 10

Sample Output 3

Yes

Currently, Takahashi's age is exactly 10 times Aoki's age.

Note that you need to determine whether there will be such a moment in the future including this year.


Sample Input 4

1 100 2

Sample Output 4

No
C - Batters

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

配点 : 200 点

問題文

高橋君は野球をモチーフにしたゲームを作ろうとしましたが、うまくコードが書けなくて困っています。
高橋君の代わりに次の問題を解くプログラムを作ってください。

マス 0, マス 1, マス 2, マス 3 の 4 つのマス目があります。はじめマスの上には何もありません。
また、整数 P があり、はじめ P = 0 です。
正の整数からなる数列 A = (A_1, A_2, \dots, A_N) が与えられるので、i = 1, 2, \dots, N について順番に次の操作を行います。

  1. マス 0 に駒を 1 個置く。
  2. マス上のすべての駒を番号が A_i 大きいマスに進める。言い換えると、駒がマス x にあればその駒をマス x + A_i に移動する。
    ただし移動先のマスが存在しない (すなわち x + A_i が 4 以上になる) 駒たちに関しては、それらを取り除いて P に取り除いた個数を加算する。

すべての操作を行った後の P の値を出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 4
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \dots A_N

出力

操作終了時点での P の値を出力せよ。


入力例 1

4
1 1 3 2

出力例 1

3

操作を説明すると次のようになり、操作終了時点での P の値は 3 になります。

  • i=1 での操作
    1. マス 0 に駒を置く。この時点でマス 0 にコマが乗っている。
    2. すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1 に駒が乗っている。
  • i=2 での操作
    1. マス 0 に駒を置く。この時点でマス 0, 1 にコマが乗っている。
    2. すべての駒を 1 大きいマスに進める。移動を終えた時点でマス 1, 2 に駒が乗っている。
  • i=3 での操作
    1. マス 0 に駒を置く。この時点でマス 0, 1, 2 にコマが乗っている。
    2. すべての駒を 3 大きいマスに進める。
      この時、マス 1,2 にある駒は移動先のマスが存在しないため (それぞれ 1+3=4,2+3=5 なので) 、盤上から取り除いて P に 2 を加算する。P の値は 2 になる。
      移動を終えた時点でマス 3 に駒が乗っている。
  • i=4 での操作
    1. マス 0 に駒を置く。この時点でマス 0, 3 にコマが乗っている。
    2. すべての駒を 2 大きいマスに進める。
      この時、マス 3 にある駒は移動先のマスが存在しないため (3+2=5 なので) 、盤上から取り除いて P に 1 を加算する。P の値は 3 になる。
      移動を終えた時点でマス 2 に駒が乗っている。

入力例 2

3
1 1 1

出力例 2

0

P の値が操作中に変化しない場合もあります。


入力例 3

10
2 2 4 1 1 1 4 2 2 1

出力例 3

8

Score : 200 points

Problem Statement

Takahashi is trying to create a game inspired by baseball, but he is having difficulty writing the code.
Write a program for Takahashi that solves the following problem.

There are 4 squares called Square 0, Square 1, Square 2, and Square 3. Initially, all squares are empty.
There is also an integer P; initially, P = 0.
Given a sequence of positive integers A = (A_1, A_2, \dots, A_N), perform the following operations for i = 1, 2, \dots, N in this order:

  1. Put a piece on Square 0.
  2. Advance every piece on the squares A_i squares ahead. In other words, if Square x has a piece, move the piece to Square (x + A_i).
    If, however, the destination square does not exist (i.e. x + A_i is greater than or equal to 4) for a piece, remove it. Add to P the number of pieces that have been removed.

Print the value of P after all the operations have been performed.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 4
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the value of P after all the operations have been performed.


Sample Input 1

4
1 1 3 2

Sample Output 1

3

The operations are described below. After all the operations have been performed, P equals 3.

  • The operations for i=1:
    1. Put a piece on Square 0. Now, Square 0 has a piece.
    2. Advance every piece on the squares 1 square ahead. After these moves, Square 1 has a piece.
  • The operations for i=2:
    1. Put a piece on Square 0. Now, Squares 0 and 1 have a piece.
    2. Advance every piece on the squares 1 square ahead. After these moves, Squares 1 and 2 have a piece.
  • The operations for i=3:
    1. Put a piece on Square 0. Now, Squares 0, 1, and 2 have a piece.
    2. Advance every piece on the squares 3 squares ahead.
      Here, for the pieces on Squares 1 and 2, the destination squares do not exist (since 1+3=4 and 2+3=5), so remove these pieces and add 2 to P. P now equals 2. After these moves, Square 3 has a piece.
  • The operations for i=4:
    1. Put a piece on Square 0. Now, Squares 0 and 3 have a piece.
    2. Advance every piece on the squares 2 squares ahead.
      Here, for the piece on Square 3, the destination square does not exist (since 3+2=5), so remove this piece and add 1 to P. P now equals 3.
      After these moves, Square 2 has a piece.

Sample Input 2

3
1 1 1

Sample Output 2

0

The value of P may not be updated by the operations.


Sample Input 3

10
2 2 4 1 1 1 4 2 2 1

Sample Output 3

8
D - Cat

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

配点 : 200 点

問題文

長さ N の文字列 S が与えられます。

S に連続して含まれる na を全て nya に置き換えて得られる文字列を答えてください。

制約

  • N は 1 以上 1000 以下の整数
  • S は英小文字からなる長さ N の文字列

入力

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

N
S

出力

答えを出力せよ。


入力例 1

4
naan

出力例 1

nyaan

naan に連続して含まれる na を全て nya に置き換えて得られる文字列は nyaan です。


入力例 2

4
near

出力例 2

near

S に na が連続して含まれないこともあります。


入力例 3

8
national

出力例 3

nyationyal

Score : 200 points

Problem Statement

You are given a string S of length N.

Find the string obtained by replacing all contiguous occurrences of na in S with nya.

Constraints

  • N is an integer between 1 and 1000, inclusive.
  • S is a string of length N consisting of lowercase English letters.

Input

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

N
S

Output

Print the answer.


Sample Input 1

4
naan

Sample Output 1

nyaan

Replacing all contiguous occurrences of na in naan with nya results in the string nyaan.


Sample Input 2

4
near

Sample Output 2

near

S may not contain a contiguous na.


Sample Input 3

8
national

Sample Output 3

nyationyal
E - Previous Permutation

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

配点 : 300 点

問題文

(1, \dots, N) の順列 P = (P_1, \dots, P_N) が与えられます。ただし、(P_1, \dots, P_N) \neq (1, \dots, N) です。

(1 \dots, N) の順列を全て辞書順で小さい順に並べたとき、P が K 番目であるとします。辞書順で小さい方から K-1 番目の順列を求めてください。

順列とは?

(1, \dots, N) の順列とは、(1, \dots, N) を並べ替えて得られる数列のことをいいます。

辞書順とは?

長さ N の数列 A = (A_1, \dots, A_N), B = (B_1, \dots, B_N) に対し、A が B より辞書順で真に小さいとは、ある整数 1 \leq i \leq N が存在して、下記の 2 つがともに成り立つことをいいます。

  • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})
  • A_i < B_i

制約

  • 2 \leq N \leq 100
  • 1 \leq P_i \leq N \, (1 \leq i \leq N)
  • P_i \neq P_j \, (i \neq j)
  • (P_1, \dots, P_N) \neq (1, \dots, N)
  • 入力される値は全て整数

入力

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

N
P_1 \ldots P_N

出力

求める順列を Q = (Q_1, \dots, Q_N) として、Q_1, \dots, Q_N をこの順に空白区切りで一行に出力せよ。


入力例 1

3
3 1 2

出力例 1

2 3 1

(1, 2, 3) の順列を辞書順で小さい順に並べると次のようになります。

  • (1, 2, 3)
  • (1, 3, 2)
  • (2, 1, 3)
  • (2, 3, 1)
  • (3, 1, 2)
  • (3, 2, 1)

よって P = (3, 1, 2) は小さい方から 5 番目であり、求める順列、すなわち小さい方から 5 - 1 = 4 番目の順列は (2, 3, 1) です。


入力例 2

10
9 8 6 5 10 3 1 2 4 7

出力例 2

9 8 6 5 10 2 7 4 3 1

Score : 300 points

Problem Statement

You are given a permutation P = (P_1, \dots, P_N) of (1, \dots, N), where (P_1, \dots, P_N) \neq (1, \dots, N).

Assume that P is the K-th lexicographically smallest among all permutations of (1 \dots, N). Find the (K-1)-th lexicographically smallest permutation.

What are permutations?

A permutation of (1, \dots, N) is an arrangement of (1, \dots, N) into a sequence.

What is lexicographical order?

For sequences of length N, A = (A_1, \dots, A_N) and B = (B_1, \dots, B_N), A is said to be strictly lexicographically smaller than B if and only if there is an integer 1 \leq i \leq N that satisfies both of the following.

  • (A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1}).
  • A_i < B_i.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq P_i \leq N \, (1 \leq i \leq N)
  • P_i \neq P_j \, (i \neq j)
  • (P_1, \dots, P_N) \neq (1, \dots, N)
  • All values in the input are integers.

Input

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

N
P_1 \ldots P_N

Output

Let Q = (Q_1, \dots, Q_N) be the sought permutation. Print Q_1, \dots, Q_N in a single line in this order, separated by spaces.


Sample Input 1

3
3 1 2

Sample Output 1

2 3 1

Here are the permutations of (1, 2, 3) in ascending lexicographical order.

  • (1, 2, 3)
  • (1, 3, 2)
  • (2, 1, 3)
  • (2, 3, 1)
  • (3, 1, 2)
  • (3, 2, 1)

Therefore, P = (3, 1, 2) is the fifth smallest, so the sought permutation, which is the fourth smallest (5 - 1 = 4), is (2, 3, 1).


Sample Input 2

10
9 8 6 5 10 3 1 2 4 7

Sample Output 2

9 8 6 5 10 2 7 4 3 1
F - Make Them Narrow

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

配点 : 250 点

問題文

長さ N の数列 A が与えられます。
A のうち丁度 K 要素を自由に選んで消し、残った要素を順序を保って連結した数列を B とします。
( B の最大値 ) - ( B の最小値 ) としてありうる最小値を求めてください。

制約

  • 入力は全て整数
  • 1 \le K < N \le 2 \times 10^5
  • 1 \le A_i \le 10^9

入力

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

N K
A_1 A_2 \dots A_N

出力

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


入力例 1

5 2
3 1 5 4 9

出力例 1

2

A=(3,1,5,4,9) から丁度 2 要素を自由に選んで消すことを考えます。

  • 例えば 2 要素目の 1 、 5 要素目の 9 を消すと、消した後の数列 B=(3,5,4) となります。
    • このとき B の最大値は 5 、最小値は 3 なので ( B の最大値 ) - ( B の最小値 ) =2 となり、これは達成可能な最小値です。

入力例 2

6 5
1 1 1 1 1 1

出力例 2

0

入力例 3

8 3
31 43 26 6 18 36 22 13

出力例 3

18

Score : 250 points

Problem Statement

You are given a sequence A of length N.
Freely choose exactly K elements from A and remove them, then concatenate the remaining elements in their original order to form a new sequence B.
Find the minimum possible value of this: the maximum value of B minus the minimum value of B.

Constraints

  • All inputs are integers.
  • 1 \le K < N \le 2 \times 10^5
  • 1 \le A_i \le 10^9

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer as an integer.


Sample Input 1

5 2
3 1 5 4 9

Sample Output 1

2

Consider removing exactly two elements from A=(3,1,5,4,9).

  • For example, if you remove the 2nd element 1 and the 5th element 9, the resulting sequence is B=(3,5,4).
    • In this case, the maximum value of B is 5 and the minimum value is 3, so (maximum value of B) - (minimum value of B) =2, which is the minimum possible value.

Sample Input 2

6 5
1 1 1 1 1 1

Sample Output 2

0

Sample Input 3

8 3
31 43 26 6 18 36 22 13

Sample Output 3

18
G - Cylinder

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

配点 : 400 点

問題文

空の筒があります。Q 個のクエリが与えられるので順に処理してください。
クエリは次の 2 種類のいずれかです。

  • 1 x c:数 x が書かれたボールを筒の右側から c 個入れる
  • 2 c:筒の左側からボールを c 個取り出し、取り出したボールに書かれている数の合計を出力する

なお、筒の中でボールの順序が入れ替わることはないものとします。

制約

  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq x \leq 10^9
  • 1 \leq c \leq 10^9
  • 2 c のクエリが与えられるとき、筒の中には c 個以上のボールがある
  • 入力に含まれる値は全て整数である

入力

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

Q
{\rm query}_1
\vdots
{\rm query}_Q

i 番目のクエリを表す {\rm query}_i は以下の 2 種類のいずれかである。

1 x c
2 c

出力

2 c のクエリに対する答えを順に改行区切りで出力せよ。


入力例 1

4
1 2 3
2 2
1 3 4
2 3

出力例 1

4
8
  • 1 番目のクエリでは、2 が書かれたボールを筒の右側から 3 個入れます。
    筒の中のボールに書かれた数は左から順に (2,2,2) となります。
  • 2 番目のクエリでは、筒の左側からボールを 2 個取り出します。
    取り出されたボールに書かれている数はそれぞれ 2,2 であり、合計は 4 であるため、これを出力します。 筒の中のボールに書かれた数は左から順に (2) となります。
  • 3 番目のクエリでは、3 が書かれたボールを筒の右側から 4 個入れます。
    筒の中のボールに書かれた数は左から順に (2,3,3,3,3) となります。
  • 4 番目のクエリでは、筒の左側からボールを 3 個取り出します。
    取り出されたボールに書かれている数はそれぞれ 2,3,3 であり、合計は 8 であるため、これを出力します。 筒の中のボールに書かれた数は左から順に (3,3) となります。

入力例 2

2
1 1000000000 1000000000
2 1000000000

出力例 2

1000000000000000000

入力例 3

5
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

出力例 3


出力するものがないこともあります。

Score : 400 points

Problem Statement

We have a horizontal cylinder. Given Q queries, process them in the given order.
Each query is of one of the following two types.

  • 1 x c: Insert c balls, with a number x written on each of them, to the right end of the cylinder.
  • 2 c: Take out the c leftmost balls contained in the cylinder and print the sum of the numbers written on the balls that have been taken out.

We assume that the balls do never change their order in the cylinder.

Constraints

  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq x \leq 10^9
  • 1 \leq c \leq 10^9
  • Whenever a query of type 2 c is given, there are c or more balls in the cylinder.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
{\rm query}_1
\vdots
{\rm query}_Q

The i-th query {\rm query}_i is in one of the following two formats.

1 x c
2 c

Output

Print the response to the queries of type 2 c in the given order, with newlines in between.


Sample Input 1

4
1 2 3
2 2
1 3 4
2 3

Sample Output 1

4
8
  • For the 1-st query, insert 3 balls, with a number 2 written on each of them, to the right end of the cylinder.
    The cylinder has now balls with numbers (2,2,2) written on them, from left to right.
  • For the 2-nd query, take out the 2 leftmost balls contained in the cylinder.
    The numbers written on the balls taken out are 2,2, for a sum of 4, which should be printed. The cylinder has now a ball with a number (2) written on it, from left to right.
  • For the 3-rd query, insert 4 balls, with a number 3 written on each of them, to the right end of the cylinder.
    The cylinder has now balls with numbers (2,3,3,3,3) written on them, from left to right.
  • For the 4-th query, take out the 3 leftmost balls contained in the cylinder.
    The numbers written on the balls taken out are 2,3,3, for a sum of 8, which should be printed. The cylinder has now balls with numbers (3,3) written on them, from left to right.

Sample Input 2

2
1 1000000000 1000000000
2 1000000000

Sample Output 2

1000000000000000000

Sample Input 3

5
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

Sample Output 3


There may be nothing you should print.

H - Swap Places

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

配点 : 500 点

問題文

頂点に 1 から N までの、辺に 1 から M までの番号がついた N 頂点 M 辺の単純無向グラフがあります。 辺 i は頂点 u_i と頂点 v_i を結んでいます。
また、全ての頂点は赤か青のいずれか一方で塗られています。頂点 i の色は C_i で表されて、C_i が 0 ならば頂点 i は赤く、1 ならば頂点 i は青く塗られています。

今、高橋君が頂点 1 に、青木君が頂点 N にいます。
2 人は次の行動を 0 回以上好きな回数繰り返します。

  • 2 人が同時に、今いる頂点に隣接している頂点のいずれか 1 個に移動する。
    ただし、高橋君の移動先の頂点の色と、青木君の移動先の頂点の色は異なる必要がある。

上記の行動を繰り返すことで、高橋君が頂点 N に、青木君が頂点 1 にいる状態にできますか?
可能である場合は必要な行動回数の最小値を答えてください。不可能である場合は -1 を出力してください。

入力のはじめに T が与えられるので、T 個のテストケースについて問題を解いてください。

制約

  • 1 \leq T \leq 1000
  • 2 \leq N \leq 2000
  • 1 \leq M \leq \min(\frac{N(N-1)}{2}, 2000)
  • C_i \in \lbrace 0, 1 \rbrace
  • 1 \leq u_i, v_i \leq N
  • 入力で与えられるグラフは単純
  • 入力される値は全て整数
  • 全てのテストケースに対する N の総和は 2000 を超えない。
  • 全てのテストケースに対する M の総和は 2000 を超えない。

入力

入力は以下の形式で標準入力から与えられる。ここで \text{test}_i は i 番目のテストケースを意味する。

T
\text{test}_1
\text{test}_2
\vdots
\text{test}_T

各テストケースは以下の形式で与えられる。

N M
C_1 C_2 \dots C_N
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

T 行出力せよ。i 行目には i 番目のテストケースに対する答えを出力せよ。
各テストケースでは、高橋君が頂点 N に、青木君が頂点 1 にいる状態にできる場合は必要な行動回数の最小値を、できない場合は -1 を出力せよ。


入力例 1

3
4 4
0 1 0 1
1 2
2 3
1 3
2 4
3 3
0 1 0
1 2
2 3
1 3
6 6
0 0 1 1 0 1
1 2
2 6
3 6
4 6
4 5
2 4

出力例 1

3
-1
3

1 番目のテストケースでは、高橋君と青木君は以下のように行動することで、 3 回の行動で目的の状態を達成することができて、これが最小です。

  • 高橋君が頂点 3 に、青木君が頂点 2 に移動する。
  • 高橋君が頂点 2 に、青木君が頂点 3 に移動する。
  • 高橋君が頂点 4 に、青木君が頂点 1 に移動する。

ここで、1 回目の移動で高橋君と青木君がともに頂点 2 に移動することはできないのに注意してください。(なぜならば、高橋君の移動先の頂点の色と青木君の移動先の頂点の色は異なる必要があるからです。)

2 番目のテストケースでは、2 人はどのように行動しても目的の状態を達成することはできません。

Score : 500 points

Problem Statement

There is a simple undirected graph with N vertices numbered 1 through N and M edges numbered 1 through M. Edge i connects vertex u_i and vertex v_i.
Every vertex is painted either red or blue. The color of vertex i is represented by C_i; vertex i is painted red if C_i is 0 and blue if C_i is 1.

Now, Takahashi is on vertex 1 and Aoki is on vertex N.
They may repeat the following move zero or more times.

  • Each of the two simultaneously moves to a vertex adjacent to the current vertex.
    Here, the vertices that Takahashi and Aoki move to must have different colors.

By repeating the move above, can Takahashi and Aoki simultaneously end up on vertices N and 1, respectively?
If it is possible, find the minimum number of moves required. If it is impossible, print -1.

You are given T at the beginning of the input. Solve the problem for T test cases.

Constraints

  • 1 \leq T \leq 1000
  • 2 \leq N \leq 2000
  • 1 \leq M \leq \min(\frac{N(N-1)}{2}, 2000)
  • C_i \in \lbrace 0, 1 \rbrace
  • 1 \leq u_i, v_i \leq N
  • The graph given in the input is simple.
  • All values in the input are integers.
  • The sum of N over all test cases does not exceed 2000.
  • The sum of M over all test cases does not exceed 2000.

Input

The input is given from Standard Input in the following format, where \text{test}_i denotes the i-th test case:

T
\text{test}_1
\text{test}_2
\vdots
\text{test}_T

Each test case is given in the following format:

N M
C_1 C_2 \dots C_N
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

Print T lines. The i-th line should contain the answer to the i-th test case.
For each test case, print the minimum number of moves required for Takahashi and Aoki to simultaneously end up in vertices N and 1, respectively, if it is possible, and -1 otherwise.


Sample Input 1

3
4 4
0 1 0 1
1 2
2 3
1 3
2 4
3 3
0 1 0
1 2
2 3
1 3
6 6
0 0 1 1 0 1
1 2
2 6
3 6
4 6
4 5
2 4

Sample Output 1

3
-1
3

For the 1-st test case, Takahashi and Aoki can achieve the objective by making the following 3 moves, which is the minimum number:

  • Takahashi moves to vertex 3, and Aoki moves to vertex 2.
  • Takahashi moves to vertex 2, and Aoki moves to vertex 3.
  • Takahashi moves to vertex 4, and Aoki moves to vertex 1.

Note that in the 1-st move, it is disallowed that both Takahashi and Aoki move to vertex 2 (because the colors of vertices that Takahashi and Aoki move to must be different.)

For the 2-nd case, no matter how they move, they cannot achieve the objective.

I - Oddly Similar

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

配点 : 550 点

問題文

N 個の長さ M の数列 A_1, A_2, \ldots, A_N があります。i 番目の数列は M 個の整数 A_{i,1}, A_{i,2}, \ldots, A_{i,M} で表されます。

それぞれの長さが M の数列 X,Y について、X_i = Y_i となるような i(1 \leq i \leq M) の個数が奇数であるときに、X と Y は似ていると言います。

1 \leq i < j \leq N を満たす整数の組 (i,j) のうち、A_i と A_j が似ているものの個数を求めてください。

制約

  • 1 \leq N \leq 2000
  • 1 \leq M \leq 2000
  • 1 \leq A_{i,j} \leq 999
  • 入力は全て整数である。

入力

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

N M
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}

出力

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


入力例 1

3 3
1 2 3
1 3 4
2 3 4

出力例 1

1

(i,j) = (1,2) は条件を満たします。なぜならば、A_{1,k} = A_{2,k} となるような k は k=1 の 1 個だけだからです。

(i,j) = (1,3) ,(2,3) は条件を満たさないため、条件を満たす (i,j) の組は (1,2) だけです。


入力例 2

6 5
8 27 27 10 24
27 8 2 4 5
15 27 26 17 24
27 27 27 27 27
27 7 22 11 27
19 27 27 27 27

出力例 2

5

Score: 550 points

Problem Statement

There are N sequences of length M, denoted as A_1, A_2, \ldots, A_N. The i-th sequence is represented by M integers A_{i,1}, A_{i,2}, \ldots, A_{i,M}.

Two sequences X and Y of length M are said to be similar if and only if the number of indices i (1 \leq i \leq M) such that X_i = Y_i is odd.

Find the number of pairs of integers (i,j) satisfying 1 \leq i < j \leq N such that A_i and A_j are similar.

Constraints

  • 1 \leq N \leq 2000
  • 1 \leq M \leq 2000
  • 1 \leq A_{i,j} \leq 999
  • All input values are integers.

Input

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

N M
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}

Output

Print the answer as an integer.


Sample Input 1

3 3
1 2 3
1 3 4
2 3 4

Sample Output 1

1

The pair (i,j) = (1,2) satisfies the condition because there is only one index k such that A_{1,k} = A_{2,k}, which is k=1.

The pairs (i,j) = (1,3), (2,3) do not satisfy the condition, making (1,2) the only pair that does.


Sample Input 2

6 5
8 27 27 10 24
27 8 2 4 5
15 27 26 17 24
27 27 27 27 27
27 7 22 11 27
19 27 27 27 27

Sample Output 2

5