Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
ある日、学校へ行くのに疲れてしまった高橋くんは、土曜日まであと何日あるかを知りたくなりました。
その日は平日で、曜日を英語で表すと S だったことが分かっています。その日より後の直近の土曜日は何日後かを求めてください。
なお、月曜日、火曜日、水曜日、木曜日、金曜日はそれぞれ英語で Monday, Tuesday, Wednesday, Thursday, Friday です。
制約
- S は
Monday,Tuesday,Wednesday,Thursday,Fridayのいずれかである
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを整数として出力せよ。
入力例 1
Wednesday
出力例 1
3
この日は水曜日なので、3 日後に土曜日になります。
入力例 2
Monday
出力例 2
5
Score : 100 points
Problem Statement
One day, tired from going to school, Takahashi wanted to know how many days there were until Saturday.
We know that the day was a weekday, and the name of the day of the week was S in English.
How many days were there until the first Saturday after that day (including Saturday but not the starting day)?
Constraints
- S is
Monday,Tuesday,Wednesday,Thursday, orFriday.
Input
Input is given from Standard Input in the following format:
S
Output
Print the answer as an integer.
Sample Input 1
Wednesday
Sample Output 1
3
It was Wednesday, so there were 3 days until the first Saturday after that day.
Sample Input 2
Monday
Sample Output 2
5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
A, B, C からなる文字列 S が与えられます。S は A, B, C を全て含むことが保証されます。
S を左から 1 文字ずつ見ていったときに、はじめて次の条件を満たした状態になるのは、左から何文字目まで見たときですか?
A,B,Cが全て 1 回以上出現している。
制約
- 3 \leq N \leq 100
- S は
A,B,Cからなる長さ N の文字列 - S は
A,B,Cを全て含む
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを出力せよ。
入力例 1
5 ACABB
出力例 1
4
左から 4 文字目までに A は 2 回, B は 1 回, C は 1 回出現していて、条件を満たします。
3 文字目以前では条件を満たさないので答えは 4 です。
入力例 2
4 CABC
出力例 2
3
左から 3 文字目までに A, B, C は 1 回ずつ出現していて、条件を満たします。
入力例 3
30 AABABBBABABBABABCABACAABCBACCA
出力例 3
17
Score : 100 points
Problem Statement
You are given a string S consisting of A, B, and C. S is guaranteed to contain all of A, B, and C.
If the characters of S are checked one by one from the left, how many characters will have been checked when the following condition is satisfied for the first time?
- All of
A,B, andChave appeared at least once.
Constraints
- 3 \leq N \leq 100
- S is a string of length N consisting of
A,B, andC. - S contains all of
A,B, andC.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer.
Sample Input 1
5 ACABB
Sample Output 1
4
In the first four characters from the left, A, B, and C appear twice, once, and once, respectively, satisfying the condition.
The condition is not satisfied by checking three or fewer characters, so the answer is 4.
Sample Input 2
4 CABC
Sample Output 2
3
In the first three characters from the left, each of A, B, and C appears once, satisfying the condition.
Sample Input 3
30 AABABBBABABBABABCABACAABCBACCA
Sample Output 3
17
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
N 個の台が一列に並んでおり、左から i 番目の台の高さは H_i です。
高橋君は最初、左端の台の上に立っています。
高橋君は高い所が好きなので、次のルールで可能な限り移動を繰り返します。
- いま立っているのが右端の台ではなく、かつ、右隣にある台の高さが自分がいま立っている台より高いとき、右隣の台に移動する
最終的に高橋君が立っている台の高さを求めてください。
制約
- 2 \leq N \leq 10^5
- 1 \leq H_i \leq 10^9
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N H_1 \ldots H_N
出力
答えを出力せよ。
入力例 1
5 1 5 10 4 2
出力例 1
10
最初、高橋君は左端にある高さ 1 の台に立っています。右隣の台の高さは 5 であり、いま立っている台より高いので、右隣の台に移動します。
移動後、高橋君は左から 2 番目にある高さ 5 の台に立っています。右隣の台の高さは 10 であり、いま立っている台より高いので、右隣の台に移動します。
移動後、高橋君は左から 3 番目にある高さ 10 の台に立っています。右隣の台の高さは 4 であり、いま立っている台より低いので、高橋君は移動をやめます。
よって、最終的に高橋君が立っている台の高さは 10 です。
入力例 2
3 100 1000 100000
出力例 2
100000
入力例 3
4 27 1828 1828 9242
出力例 3
1828
Score : 200 points
Problem Statement
There are N platforms arranged in a row. The height of the i-th platform from the left is H_i.
Takahashi is initially standing on the leftmost platform.
Since he likes heights, he will repeat the following move as long as possible.
- If the platform he is standing on is not the rightmost one, and the next platform to the right has a height greater than that of the current platform, step onto the next platform.
Find the height of the final platform he will stand on.
Constraints
- 2 \leq N \leq 10^5
- 1 \leq H_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N H_1 \ldots H_N
Output
Print the answer.
Sample Input 1
5 1 5 10 4 2
Sample Output 1
10
Takahashi is initially standing on the leftmost platform, whose height is 1. The next platform to the right has a height of 5 and is higher than the current platform, so he steps onto it.
He is now standing on the 2-nd platform from the left, whose height is 5. The next platform to the right has a height of 10 and is higher than the current platform, so he steps onto it.
He is now standing on the 3-rd platform from the left, whose height is 10. The next platform to the right has a height of 4 and is lower than the current platform, so he stops moving.
Thus, the height of the final platform Takahashi will stand on is 10.
Sample Input 2
3 100 1000 100000
Sample Output 2
100000
Sample Input 3
4 27 1828 1828 9242
Sample Output 3
1828
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
1 から N までの番号がついた N 人の参加者が、1 から M までの番号がついた M 問からなるコンテストに参加します。
1 以上 N 以下の整数 i 、1 以上 M 以下の整数 j について、S_i の j 番目の文字が o のとき参加者 i は問題 j を解くことが可能で、S_i の j 番目の文字が x のとき参加者 i は問題 j を解くことが不可能です。
このコンテストは、二人の参加者でペアを組んで参加します。二人が協力することで M 問全てを解くことが可能であるようなペアの個数を答えてください。
より厳密には、1\leq x < y\leq N を満たす整数の組 (x,y) であって、 1 以上 M 以下の任意の整数 j について、参加者 x か参加者 y の少なくとも一方は問題 j を解くことが可能であるという条件を満たすものの個数を答えてください。
制約
- N は 2 以上 30 以下の整数
- M は 1 以上 30 以下の整数
- S_i は
o,xからなる長さ M の文字列
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
5 5 ooooo oooxx xxooo oxoxo xxxxx
出力例 1
5
参加者 1 と 2 のペア、参加者 1 と 3 のペア、参加者 1 と 4 のペア、参加者 1 と 5 のペア、参加者 2 と 3 のペアの 5 個のペアが条件を満たします。
例えば参加者 2 と 4 のペアは、問題 4 が解けないので条件を満たしません。
入力例 2
3 2 ox xo xx
出力例 2
1
入力例 3
2 4 xxxx oxox
出力例 3
0
Score : 200 points
Problem Statement
N participants, numbered 1 to N, will participate in a contest with M problems, numbered 1 to M.
For an integer i between 1 and N and an integer j between 1 and M, participant i can solve problem j if the j-th character of S_i is o, and cannot solve it if that character is x.
The participants must be in pairs. Print the number of ways to form a pair of participants who can collectively solve all the M problems.
More formally, print the number of pairs (x,y) of integers satisfying 1\leq x < y\leq N such that for any integer j between 1 and M, at least one of participant x and participant y can solve problem j.
Constraints
- N is an integer between 2 and 30, inclusive.
- M is an integer between 1 and 30, inclusive.
- S_i is a string of length M consisting of
oandx.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
5 5 ooooo oooxx xxooo oxoxo xxxxx
Sample Output 1
5
The following five pairs satisfy the condition: participants 1 and 2, participants 1 and 3, participants 1 and 4, participants 1 and 5, and participants 2 and 3.
On the other hand, the pair of participants 2 and 4, for instance, does not satisfy the condition because they cannot solve problem 4.
Sample Input 2
3 2 ox xo xx
Sample Output 2
1
Sample Input 3
2 4 xxxx oxox
Sample Output 3
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N カップのアイスクリームがあります。
i カップ目の味は F_i 、美味しさは S_i ( S_i は偶数 ) です。
あなたは、 N 個のカップの中から 2 つを選んで食べることにしました。
このときの満足度は次のように定義されます。
- 食べたアイスクリームの美味しさを s,t ( 但し、 s \ge t ) とする。
- 2 つのカップの味が異なるなら、満足度は \displaystyle s+t である。
- そうでないなら、満足度は \displaystyle s + \frac{t}{2} である。
満足度として達成可能な最大値を求めてください。
制約
- 入力は全て整数
- 2 \le N \le 3 \times 10^5
- 1 \le F_i \le N
- 2 \le S_i \le 10^9
- S_i は偶数
入力
入力は以下の形式で標準入力から与えられる。
N F_1 S_1 F_2 S_2 \vdots F_N S_N
出力
答えを整数として出力せよ。
入力例 1
4 1 4 2 10 2 8 3 6
出力例 1
16
2 カップ目と 4 カップ目のアイスを食べることを考えます。
- 2 カップ目の味は 2 、美味しさは 10 です。
- 4 カップ目の味は 3 、美味しさは 6 です。
- 両者の味は異なるので、満足度は 10+6=16 です。
以上より、満足度 16 を達成できます。
満足度を 16 より大きくすることはできません。
入力例 2
4 4 10 3 2 2 4 4 12
出力例 2
17
1 カップ目と 4 カップ目のアイスを食べることを考えます。
- 1 カップ目の味は 4 、美味しさは 10 です。
- 4 カップ目の味は 4 、美味しさは 12 です。
- 両者の味は同じなので、満足度は 12+\frac{10}{2}=17 です。
以上より、満足度 17 を達成できます。
満足度を 17 より大きくすることはできません。
Score : 300 points
Problem Statement
We have N cups of ice cream.
The flavor and deliciousness of the i-th cup are F_i and S_i, respectively (S_i is an even number).
You will choose and eat two of the N cups.
Your satisfaction here is defined as follows.
- Let s and t (s \ge t) be the deliciousness of the eaten cups.
- If the two cups have different flavors, your satisfaction is \displaystyle s+t.
- Otherwise, your satisfaction is \displaystyle s + \frac{t}{2}.
Find the maximum achievable satisfaction.
Constraints
- All input values are integers.
- 2 \le N \le 3 \times 10^5
- 1 \le F_i \le N
- 2 \le S_i \le 10^9
- S_i is even.
Input
Input is given from Standard Input in the following format:
N F_1 S_1 F_2 S_2 \vdots F_N S_N
Output
Print the answer as an integer.
Sample Input 1
4 1 4 2 10 2 8 3 6
Sample Output 1
16
Consider eating the second and fourth cups.
- The second cup has a flavor of 2 and deliciousness of 10.
- The fourth cup has a flavor of 3 and deliciousness of 6.
- Since they have different flavors, your satisfaction is 10+6=16.
Thus, you can achieve the satisfaction of 16.
You cannot achieve a satisfaction greater than 16.
Sample Input 2
4 4 10 3 2 2 4 4 12
Sample Output 2
17
Consider eating the first and fourth cups.
- The first cup has a flavor of 4 and deliciousness of 10.
- The fourth cup has a flavor of 4 and deliciousness of 12.
- Since they have the same flavor, your satisfaction is 12+\frac{10}{2}=17.
Thus, you can achieve the satisfaction of 17.
You cannot achieve a satisfaction greater than 17.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
1 以上 N 以下の整数からなる集合が M 個あり、順に S_1, S_2, \dots, S_M と呼びます。
S_i は C_i 個の整数 a_{i, 1}, a_{i, 2}, \dots, a_{i, C_i} からなります。
M 個の集合から 1 個以上の集合を選ぶ方法は 2^M-1 通りあります。
このうち、次の条件を満たす選び方は何通りありますか?
- 1 \leq x \leq N を満たす全ての整数 x に対して、選んだ集合の中に x を含む集合が少なくとも 1 個存在する。
制約
- 1 \leq N \leq 10
- 1 \leq M \leq 10
- 1 \leq C_i \leq N
- 1 \leq a_{i,1} \lt a_{i,2} \lt \dots \lt a_{i,C_i} \leq N
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M
C_1
a_{1,1} a_{1,2} \dots a_{1,C_1}
C_2
a_{2,1} a_{2,2} \dots a_{2,C_2}
\vdots
C_M
a_{M,1} a_{M,2} \dots a_{M,C_M}
出力
問題文の条件を満たす集合の選び方の数を出力せよ。
入力例 1
3 3 2 1 2 2 1 3 1 2
出力例 1
3
入力で与えられている集合はそれぞれ S_1 = \lbrace 1, 2 \rbrace, S_2 = \lbrace 1, 3 \rbrace, S_3 = \lbrace 2 \rbrace です。
問題文の条件を満たす集合の選び方は次の 3 通りです。
- S_1, S_2 を選ぶ。
- S_1, S_2, S_3 を選ぶ。
- S_2, S_3 を選ぶ。
入力例 2
4 2 2 1 2 2 1 3
出力例 2
0
問題文の条件を満たす選び方が存在しない場合もあります。
入力例 3
6 6 3 2 3 6 3 2 4 6 2 3 6 3 1 5 6 3 1 3 6 2 1 4
出力例 3
18
Score : 300 points
Problem Statement
There are M sets, called S_1, S_2, \dots, S_M, consisting of integers between 1 and N.
S_i consists of C_i integers a_{i, 1}, a_{i, 2}, \dots, a_{i, C_i}.
There are (2^M-1) ways to choose one or more sets from the M sets.
How many of them satisfy the following condition?
- For all integers x such that 1 \leq x \leq N, there is at least one chosen set containing x.
Constraints
- 1 \leq N \leq 10
- 1 \leq M \leq 10
- 1 \leq C_i \leq N
- 1 \leq a_{i,1} \lt a_{i,2} \lt \dots \lt a_{i,C_i} \leq N
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M
C_1
a_{1,1} a_{1,2} \dots a_{1,C_1}
C_2
a_{2,1} a_{2,2} \dots a_{2,C_2}
\vdots
C_M
a_{M,1} a_{M,2} \dots a_{M,C_M}
Output
Print the number of ways to choose sets that satisfy the conditions in the Problem Statement.
Sample Input 1
3 3 2 1 2 2 1 3 1 2
Sample Output 1
3
The sets given in the input are S_1 = \lbrace 1, 2 \rbrace, S_2 = \lbrace 1, 3 \rbrace, S_3 = \lbrace 2 \rbrace.
The following three ways satisfy the conditions in the Problem Statement:
- choosing S_1, S_2;
- choosing S_1, S_2, S_3;
- choosing S_2, S_3.
Sample Input 2
4 2 2 1 2 2 1 3
Sample Output 2
0
There may be no way to choose sets that satisfies the conditions in the Problem Statement.
Sample Input 3
6 6 3 2 3 6 3 2 4 6 2 3 6 3 1 5 6 3 1 3 6 2 1 4
Sample Output 3
18
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
正整数 N,M が与えられます。
次の条件をともにみたす最小の正整数 X を求めてください。
ただし、そのようなものが存在しない場合は -1 を出力してください。
- X は 1 以上 N 以下の整数 a,b の積として表される。ここで、a と b は同じ整数でも良い。
- X は M 以上である。
制約
- 1\leq N\leq 10^{12}
- 1\leq M\leq 10^{12}
- N,M は整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
問題文の条件をともにみたす最小の正整数 X を出力せよ。そのようなものが存在しない場合は -1 を出力せよ。
入力例 1
5 7
出力例 1
8
まず、7 を 1 以上 5 以下の整数 2 つの積として表すことはできません。
次に、8 は 8=2\times 4 などとして、1 以上 5 以下の整数 2 つの積として表すことができます。
よって、8 を出力します。
入力例 2
2 5
出力例 2
-1
1\times 1=1、1\times 2=2、2\times 1=2、2\times 2=4 より、
1 以上 2 以下の整数 2 つの積として表す事ができるのは 1,2,4 のみであるため、
5 以上の数はそのような整数 2 つの積として表すことができません。
よって、-1 を出力します。
入力例 3
100000 10000000000
出力例 3
10000000000
a=b=100000 (=10^5)とした時、a,b の積は 10000000000 (=10^{10}) となり、これが答えとなります。
答えが 32 bit 整数型に収まらない場合があることに注意してください。
Score : 400 points
Problem Statement
You are given positive integers N and M.
Find the smallest positive integer X that satisfies both of the conditions below, or print -1 if there is no such integer.
- X can be represented as the product of two integers a and b between 1 and N, inclusive. Here, a and b may be the same.
- X is at least M.
Constraints
- 1\leq N\leq 10^{12}
- 1\leq M\leq 10^{12}
- N and M are integers.
Input
The input is given from Standard Input in the following format:
N M
Output
Print the smallest positive integer X that satisfies both of the conditions, or -1 if there is no such integer.
Sample Input 1
5 7
Sample Output 1
8
First, 7 cannot be represented as the product of two integers between 1 and 5.
Second, 8 can be represented as the product of two integers between 1 and 5, such as 8=2\times 4.
Thus, you should print 8.
Sample Input 2
2 5
Sample Output 2
-1
Since 1\times 1=1, 1\times 2=2, 2\times 1=2, and 2\times 2=4, only 1, 2, and 4 can be represented as the product of two integers between 1 and 2,
so no number greater than or equal to 5 can be represented as the product of two such integers.
Thus, you should print -1.
Sample Input 3
100000 10000000000
Sample Output 3
10000000000
For a=b=100000 (=10^5), the product of a and b is 10000000000 (=10^{10}), which is the answer.
Note that the answer may not fit into a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
長さ N の正整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。以下の条件を全て満たす正整数組 (i,j,k) の個数を求めてください。
- 1\leq i < j < k\leq N
- A_i = A_k
- A_i \neq A_j
制約
- 3\leq N\leq 3\times 10^5
- 1\leq A_i \leq N
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを整数として出力せよ。
入力例 1
5 1 2 1 3 2
出力例 1
3
条件を全て満たす正整数組 (i,j,k) は以下の 3 個です。
- (i,j,k)=(1,2,3)
- (i,j,k)=(2,3,5)
- (i,j,k)=(2,4,5)
入力例 2
7 1 2 3 4 5 6 7
出力例 2
0
条件を全て満たす正整数組 (i,j,k) が存在しない場合もあります。
入力例 3
13 9 7 11 7 3 8 1 13 11 11 11 6 13
出力例 3
20
Score : 450 points
Problem Statement
You are given a sequence of positive integers of length N: A=(A_1,A_2,\ldots,A_N). Find the number of triples of positive integers (i,j,k) that satisfy all of the following conditions:
- 1\leq i < j < k\leq N,
- A_i = A_k,
- A_i \neq A_j.
Constraints
- 3\leq N\leq 3\times 10^5
- 1\leq A_i \leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the answer as an integer.
Sample Input 1
5 1 2 1 3 2
Sample Output 1
3
The following three triples of positive integers (i,j,k) satisfy the conditions:
- (i,j,k)=(1,2,3)
- (i,j,k)=(2,3,5)
- (i,j,k)=(2,4,5)
Sample Input 2
7 1 2 3 4 5 6 7
Sample Output 2
0
There may be no triples of positive integers (i,j,k) that satisfy the conditions.
Sample Input 3
13 9 7 11 7 3 8 1 13 11 11 11 6 13
Sample Output 3
20
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
0and1. - 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.