実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
縦 2 行、横 2 列のグリッド(各マスが正方形のマス目)があります。
このグリッドは、各マスが黒か白であり、少なくとも 2 つの黒マスを含みます。
各マスの色の情報は文字列 S_1,S_2 として、以下の形式で与えられます。
- 文字列 S_i の j 文字目が
#であれば上から i マス目、左から j マス目は黒 - 文字列 S_i の j 文字目が
.であれば上から i マス目、左から j マス目は白
2 つの異なる黒マス同士が辺で接している時、またその時に限りそれら 2 つの黒マスは直接行き来できます。
黒マスのみをいくつか通ることによって、どの 2 つの黒マス同士も(直接または間接的に)行き来できるかどうか判定してください。
制約
- S_1,S_2 は
#または.からなる 2 文字の文字列 - S_1,S_2 に
#が合計で 2 つ以上含まれる
入力
入力は以下の形式で標準入力から与えられる。
S_1 S_2
出力
どの 2 つの黒マス同士も行き来できるなら Yes 、そうでないなら No と出力せよ。
入力例 1
## .#
出力例 1
Yes
左上の黒マスと右上の黒マス、右上の黒マスと右下の黒マスを直接行き来することができます。
これらの移動を用いてどの黒マスからどの黒マスへも行き来できるので、答えは Yes となります。
入力例 2
.# #.
出力例 2
No
右上の黒マスと左下の黒マスを行き来することはできません。答えは No となります。
Score : 100 points
Problem Statement
We have a grid with 2 horizontal rows and 2 vertical columns.
Each of the squares is black or white, and there are at least 2 black squares.
The colors of the squares are given to you as strings S_1 and S_2, as follows.
- If the j-th character of S_i is
#, the square at the i-th row from the top and j-th column from the left is black. - If the j-th character of S_i is
., the square at the i-th row from the top and j-th column from the left is white.
You can travel between two different black squares if and only if they share a side.
Determine whether it is possible to travel from every black square to every black square (directly or indirectly) by only passing black squares.
Constraints
- Each of S_1 and S_2 is a string with two characters consisting of
#and.. - S_1 and S_2 have two or more
#s in total.
Input
Input is given from Standard Input in the following format:
S_1 S_2
Output
If it is possible to travel from every black square to every black square, print Yes; otherwise, print No.
Sample Input 1
## .#
Sample Output 1
Yes
It is possible to directly travel between the top-left and top-right black squares and between top-right and bottom-right squares.
These two moves enable us to travel from every black square to every black square, so the answer is Yes.
Sample Input 2
.# #.
Sample Output 2
No
It is impossible to travel between the top-right and bottom-left black squares, so the answer is No.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
0 以上 9 以下の整数 A, B が与えられます。
0 以上 9 以下の整数であって A + B と等しくないものをいずれかひとつ出力してください。
制約
- 0 \leq A \leq 9
- 0 \leq B \leq 9
- A + B \leq 9
- A, B は整数
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
0 以上 9 以下の整数であって A + B と等しくないものをいずれかひとつ出力せよ。
入力例 1
2 5
出力例 1
2
A = 2, B = 5 のとき A + B = 7 です。したがって、0, 1, 2, 3, 4, 5, 6, 8, 9 のいずれかを出力すると正解となります。
入力例 2
0 0
出力例 2
9
入力例 3
7 1
出力例 3
4
Score: 100 points
Problem Statement
You are given two integers A and B, each between 0 and 9, inclusive.
Print any integer between 0 and 9, inclusive, that is not equal to A + B.
Constraints
- 0 \leq A \leq 9
- 0 \leq B \leq 9
- A + B \leq 9
- A and B are integers.
Input
The input is given from Standard Input in the following format:
A B
Output
Print any integer between 0 and 9, inclusive, that is not equal to A + B.
Sample Input 1
2 5
Sample Output 1
2
When A = 2, B = 5, we have A + B = 7. Thus, printing any of 0, 1, 2, 3, 4, 5, 6, 8, 9 is correct.
Sample Input 2
0 0
Sample Output 2
9
Sample Input 3
7 1
Sample Output 3
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
N 人の人があるコンテストに参加し、i 位の人のハンドルネームは S_i でした。
上位 K 人のハンドルネームを辞書順に出力してください。
辞書順とは?
辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。
以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。
- S と T のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
- S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
- S_i \neq T_i である i が存在しない場合、 S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。
制約
- 1 \leq K \leq N \leq 100
- K, N は整数
- S_i は英小文字からなる長さ 10 以下の文字列
- i \neq j ならば S_i \neq S_j
入力
入力は以下の形式で標準入力から与えられる。
N K S_1 S_2 \vdots S_N
出力
答えを改行区切りで出力せよ。
入力例 1
5 3 abc aaaaa xyz a def
出力例 1
aaaaa abc xyz
このコンテストには 5 人が参加し、1 位の人のハンドルネームは abc 、2 位の人のハンドルネームは aaaaa 、3 位の人のハンドルネームは xyz 、4 位の人のハンドルネームは a 、5 位の人のハンドルネームは def でした。
上位 3 人のハンドルネームは abc、aaaaa、xyz であるため、これを辞書順に並べ替えて aaaaa 、abc 、xyz の順に出力します。
入力例 2
4 4 z zyx zzz rbg
出力例 2
rbg z zyx zzz
入力例 3
3 1 abc arc agc
出力例 3
abc
Score : 200 points
Problem Statement
There were N participants in a contest. The participant ranked i-th had the nickname S_i.
Print the nicknames of the top K participants in lexicographical order.
What is lexicographical order?
Simply put, the lexicographical order is the order of words in a dictionary. As a formal description, below is an algorithm to order distinct strings S and T.
Let S_i denote the i-th character of a string S. We write S \lt T if S is lexicographically smaller than T, and S \gt T if S is larger.
- Let L be the length of the shorter of S and T. For i=1,2,\dots,L, check whether S_i equals T_i.
- If there is an i such that S_i \neq T_i, let j be the smallest such i. Compare S_j and T_j. If S_j is alphabetically smaller than T_j, we get S \lt T; if S_j is larger, we get S \gt T.
- If there is no i such that S_i \neq T_i, compare the lengths of S and T. If S is shorter than T, we get S \lt T; if S is longer, we get S \gt T.
Constraints
- 1 \leq K \leq N \leq 100
- K and N are integers.
- S_i is a string of length 10 consisting of lowercase English letters.
- S_i \neq S_j if i \neq j.
Input
The input is given from Standard Input in the following format:
N K S_1 S_2 \vdots S_N
Output
Print the nicknames, separated by newlines.
Sample Input 1
5 3 abc aaaaa xyz a def
Sample Output 1
aaaaa abc xyz
This contest had five participants. The participants ranked first, second, third, fourth, and fifth had the nicknames abc, aaaaa, xyz, a, and def, respectively.
The nicknames of the top three participants were abc, aaaaa, xyz, so print these in lexicographical order: aaaaa, abc, xyz.
Sample Input 2
4 4 z zyx zzz rbg
Sample Output 2
rbg z zyx zzz
Sample Input 3
3 1 abc arc agc
Sample Output 3
abc
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
整数 B が与えられます。
A^A = B であるような正の整数 A が存在するならばその値を、存在しないならば -1 を出力してください。
制約
- 1 \leq B \leq 10^{18}
- B は整数
入力
入力は以下の形式で標準入力から与えられる。
B
出力
A^A = B であるような正の整数 A が存在するならばその値を、存在しないならば -1 を出力せよ。
A^A = B を満たす正の整数 A が複数ある場合は、どれを出力しても正解とみなされる。
入力例 1
27
出力例 1
3
3^3 = 27 なので 3 を出力します。
入力例 2
100
出力例 2
-1
A^A = B を満たす A は存在しません。
入力例 3
10000000000
出力例 3
10
Score : 200 points
Problem Statement
You are given an integer B.
If there exists a positive integer A such that A^A = B, print its value; otherwise, output -1.
Constraints
- 1 \leq B \leq 10^{18}
- B is an integer.
Input
The input is given from Standard Input in the following format:
B
Output
If there exists a positive integer A such that A^A = B, print its value; otherwise, print -1.
If there are multiple positive integers A such that A^A = B, any of them will be accepted.
Sample Input 1
27
Sample Output 1
3
3^3 = 27, so print 3.
Sample Input 2
100
Sample Output 2
-1
There is no A such that A^A = B.
Sample Input 3
10000000000
Sample Output 3
10
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
AtCoderレストランでは 1 から N までの番号がつけられている N 種類の食材を扱っています。
また、AtCoderレストランでは 1 から M までの番号がつけられている M 個の料理を提供しています。料理 i には K_i 種類の食材が使われており、食材 A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} が使われています。
すぬけくんは現在 N 種類の食材がすべて苦手です。また、すぬけ君は苦手な食材が 1 種類でも使われている料理を食べることができず、苦手な食材が 1 種類も使われていない料理を食べることができます。
すぬけ君はこれから N 日間かけて苦手な食材を克服しようとしています。 すぬけ君は i 日目に食材 B_i を克服し、それ以降苦手な食材でなくなります。
i=1,2,\ldots,N について以下の値を求めてください。
- i 日目にすぬけ君が食材 B_i を克服した直後、すぬけ君が食べることができるAtCoderレストランの料理の個数
制約
- 1 \leq N \leq 3 \times 10^{5}
- 1 \leq M \leq 3 \times 10^{5}
- 1 \leq K_i \leq N (1 \leq i \leq M)
- K_i の総和は 3 \times 10^{5} 以下
- 1 \leq A_{i,j} \leq N (1 \leq i \leq M, 1 \leq j \leq K_i)
- A_{i,j} \neq A_{i,k} (1 \leq i \leq M, j \neq k)
- 1 \leq B_i \leq N (1 \leq i \leq N)
- B_i \neq B_j (i \neq j )
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
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}
\vdots
K_M A_{M,1} A_{M,2} \ldots A_{M,K_M}
B_1 B_2 \ldots B_N
出力
N 行出力せよ。k 行目には、i=k のときの値を出力せよ。
入力例 1
5 4 2 1 2 3 3 4 5 3 1 2 5 1 3 1 3 2 5 4
出力例 1
0 1 2 3 4
以下のようにすぬけ君は苦手な食材を克服します。
- 1 日目: 食材 1 を克服する。このとき、どの料理にも苦手な食材が使われているため 0 を出力する。
- 2 日目: 食材 3 を克服する。このとき、料理 4 は苦手な食材が使われなくなるため食べられるようになる。料理 4 以外の料理は苦手な食材が使われているため、 1 を出力する。
- 3 日目: 食材 2 を克服する。このとき、料理 1 は苦手な食材が使われなくなるため食べられるようになる。料理 1,4 以外の料理は苦手な食材が使われているため、 2 を出力する。
- 4 日目: 食材 5 を克服する。このとき、料理 3 は苦手な食材が使われなくなるため食べられるようになる。料理 1,3,4 以外の料理は苦手な食材が使われているため、 3 を出力する。
- 5 日目: 食材 4 を克服する。このとき、料理 2 は苦手な食材が使われなくなるため食べられるようになる。全ての料理に苦手な食材が使われていないため、 4 を出力する。
入力例 2
9 8 1 4 5 6 9 7 4 3 4 2 4 1 3 1 1 5 7 9 8 1 5 2 9 8 1 2 1 1 6 5 2 7 8 4 1 9 3
出力例 2
0 0 1 1 1 2 4 6 8
Score : 300 points
Problem Statement
The AtCoder Restaurant uses N types of ingredients numbered from 1 to N.
The restaurant offers M dishes numbered from 1 to M. Dish i uses K_i types of ingredients, namely A_{i,1}, A_{i,2}, \ldots, A_{i,K_i}.
Snuke currently dislikes all N ingredients. He cannot eat any dish that uses one or more ingredients he dislikes, and he can eat a dish that uses none of the disliked ingredients.
Over the next N days, he will overcome his dislikes one ingredient per day. On day i, he overcomes ingredient B_i, and from then on he no longer dislikes it.
For each i=1,2,\ldots,N, find:
- the number of dishes at the AtCoder Restaurant that he can eat immediately after overcoming ingredient B_i on day i.
Constraints
- 1 \leq N \leq 3 \times 10^{5}
- 1 \leq M \leq 3 \times 10^{5}
- 1 \leq K_i \leq N (1 \leq i \leq M)
- The sum of K_i is at most 3 \times 10^{5}.
- 1 \leq A_{i,j} \leq N (1 \leq i \leq M, 1 \leq j \leq K_i)
- A_{i,j} \neq A_{i,k} (1 \leq i \leq M, j \neq k)
- 1 \leq B_i \leq N (1 \leq i \leq N)
- B_i \neq B_j (i \neq j )
- All input values are integers.
Input
The 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}
\vdots
K_M A_{M,1} A_{M,2} \ldots A_{M,K_M}
B_1 B_2 \ldots B_N
Output
Print N lines. The k-th line should contain the answer for i=k.
Sample Input 1
5 4 2 1 2 3 3 4 5 3 1 2 5 1 3 1 3 2 5 4
Sample Output 1
0 1 2 3 4
Snuke overcomes his disliked ingredients as follows:
- Day 1: He overcomes ingredient 1. At this time, every dish still uses a disliked ingredient, so print 0.
- Day 2: He overcomes ingredient 3. Dish 4 no longer uses any disliked ingredient and becomes edible; all other dishes still use disliked ingredients, so print 1.
- Day 3: He overcomes ingredient 2. Dish 1 no longer uses any disliked ingredient and becomes edible; all dishes except 1 and 4 still use disliked ingredients, so print 2.
- Day 4: He overcomes ingredient 5. Dish 3 no longer uses any disliked ingredient and becomes edible; all dishes except 1, 3, and 4 still use disliked ingredients, so print 3.
- Day 5: He overcomes ingredient 4. Dish 2 no longer uses any disliked ingredient and becomes edible; now all dishes have no disliked ingredients, so print 4.
Sample Input 2
9 8 1 4 5 6 9 7 4 3 4 2 4 1 3 1 1 5 7 9 8 1 5 2 9 8 1 2 1 1 6 5 2 7 8 4 1 9 3
Sample Output 2
0 0 1 1 1 2 4 6 8