Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
0
と 1
からなる長さ 16 の文字列 S が与えられます。
2 以上 16 以下のすべての偶数 i について S の i 文字目が 0
ならば Yes
を、
そうでないならば No
を出力してください。
制約
- S は
0
と1
からなる長さ 16 の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
2 以上 16 以下のすべての偶数 i について S の i 文字目が 0
ならば Yes
を、
そうでないならば No
を出力せよ。
入力例 1
1001000000001010
出力例 1
No
S= 1001000000001010
の 4 文字目が 1
であるため、No
を出力します。
入力例 2
1010100000101000
出力例 2
Yes
S= 1010100000101000
の偶数番目の文字はすべて 0
であるため、Yes
を出力します。
入力例 3
1111111111111111
出力例 3
No
S の偶数文字目はすべて 1
となっています。
特に「すべて 0
」ではないため、No
を出力します。
Score : 100 points
Problem Statement
You are given a string S of length 16 consisting of 0
and 1
.
If the i-th character of S is 0
for every even number i from 2 through 16, print Yes
; otherwise, print No
.
Constraints
- S is a string of length 16 consisting of
0
and1
.
Input
The input is given from Standard Input in the following format:
S
Output
If the i-th character of S is 0
for every even number i from 2 through 16, print Yes
; otherwise, print No
.
Sample Input 1
1001000000001010
Sample Output 1
No
The 4-th character of S= 1001000000001010
is 1
, so you should print No
.
Sample Input 2
1010100000101000
Sample Output 2
Yes
Every even-positioned character in S= 1010100000101000
is 0
, so you should print Yes
.
Sample Input 3
1111111111111111
Sample Output 3
No
Every even-positioned character in S is 1
.
Particularly, they are not all 0
, so you should print No
.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
初項が A、末項が B、公差が D であるような等差数列を出力してください。
なお、そのような等差数列が存在する入力のみが与えられます。
制約
- 1 \leq A \leq B \leq 100
- 1\leq D \leq 100
- 初項が A、末項が B、公差が D であるような等差数列が存在する
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
A B D
出力
初項が A、末項が B、公差が D であるような等差数列の項を順に空白区切りで出力せよ。
入力例 1
3 9 2
出力例 1
3 5 7 9
初項が 3、末項が 9、公差が 2 であるような等差数列は (3,5,7,9) です。
入力例 2
10 10 1
出力例 2
10
初項が 10、末項が 10、公差が 1 であるような等差数列は (10) です。
Score: 100 points
Problem Statement
Print an arithmetic sequence with first term A, last term B, and common difference D.
You are only given inputs for which such an arithmetic sequence exists.
Constraints
- 1 \leq A \leq B \leq 100
- 1 \leq D \leq 100
- There is an arithmetic sequence with first term A, last term B, and common difference D.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A B D
Output
Print the terms of the arithmetic sequence with first term A, last term B, and common difference D, in order, separated by spaces.
Sample Input 1
3 9 2
Sample Output 1
3 5 7 9
The arithmetic sequence with first term 3, last term 9, and common difference 2 is (3,5,7,9).
Sample Input 2
10 10 1
Sample Output 2
10
The arithmetic sequence with first term 10, last term 10, and common difference 1 is (10).
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
英小文字からなる文字列 S が与えられます。S に最も多く出現する文字を求めてください。そのような文字が複数ある場合は、そのうちアルファベット順で最も早いものを答えてください。
制約
- 1 \leq |S| \leq 1000(|S| は文字列 S の長さ)
- S の各文字は英小文字である。
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S に最も多く出現する文字のうちアルファベット順で最も早いものを出力せよ。
入力例 1
frequency
出力例 1
e
frequency
には e
が 2 回出現し、これは他のどの文字よりも多いため e
を出力します。
入力例 2
atcoder
出力例 2
a
atcoder
には a
, t
, c
, o
, d
, e
, r
が 1 回ずつ出現するため、このうちアルファベット順で最も早い a
を出力します。
入力例 3
pseudopseudohypoparathyroidism
出力例 3
o
Score: 200 points
Problem Statement
You are given a string S consisting of lowercase English letters. Find the character that appears most frequently in S. If multiple such characters exist, report the one that comes earliest in alphabetical order.
Constraints
- 1 \leq |S| \leq 1000 (|S| is the length of the string S.)
- Each character in S is a lowercase English letter.
Input
The input is given from Standard Input in the following format:
S
Output
Among the characters that appear most frequently in S, print the one that comes earliest in alphabetical order.
Sample Input 1
frequency
Sample Output 1
e
In frequency
, the letter e
appears twice, which is more than any other character, so you should print e
.
Sample Input 2
atcoder
Sample Output 2
a
In atcoder
, each of the letters a
, t
, c
, o
, d
, e
, and r
appears once, so you should print the earliest in alphabetical order, which is a
.
Sample Input 3
pseudopseudohypoparathyroidism
Sample Output 3
o
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 A,B が与えられます。
A+B を(十進法で)計算する時、繰り上がりが生じないなら Easy
、生じるなら Hard
と出力してください。
制約
- A,B は整数
- 1 \le A,B \le 10^{18}
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
繰り上がりが生じないなら Easy
、生じるなら Hard
と出力せよ。
入力例 1
229 390
出力例 1
Hard
229+390 を計算する際、十の位から百の位へと繰り上がりが発生します。よって、答えは Hard
です。
入力例 2
123456789 9876543210
出力例 2
Easy
繰り上がりは発生しません。答えは Easy
です。
また、入力が 32bit 整数に収まらないこともあります。
Score : 200 points
Problem Statement
You are given positive integers A and B.
Let us calculate A+B (in decimal). If it does not involve a carry, print Easy
; if it does, print Hard
.
Constraints
- A and B are integers.
- 1 \le A,B \le 10^{18}
Input
Input is given from Standard Input in the following format:
A B
Output
If the calculation does not involve a carry, print Easy
; if it does, print Hard
.
Sample Input 1
229 390
Sample Output 1
Hard
When calculating 229+390, we have a carry from the tens digit to the hundreds digit, so the answer is Hard
.
Sample Input 2
123456789 9876543210
Sample Output 2
Easy
We do not have a carry here; the answer is Easy
.
Note that the input may not fit into a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字のみからなる長さ N の文字列 S が与えられます。
S の文字を並び替えて得られる文字列(S 自身を含む)であって、長さ K の回文を部分文字列として 含まない ものの個数を求めてください。
ただし、長さ N の文字列 T が「長さ K の回文を部分文字列として含む」とは、
ある (N-K) 以下の非負整数 i が存在して、1 以上 K 以下の任意の整数 j について T_{i+j}=T_{i+K+1-j} が成り立つことをいいます。
ここで、T_k は文字列 T の k 文字目を表すものとします。
制約
- 2\leq K \leq N \leq 10
- N,K は整数
- S は英小文字のみからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N K S
出力
S の文字を並び替えて得られる文字列であって、長さ K の回文を部分文字列として含まないものの個数を出力せよ。
入力例 1
3 2 aab
出力例 1
1
aab
を並び替えて得られる文字列は aab
, aba
, baa
の 3 つであり、このうち aab
および baa
は長さ 2 の回文 aa
を部分文字列として含んでいます。
よって、条件をみたす文字列は aba
のみであり、1 を出力します。
入力例 2
5 3 zzyyx
出力例 2
16
zzyyx
を並べて得られる文字列は 30 個ありますが、そのうち長さ 3 の回文を含まないようなものは 16 個です。よって、16 を出力します。
入力例 3
10 5 abcwxyzyxw
出力例 3
440640
Score : 300 points
Problem Statement
You are given a string S of length N consisting only of lowercase English letters.
Find the number of strings obtained by permuting the characters of S (including the string S itself) that do not contain a palindrome of length K as a substring.
Here, a string T of length N is said to "contain a palindrome of length K as a substring" if and only if there exists a non-negative integer i not greater than (N-K) such that T_{i+j} = T_{i+K+1-j} for every integer j with 1 \leq j \leq K.
Here, T_k denotes the k-th character of the string T.
Constraints
- 2 \leq K \leq N \leq 10
- N and K are integers.
- S is a string of length N consisting only of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N K S
Output
Print the number of strings obtained by permuting S that do not contain a palindrome of length K as a substring.
Sample Input 1
3 2 aab
Sample Output 1
1
The strings obtained by permuting aab
are aab
, aba
, and baa
. Among these, aab
and baa
contain the palindrome aa
of length 2 as a substring.
Thus, the only string that satisfies the condition is aba
, so print 1.
Sample Input 2
5 3 zzyyx
Sample Output 2
16
There are 30 strings obtained by permuting zzyyx
, 16 of which do not contain a palindrome of length 3. Thus, print 16.
Sample Input 3
10 5 abcwxyzyxw
Sample Output 3
440640
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
十進法ですべての桁の数字が 1 である整数をレピュニットと呼びます。レピュニットを小さい順に並べると 1,11,111,\ldots です。
ちょうど 3 つのレピュニットの和として表せる整数のうち N 番目に小さいものを求めてください。
制約
- N は 1 以上 333 以下の整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
5
出力例 1
113
ちょうど 3 つのレピュニットの和として表せる整数を小さい順に並べると 3,13,23,33,113,\ldots です。例えば 113 は 113=1+1+111 と表せます。
3 つのレピュニットは相異ならなくてもよいことに注意してください。
入力例 2
19
出力例 2
2333
入力例 3
333
出力例 3
112222222233
Score : 300 points
Problem Statement
A repunit is an integer whose digits are all 1 in decimal representation. The repunits in ascending order are 1, 11, 111, \ldots.
Find the N-th smallest integer that can be expressed as the sum of exactly three repunits.
Constraints
- N is an integer between 1 and 333, inclusive.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
5
Sample Output 1
113
The integers that can be expressed as the sum of exactly three repunits are 3, 13, 23, 33, 113, \ldots in ascending order. For example, 113 can be expressed as 113 = 1 + 1 + 111.
Note that the three repunits do not have to be distinct.
Sample Input 2
19
Sample Output 2
2333
Sample Input 3
333
Sample Output 3
112222222233
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
2N 個のボールがあります。各ボールには 1 以上 N 以下の整数によって表される色が塗られており、各色で塗られたボールはちょうど 2 個ずつ存在します。
これらのボールが、底が地面と平行になるように置かれた M 本の筒に入れられています。はじめ、i \ (1 \leq i \leq M) 本目の筒には k_i 個のボールが入っており、上から j \ (1 \leq j \leq k_i) 番目のボールの色は a_{i, j} です。
あなたの目標は、以下の操作を繰り返すことで M 本の筒全てを空にすることです。
- 異なる 2 本の空でない筒を選び、それぞれの筒の一番上にあるボールを取り出して捨てる。ここで、取り出して捨てた 2 つのボールは同じ色で塗られている必要がある。
目標が達成可能かを判定してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 2 \leq M \leq 2 \times 10^5
- 1 \leq k_i\ (1 \leq i \leq M)
- 1 \leq a_{i,j} \leq N\ (1 \leq i \leq M,1 \leq j \leq k_i)
- \sum_{i=1}^{M} k_i = 2N
- 全ての x\ (1 \leq x \leq N) について、1 \leq i \leq M かつ 1 \leq j \leq k_i かつ a_{i,j}=x なる整数の組 (i,j) はちょうど 2 つ存在する
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M k_1 a_{1,1} a_{1,2} \ldots a_{1,k_1} k_2 a_{2,1} a_{2,2} \ldots a_{2,k_2} \hspace{2.1cm}\vdots k_M a_{M,1} a_{M,2} \ldots a_{M,k_M}
出力
目標が達成可能なら Yes
を、達成不可能なら No
を出力せよ。
入力例 1
2 2 2 1 2 2 1 2
出力例 1
Yes
以下のように操作を行えばよいです。
- 1 つ目の筒と 2 つ目の筒を選び、それぞれの筒の一番上にあるボールを取り出して捨てる。捨てられるボールの色は共に 1 であり等しいので、この操作は有効である。
- 1 つ目の筒と 2 つ目の筒を選び、それぞれの筒の一番上にあるボールを取り出して捨てる。捨てられるボールの色は共に 2 であり等しいので、この操作は有効である。
入力例 2
2 2 2 1 2 2 2 1
出力例 2
No
そもそも一度も操作を行うことができないため、目標を達成する、すなわち M 本の筒全てを空にすることは不可能です。
Score : 400 points
Problem Statement
We have 2N balls. Each ball has a color represented by an integer between 1 and N (inclusive). For each of the N colors, there are exactly two balls of that color.
These balls are contained in M cylinders placed perpendicularly to the floor. Initially, the i-th cylinder (1 \leq i \leq M) contains k_i balls, the j-th of which from the top (1 \leq j \leq k_i) has the color a_{i, j}.
Your objective is to empty all M cylinders by repeating the following operation.
- Choose two different non-empty cylinders and remove the topmost ball from each of them. Here, the two balls removed must be of the same color.
Determine whether the objective is achievable.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 2 \leq M \leq 2 \times 10^5
- 1 \leq k_i\ (1 \leq i \leq M)
- 1 \leq a_{i,j} \leq N\ (1 \leq i \leq M,1 \leq j \leq k_i)
- \sum_{i=1}^{M} k_i = 2N
- For every x\ (1 \leq x \leq N), there exists exactly two pairs of integers (i,j) such that 1 \leq i \leq M, 1 \leq j \leq k_i, and a_{i,j}=x.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M k_1 a_{1,1} a_{1,2} \ldots a_{1,k_1} k_2 a_{2,1} a_{2,2} \ldots a_{2,k_2} \hspace{2.1cm}\vdots k_M a_{M,1} a_{M,2} \ldots a_{M,k_M}
Output
If the objective is achievable, print Yes
; otherwise, print No
.
Sample Input 1
2 2 2 1 2 2 1 2
Sample Output 1
Yes
The objective can be achieved as follows.
- Choose the first and second cylinders to remove the topmost ball from each of them, which is allowed since the removed balls have the same color: 1.
- Choose the first and second cylinders to remove the topmost ball from each of them, which is allowed since the removed balls have the same color: 2.
Sample Input 2
2 2 2 1 2 2 2 1
Sample Output 2
No
No operation can be done at all, which means it is impossible to achieve the objective of emptying the M cylinders.
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
0 以上 M 以下の整数からなる長さ N の数列 A=(A_1,A_2,\dots,A_N) があります。
今からすぬけくんが以下の操作 1, 2 を順に行います。
- A_i=0 を満たすそれぞれの i について、1 以上 M 以下の整数を独立かつ一様ランダムに選び、A_i をその整数で置き換える。
- A を昇順に並び替える。
すぬけくんが操作 1, 2 を行ったあとの A_K の期待値を \text{mod } 998244353 で出力してください。
「期待値を \text{mod } 998244353 で出力」とは
求める期待値は必ず有理数となることが証明できます。 またこの問題の制約下では、その値を互いに素な 2 つの整数 P, Q を用いて \frac{P}{Q} と表したとき、 R \times Q \equiv P\pmod{998244353} かつ 0 \leq R \lt 998244353 を満たす整数 R がただ 1 つ存在することが証明できます。この R を出力してください。制約
- 1\leq K \leq N \leq 2000
- 1\leq M \leq 2000
- 0\leq A_i \leq M
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K A_1 A_2 \dots A_N
出力
すぬけくんが操作 1, 2 を行ったあとの A_K の期待値を \text{mod } 998244353 で出力せよ。
入力例 1
3 5 2 2 0 4
出力例 1
3
すぬけくんは操作 1 において A_2 を 1 以上 5 以下の整数で置き換えます。この整数を x とすると、
- x=1,2 のとき、すぬけくんが操作 1, 2 を行ったあと A_2=2 です。
- x=3 のとき、すぬけくんが操作 1, 2 を行ったあと A_2=3 です。
- x=4,5 のとき、すぬけくんが操作 1, 2 を行ったあと A_2=4 です。
よって、A_2 の期待値は \frac{2+2+3+4+4}{5}=3 です。
入力例 2
2 3 1 0 0
出力例 2
221832080
期待値は \frac{14}{9} です。
入力例 3
10 20 7 6 5 0 2 0 0 0 15 0 0
出力例 3
617586310
Score : 500 points
Problem Statement
We have a sequence of length N consisting of integers between 0 and M, inclusive: A=(A_1,A_2,\dots,A_N).
Snuke will perform the following operations 1 and 2 in order.
- For each i such that A_i=0, independently choose a uniform random integer between 1 and M, inclusive, and replace A_i with that integer.
- Sort A in ascending order.
Print the expected value of A_K after the two operations, modulo 998244353.
How to print a number modulo 998244353?
It can be proved that the sought expected value is always rational. Additionally, under the Constraints of this problem, when that value is represented as \frac{P}{Q} using two coprime integers P and Q, it can be proved that there is a unique integer R such that R \times Q \equiv P\pmod{998244353} and 0 \leq R \lt 998244353. Print this R.Constraints
- 1\leq K \leq N \leq 2000
- 1\leq M \leq 2000
- 0\leq A_i \leq M
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M K A_1 A_2 \dots A_N
Output
Print the expected value of A_K after the two operations, modulo 998244353.
Sample Input 1
3 5 2 2 0 4
Sample Output 1
3
In the operation 1, Snuke will replace A_2 with an integer between 1 and 5. Let x be this integer.
- If x=1 or 2, we will have A_2=2 after the two operations.
- If x=3, we will have A_2=3 after the two operations.
- If x=4 or 5, we will have A_2=4 after the two operations.
Thus, the expected value of A_2 is \frac{2+2+3+4+4}{5}=3.
Sample Input 2
2 3 1 0 0
Sample Output 2
221832080
The expected value is \frac{14}{9}.
Sample Input 3
10 20 7 6 5 0 2 0 0 0 15 0 0
Sample Output 3
617586310
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 個の都市があり、都市 1, 都市 2, \ldots, 都市 N と番号づけられています。
いくつかの異なる都市の間は一方通行のテレポーターによって移動できます。
都市 i (1\leq i\leq N) からテレポーターによって直接移動できる都市は 0
と 1
からなる長さ M の文字列 S_i によって表されます。具体的には、1\leq j\leq N に対して、
- 1\leq j-i\leq M かつ S_i の (j-i) 文字目が
1
ならば、都市 i から都市 j に直接移動できる。 - そうでない時、都市 i から都市 j へは直接移動できない。
k=2,3,\ldots, N-1 に対して次の問題を解いてください。
テレポータを繰り返し使用することによって、都市 k を通らずに都市 1 から 都市 N へ移動できるか判定し、 できるならばそのために必要なテレポーターの使用回数の最小値を、 できないならば -1 を出力せよ。
制約
- 3 \leq N \leq 10^5
- 1\leq M\leq 10
- M<N
- S_i は
0
と1
のみからなる長さ M の文字列 - i+j>N ならば S_i の j 文字目は
0
- N,M は整数
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
N-2 個の整数を空白区切りで一行に出力せよ。 i (1\leq i\leq N-2) 番目には、k=i+1 に対する問題の答えを出力せよ。
入力例 1
5 2 11 01 11 10 00
出力例 1
2 3 2
テレポータによって各都市からはそれぞれ以下の都市へ直接移動する事ができます。
- 都市 1 からは都市 2,3 へ移動できる。
- 都市 2 からは都市 4 へ移動できる。
- 都市 3 からは都市 4,5 へ移動できる。
- 都市 4 からは都市 5 へ移動できる。
- 都市 5 から移動できる都市は存在しない。
よって、都市 1 から都市 5 へ移動する方法は、
- 経路 1 : 都市 1 \to 都市 2 \to 都市 4 \to 都市 5
- 経路 2 : 都市 1 \to 都市 3 \to 都市 4 \to 都市 5
- 経路 3 : 都市 1 \to 都市 3 \to 都市 5
の 3 つがあり、
- 都市 2 を通らない経路は経路 2, 経路 3 の 2つであり、そのうちテレポーターの使用回数が最小となるのは経路 3 で、この時 2 回使用する。
- 都市 3 を通らない経路は経路 1 のみであり、この時テレポーターは 3 回使用する。
- 都市 4 を通らない経路は経路 3 のみであり、この時テレポーターは 2 回使用する。
となります。よって、2,3,2 をこの順に空白区切りで出力します。
入力例 2
6 3 101 001 101 000 100 000
出力例 2
-1 3 3 -1
都市 1 から都市 6 へ移動する方法は、都市 1 \to 都市 2 \to 都市 5 \to 都市 6 のみであるため、
k=2,5 の場合には都市 k を通らずに都市 1 から都市 6 へ移動する方法は存在せず、
k=3,4 の場合には上の方法が条件をみたし、テレポーターを 3 回使用します。
よって、-1,3,3,-1 をこの順に空白区切りで出力します。
テレポーターは一方通行であるため、
都市 3 から都市 4 へはテレポーターによって移動できますが、
都市 4 から都市 3 へは移動できず、
都市 1 \to 都市 4 \to 都市 3 \to 都市 6
のような移動はできない事に注意してください。
Score : 500 points
Problem Statement
There are N cities numbered city 1, city 2, \ldots, and city N.
There are also one-way teleporters that send you to different cities.
Whether a teleporter can send you directly from city i (1\leq i\leq N) to another is represented by a length-M string S_i consisting of 0
and 1
. Specifically, for 1\leq j\leq N,
- if 1\leq j-i\leq M and the (j-i)-th character of S_i is
1
, then a teleporter can send you directly from city i to city j; - otherwise, it cannot send you directly from city i to city j.
Solve the following problem for k=2,3,\ldots, N-1:
Can you travel from city 1 to city N without visiting city k by repeatedly using a teleporter? If you can, print the minimum number of times you need to use a teleporter; otherwise, print -1.
Constraints
- 3 \leq N \leq 10^5
- 1\leq M\leq 10
- M<N
- S_i is a string of length M consisting of
0
and1
. - If i+j>N, then the j-th character of S_i is
0
. - N and M are integers.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print (N-2) integers, separated by spaces, in a single line. The i-th (1\leq i\leq N-2) integer should be the answer to the problem for k=i+1.
Sample Input 1
5 2 11 01 11 10 00
Sample Output 1
2 3 2
A teleporter sends you
- from city 1 to cities 2 and 3;
- from city 2 to city 4;
- from city 3 to cities 4 and 5;
- from city 4 to city 5; and
- from city 5 to nowhere.
Therefore, there are three paths to travel from city 1 to city 5:
- path 1 : city 1 \to city 2 \to city 4 \to city 5;
- path 2 : city 1 \to city 3 \to city 4 \to city 5; and
- path 3 : city 1 \to city 3 \to city 5.
Among these paths,
- two paths, path 2 and path 3, do not visit city 2. Among them, path 3 requires the minimum number of teleporter uses (twice).
- Path 1 is the only path without city 3. It requires using a teleporter three times.
- Path 3 is the only path without city 4. It requires using a teleporter twice.
Thus, 2, 3, and 2, separated by spaces, should be printed.
Sample Input 2
6 3 101 001 101 000 100 000
Sample Output 2
-1 3 3 -1
The only path from city 1 to city 6 is city 1 \to city 2 \to city 5 \to city 6.
For k=2,5, there is no way to travel from city 1 to city 6 without visiting city k.
For k=3,4, the path above satisfies the condition; it requires using a teleporter three times.
Thus, -1, 3, 3, and -1, separated by spaces, should be printed.
Note that a teleporter is one-way;
a teleporter can send you from city 3 to city 4,
but not from city 4 to city 3,
so the following path, for example, is invalid:
city 1 \to city 4 \to city 3 \to city 6.