A - Bitwise Exclusive Or

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

0 以上 255 以下の整数 A,B が与えられます。 A \text{ xor }C=B となる 0 以上の整数 C を求めてください。

なお、そのような C はただ 1 つ存在し、0 以上 255 以下であることが証明されます。

\text{ xor } とは

整数 a, b のビットごとの排他的論理和 a \text{ xor } b は、以下のように定義されます。

  • a \text{ xor } b を二進表記した際の 2^k (k \geq 0) の位の数は、a, b を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \text{ xor } 5 = 6 となります (二進表記すると: 011 \text{ xor } 101 = 110)。

制約

  • 0\leq A,B \leq 255
  • 入力に含まれる値は全て整数である

入力

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

A B

出力

答えを出力せよ。


入力例 1

3 6

出力例 1

5

3 は 二進表記で 115 は二進表記で 101 なので、これらの \text{xor} は二進表記で 110 であり、十進表記で 6 です。

このように、3 \text{ xor } 5 = 6 となるので、答えは 5 です。


入力例 2

10 12

出力例 2

6

図

Score : 100 points

Problem Statement

You are given integers A and B between 0 and 255 (inclusive). Find a non-negative integer C such that A \text{ xor }C=B.

It can be proved that there uniquely exists such C, and it will be between 0 and 255 (inclusive).

What is bitwise \mathrm{XOR}?

The bitwise \mathrm{XOR} of integers A and B, A\ \mathrm{XOR}\ B, is defined as follows:

  • When A\ \mathrm{XOR}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of A and B is 1, and 0 otherwise.
For example, we have 3\ \mathrm{XOR}\ 5 = 6 (in base two: 011\ \mathrm{XOR}\ 101 = 110).

Constraints

  • 0\leq A,B \leq 255
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the answer.


Sample Input 1

3 6

Sample Output 1

5

When written in binary, 3 will be 11, and 5 will be 101. Thus, their \text{xor} will be 110 in binary, or 6 in decimal.

In short, 3 \text{ xor } 5 = 6, so the answer is 5.


Sample Input 2

10 12

Sample Output 2

6

Figure

B - Order Something Else

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

高橋君は、レストランで「AtCoder ドリンク」というドリンクを飲もうとしています。 AtCoder ドリンクは定価である P 円を払えば飲むことができます。

また、高橋君は割引券を持っており、それを使うと AtCoder ドリンクを定価より安い価格である Q 円で飲むことができますが、 その場合には AtCoder ドリンクの他に、N 品ある料理の中から 1 つを追加で注文しなければなりません。 i = 1, 2, \ldots, N について、i 番目の料理の価格は D_i 円です。

高橋君がドリンクを飲むため支払う合計金額の最小値を出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq Q \lt P \leq 10^5
  • 1 \leq D_i \leq 10^5
  • 入力はすべて整数

入力

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

N P Q
D_1 D_2 \ldots D_N

出力

答えを出力せよ。


入力例 1

3 100 50
60 20 40

出力例 1

70

割引券を使用して 2 番目の料理を注文することで、ドリンク代 50 円と料理代 20 円の合計 70 円の支払いで AtCoder ドリンクを飲むことができ、支払う合計金額が最小となります。


入力例 2

3 100 50
60000 20000 40000

出力例 2

100

割引券を使用せず定価の 100 円で AtCoder ドリンクを飲むことで、支払う合計金額が最小となります。

Score : 100 points

Problem Statement

Takahashi wants a beverage called AtCoder Drink in a restaurant. It can be ordered at a regular price of P yen.

He also has a discount coupon that allows him to order it at a lower price of Q yen. However, he must additionally order one of the restaurant's N dishes to use that coupon. For each i = 1, 2, \ldots, N, the price of the i-th dish is D_i yen.

Print the minimum total amount of money that he must pay to get the drink.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq Q \lt P \leq 10^5
  • 1 \leq D_i \leq 10^5
  • All input values are integers.

Input

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

N P Q
D_1 D_2 \ldots D_N

Output

Print the answer.


Sample Input 1

3 100 50
60 20 40

Sample Output 1

70

If he uses the coupon and orders the second dish, he can get the drink by paying 50 yen for it and 20 yen for the dish, for a total of 70 yen, which is the minimum total payment needed.


Sample Input 2

3 100 50
60000 20000 40000

Sample Output 2

100

The total payment will be minimized by not using the coupon and paying the regular price of 100 yen.

C - ABCDEFG

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

直線上に 7 個の点 A, B, C, D, E, F, G がこの順に並んでいます。(下の図も参考にしてください)
隣り合う点の距離は次の通りです。

  • A と点 B の距離は 3
  • B と点 C の距離は 1
  • C と点 D の距離は 4
  • D と点 E の距離は 1
  • E と点 F の距離は 5
  • F と点 G の距離は 9

image

2 つの英大文字 p, q が与えられます。p, qA,B,C,D,E,F,G のいずれかで、 p \neq q が成り立ちます。
p と点 q の間の距離を答えてください。

制約

  • p, qA,B,C,D,E,F,G のいずれか
  • p \neq q

入力

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

p q

出力

p と点 q の間の距離を出力せよ。


入力例 1

A C

出力例 1

4

A と点 C の距離は 3 + 1 = 4 です。


入力例 2

G B

出力例 2

20

G と点 B の距離は 9 + 5 + 1 + 4 + 1 = 20 です。


入力例 3

C F

出力例 3

10

Score : 200 points

Problem Statement

There are 7 points A, B, C, D, E, F, and G on a straight line, in this order. (See also the figure below.)
The distances between adjacent points are as follows.

  • Between A and B: 3
  • Between B and C: 1
  • Between C and D: 4
  • Between D and E: 1
  • Between E and F: 5
  • Between F and G: 9

image

You are given two uppercase English letters p and q. Each of p and q is A, B, C, D, E, F, or G, and it holds that p \neq q.
Find the distance between the points p and q.

Constraints

  • Each of p and q is A,B,C,D,E,F, or G.
  • p \neq q

Input

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

p q

Output

Print the distance between the points p and q.


Sample Input 1

A C

Sample Output 1

4

The distance between the points A and C is 3 + 1 = 4.


Sample Input 2

G B

Sample Output 2

20

The distance between the points G and B is 9 + 5 + 1 + 4 + 1 = 20.


Sample Input 3

C F

Sample Output 3

10
D - Append

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

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

  • 1 x: A の末尾に x を追加する。
  • 2 k: A の後ろから k 番目の値を求める。このクエリが与えられるとき、A の長さは k 以上であることが保証される。

制約

  • 1 \leq Q \leq 100
  • 1 種類目のクエリにおいて x1 \leq x \leq 10^9 を満たす整数
  • 2 種類目のクエリにおいて k はその時点の数列 A の長さ以下の正の整数

入力

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

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

クエリは以下の 2 つのいずれかの形式である。

1 x
2 k

出力

2 種類目のクエリの個数を q として q 行出力せよ。
i 行目にはそのような i 回目のクエリに対する答えを出力せよ。


入力例 1

5
1 20
1 30
2 1
1 40
2 3

出力例 1

30
20
  • 最初 A は空である。
  • 1 番目のクエリにより A の末尾に 20 が追加され A=(20) となる。
  • 2 番目のクエリにより A の末尾に 30 が追加され A=(20,30) となる。
  • 3 番目のクエリの答えは A=(20,30) の後ろから 1 番目の値の 30 である。
  • 4 番目のクエリにより A の末尾に 40 が追加され A=(20,30,40) となる。
  • 5 番目のクエリの答えは A=(20,30,40) の後ろから 3 番目の値の 20 である。

Score: 200 points

Problem Statement

You have an empty sequence A. There are Q queries given, and you need to process them in the order they are given.
The queries are of the following two types:

  • 1 x: Append x to the end of A.
  • 2 k: Find the k-th value from the end of A. It is guaranteed that the length of A is at least k when this query is given.

Constraints

  • 1 \leq Q \leq 100
  • In the first type of query, x is an integer satisfying 1 \leq x \leq 10^9.
  • In the second type of query, k is a positive integer not greater than the current length of sequence A.

Input

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

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Each query is in one of the following two formats:

1 x
2 k

Output

Print q lines, where q is the number of queries of the second type.
The i-th line should contain the answer to the i-th such query.


Sample Input 1

5
1 20
1 30
2 1
1 40
2 3

Sample Output 1

30
20
  • Initially, A is empty.
  • The first query appends 20 to the end of A, making A=(20).
  • The second query appends 30 to the end of A, making A=(20,30).
  • The answer to the third query is 30, which is the 1-st value from the end of A=(20,30).
  • The fourth query appends 40 to the end of A, making A=(20,30,40).
  • The answer to the fifth query is 20, which is the 3-rd value from the end of A=(20,30,40).
E - Almost Equal

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 250

問題文

英小文字からなる長さ M の文字列 NS_1,S_2,\dots,S_N が与えられます。ここで、S_i は互いに異なります。

これらを並び替えた文字列の列 T_1,T_2,\dots,T_N であって、以下の条件を満たすものが存在するか判定してください。

  • 1 \le i \le N-1 を満たす全ての整数 i に対して、T_i1 文字だけ別の英小文字に変えて T_{i+1} にすることが出来る。

制約

  • 2 \le N \le 8
  • 1 \le M \le 5
  • S_i は英小文字からなる長さ M の文字列である。(1 \le i \le N)
  • S_i は互いに異なる。

入力

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

N M
S_1
S_2
\vdots
S_N

出力

問題文の条件を満たす列が存在するならば Yes を、そうでないならば No を出力せよ。


入力例 1

4 4
bbed
abcd
abed
fbed

出力例 1

Yes

abcd , abed , bbed , fbed の順に並び替えると条件を満たします。


入力例 2

2 5
abcde
abced

出力例 2

No

どのように並び替えても条件を満たすことは出来ません。


入力例 3

8 4
fast
face
cast
race
fact
rice
nice
case

出力例 3

Yes

Score : 250 points

Problem Statement

You are given N strings S_1,S_2,\dots,S_N, each of length M, consisting of lowercase English letter. Here, S_i are pairwise distinct.

Determine if one can rearrange these strings to obtain a new sequence of strings T_1,T_2,\dots,T_N such that:

  • for all integers i such that 1 \le i \le N-1, one can alter exactly one character of T_i to another lowercase English letter to make it equal to T_{i+1}.

Constraints

  • 2 \le N \le 8
  • 1 \le M \le 5
  • S_i is a string of length M consisting of lowercase English letters. (1 \le i \le N)
  • S_i are pairwise distinct.

Input

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

N M
S_1
S_2
\vdots
S_N

Output

Print Yes if one can obtain a conforming sequence; print No otherwise.


Sample Input 1

4 4
bbed
abcd
abed
fbed

Sample Output 1

Yes

One can rearrange them in this order: abcd, abed, bbed, fbed. This sequence satisfies the condition.


Sample Input 2

2 5
abcde
abced

Sample Output 2

No

No matter how the strings are rearranged, the condition is never satisfied.


Sample Input 3

8 4
fast
face
cast
race
fact
rice
nice
case

Sample Output 3

Yes
F - digitnum

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

整数 N が与えられるので、以下の問題を解いてください。

f(x)= ( x 以下の正整数で、 x と桁数が同じものの数) とします。
f(1)+f(2)+\dots+f(N)998244353 で割った余りを求めてください。

制約

  • N は整数
  • 1 \le N < 10^{18}

入力

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

N

出力

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


入力例 1

16

出力例 1

73
  • 1 以上 9 以下の正整数 x について、 x 以下の整数で、 x と桁数が同じものは 1,2,\dots,x です。
    • これより、 f(1)=1,f(2)=2,...,f(9)=9 となります。
  • 10 以上 16 以下の正整数 x について、 x 以下の整数で、 x と桁数が同じものは 10,11,\dots,x です。
    • これより、 f(10)=1,f(11)=2,...,f(16)=7 となります。

結局、求める答えは 73 です。


入力例 2

238

出力例 2

13870

入力例 3

999999999999999999

出力例 3

762062362

998244353 で割った余りを求めることに注意してください。

Score : 300 points

Problem Statement

Given an integer N, solve the following problem.

Let f(x)= (The number of positive integers at most x with the same number of digits as x).
Find f(1)+f(2)+\dots+f(N) modulo 998244353.

Constraints

  • N is an integer.
  • 1 \le N < 10^{18}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer as an integer.


Sample Input 1

16

Sample Output 1

73
  • For a positive integer x between 1 and 9, the positive integers at most x with the same number of digits as x are 1,2,\dots,x.
    • Thus, we have f(1)=1,f(2)=2,...,f(9)=9.
  • For a positive integer x between 10 and 16, the positive integers at most x with the same number of digits as x are 10,11,\dots,x.
    • Thus, we have f(10)=1,f(11)=2,...,f(16)=7.

The final answer is 73.


Sample Input 2

238

Sample Output 2

13870

Sample Input 3

999999999999999999

Sample Output 3

762062362

Be sure to find the sum modulo 998244353.

G - I Hate Non-integer Number

Time Limit: 2.5 sec / Memory Limit: 1024 MB

配点 : 400

問題文

項数が N の正整数列 A=(a_1,\ldots,a_N) が与えられます。
A の項を 1 個以上選ぶ方法は 2^N-1 通りありますが、そのうち選んだ項の平均値が整数であるものが何通りかを 998244353 で割った余りを求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq a_i \leq 10^9
  • 入力はすべて整数

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

3
2 6 2

出力例 1

6

A の項を選ぶ方法それぞれに対する平均値は以下のようになります。

  • a_1 のみを選んだ場合、平均値は \frac{a_1}{1}=\frac{2}{1} = 2 であり、整数である。

  • a_2 のみを選んだ場合、平均値は \frac{a_2}{1}=\frac{6}{1} = 6 であり、整数である。

  • a_3 のみを選んだ場合、平均値は \frac{a_3}{1}=\frac{2}{1} = 2 であり、整数である。

  • a_1a_2 を選んだ場合、平均値は \frac{a_1+a_2}{2}=\frac{2+6}{2} = 4 であり、整数である。

  • a_1a_3 を選んだ場合、平均値は \frac{a_1+a_3}{2}=\frac{2+2}{2} = 2 であり、整数である。

  • a_2a_3 を選んだ場合、平均値は \frac{a_2+a_3}{2}=\frac{6+2}{2} = 4 であり、整数である。

  • a_1a_2a_3 を選んだ場合、平均値は \frac{a_1+a_2+a_3}{3}=\frac{2+6+2}{3} = \frac{10}{3} であり、整数ではない。

以上より、6 通りの選び方が条件を満たします。


入力例 2

5
5 5 5 5 5

出力例 2

31

どのように A の項を 1 個以上選んでも平均値が 5 になります。

Score : 400 points

Problem Statement

You are given a sequence of positive integers A=(a_1,\ldots,a_N) of length N.
There are (2^N-1) ways to choose one or more terms of A. How many of them have an integer-valued average? Find the count modulo 998244353.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq a_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

3
2 6 2

Sample Output 1

6

For each way to choose terms of A, the average is obtained as follows:

  • If just a_1 is chosen, the average is \frac{a_1}{1}=\frac{2}{1} = 2, which is an integer.

  • If just a_2 is chosen, the average is \frac{a_2}{1}=\frac{6}{1} = 6, which is an integer.

  • If just a_3 is chosen, the average is \frac{a_3}{1}=\frac{2}{1} = 2, which is an integer.

  • If a_1 and a_2 are chosen, the average is \frac{a_1+a_2}{2}=\frac{2+6}{2} = 4, which is an integer.

  • If a_1 and a_3 are chosen, the average is \frac{a_1+a_3}{2}=\frac{2+2}{2} = 2, which is an integer.

  • If a_2 and a_3 are chosen, the average is \frac{a_2+a_3}{2}=\frac{6+2}{2} = 4, which is an integer.

  • If a_1, a_2, and a_3 are chosen, the average is \frac{a_1+a_2+a_3}{3}=\frac{2+6+2}{3} = \frac{10}{3}, which is not an integer.

Therefore, 6 ways satisfy the condition.


Sample Input 2

5
5 5 5 5 5

Sample Output 2

31

Regardless of the choice of one or more terms of A, the average equals 5.

H - Prerequisites

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 425

問題文

1 から N までの番号がついた N 冊の本があります。
i には C_i 冊の前提となる本があり、そのうち j 冊目は本 P_{i,j} で、本 i を読む前にこの C_i 冊をすべて読む必要があります。
ただし、適切な順序を選ぶことですべての本を読むことができます。

あなたは本 1 を読むために必要な最小の数の本を読もうとしています。
1 以外に読まなければならない本の番号を読むべき順に出力してください。ただし、この条件下で読むべき本の集合は一意に定まります。
条件を満たす読む順番が複数考えられる場合は、そのいずれを出力しても構いません。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq C_i < N
  • \sum_{i=1}^{N} C_i \leq 2 \times 10^5
  • C_1 \geq 1
  • 1 \leq P_{i,j} \leq N
  • 1 \leq j < k \leq C_i のとき P_{i,j} \neq P_{i,k}
  • すべての本を読むことが可能である

入力

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

N
C_1 P_{1,1} \ldots P_{1,C_1}
C_2 P_{2,1} \ldots P_{2,C_2}
\vdots
C_N P_{N,1} \ldots P_{N,C_N}

出力

1 を読むために読む必要のある最小の数の本を読むとき、それらの番号を読むべき順に空白区切りで出力せよ。


入力例 1

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

出力例 1

5 3 4 2

1 を読むために本 2,3,4、本 2 を読むために本 3,5、本 4 を読むために本 5 を読む必要があります。本 3,5,6 を読むために他の本を読む必要はありません。

このとき、例えば本 5,3,4,2 の順に読むことで本 1 を読むことができます。3 冊以下の本を読んだ状態で本 1 が読めるようになることはないため、これは答えの一つです。他にも本 3,5,4,2 の順などで読むことでも 4 冊の本を読んだ状態で本 1 を読むことができるようになります。


入力例 2

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

出力例 2

6 5 4 3 2

入力例 3

8
1 5
1 6
1 7
1 8
0
0
0
0

出力例 3

5

Score : 425 points

Problem Statement

We have N books numbered 1 to N.
Book i assumes that you have read C_i books, the j-th of which is book P_{i,j}: you must read all these C_i books before reading book i.
Here, you can read all the books in some order.

You are trying to read the minimum number of books required to read book 1.
Print the numbers of the books you must read excluding book 1 in the order they should be read. Under this condition, the set of books to read is uniquely determined.
If there are multiple reading orders that satisfy the condition, you may print any of them.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 0 \leq C_i < N
  • \sum_{i=1}^{N} C_i \leq 2 \times 10^5
  • C_1 \geq 1
  • 1 \leq P_{i,j} \leq N
  • P_{i,j} \neq P_{i,k} for 1 \leq j < k \leq C_i.
  • It is possible to read all the books.

Input

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

N
C_1 P_{1,1} \ldots P_{1,C_1}
C_2 P_{2,1} \ldots P_{2,C_2}
\vdots
C_N P_{N,1} \ldots P_{N,C_N}

Output

Print the numbers of the books you must read to read book 1 in the order they should be read, with spaces in between.


Sample Input 1

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

Sample Output 1

5 3 4 2

To read book 1, you must read books 2,3,4; to read book 2, you must read books 3,5; to read book 4, you must read book 5. To read books 3,5,6, you do not have to read any other books.

For example, if you read books 5,3,4,2 in this order, you can read book 1. This is a correct answer, because you will never be able to read book 1 with three or fewer books read. As another example, reading books 3,5,4,2 in this order also allows you to read book 1 with 4 books read.


Sample Input 2

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

Sample Output 2

6 5 4 3 2

Sample Input 3

8
1 5
1 6
1 7
1 8
0
0
0
0

Sample Output 3

5
I - Teleporter and Closed off

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 個の都市があり、都市 1, 都市 2, \ldots, 都市 N と番号づけられています。
いくつかの異なる都市の間は一方通行のテレポーターによって移動できます。 都市 i (1\leq i\leq N) からテレポーターによって直接移動できる都市は 01 からなる長さ 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_i01 のみからなる長さ M の文字列
  • i+j>N ならば S_ij 文字目は 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, 経路 32つであり、そのうちテレポーターの使用回数が最小となるのは経路 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 and 1.
  • 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.