実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
ボールがいくつか刺さった棒が N 本あり、各ボールには英小文字が 1 個書かれています。
i = 1, 2, \ldots, N について、i 番目の棒に刺さった各ボールの英小文字は、文字列 S_i によって表されます。 具体的には、i 番目の棒には文字列 S_i の長さ |S_i| に等しい個数のボールが刺さっており、 刺さっているボールの英小文字を、棒のある端から順に並べたものは文字列 S_i と等しいです。
2 つの棒は、一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列と、もう一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列が一致するとき、同じ棒とみなされます。 より形式的には、1 以上 N 以下の整数 i, j について、i 本目の棒と j 本目の棒は、S_i が S_j と一致するか、S_i が S_j を前後反転したものと一致するとき、かつそのときに限り、同じとみなされます。
N 本の棒の中に、何種類の異なる棒があるかを出力してください。
制約
- N は整数
- 2 \leq N \leq 2 \times 10^5
- S_i は英小文字のみからなる文字列
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
6 a abc de cba de abc
出力例 1
3
- S_2 =
abcが S_4 =cbaを前後反転したものと一致するため、2 番目の棒と 4 番目の棒は同じとみなされます。 - S_2 =
abcが S_6 =abcと一致するため、2 番目の棒と 6 番目の棒は同じとみなされます。 - S_3 =
deが S_5 =deと一致するため、3 番目の棒と 5 番目の棒は同じとみなされます。
よって、6 本の棒の中に、1 本目の棒、2 本目の棒( 4, 6 本目の棒と同じ)、3 本目の棒( 5 本目の棒と同じ)の 3 種類の異なる棒があります。
Score : 300 points
Problem Statement
There are N sticks with several balls stuck onto them. Each ball has a lowercase English letter written on it.
For each i = 1, 2, \ldots, N, the letters written on the balls stuck onto the i-th stick are represented by a string S_i. Specifically, the number of balls stuck onto the i-th stick is the length |S_i| of the string S_i, and S_i is the sequence of letters on the balls starting from one end of the stick.
Two sticks are considered the same when the sequence of letters on the balls starting from one end of one stick is equal to the sequence of letters starting from one end of the other stick. More formally, for integers i and j between 1 and N, inclusive, the i-th and j-th sticks are considered the same if and only if S_i equals S_j or its reversal.
Print the number of different sticks among the N sticks.
Constraints
- N is an integer.
- 2 \leq N \leq 2 \times 10^5
- S_i is a string consisting of lowercase English letters.
- |S_i| \geq 1
- \sum_{i = 1}^N |S_i| \leq 2 \times 10^5
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
6 a abc de cba de abc
Sample Output 1
3
- S_2 =
abcequals the reversal of S_4 =cba, so the second and fourth sticks are considered the same. - S_2 =
abcequals S_6 =abc, so the second and sixth sticks are considered the same. - S_3 =
deequals S_5 =de, so the third and fifth sticks are considered the same.
Therefore, there are three different sticks among the six: the first, second (same as the fourth and sixth), and third (same as the fifth).
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
N 個のリールからなるスロットがあります。
i 番目のリールの配列は文字列 S_i によって表されます。
ここで、S_i は 0, 1, \ldots, 9 がちょうど 1 回ずつ現れる長さ 10 の文字列です。
それぞれのリールには対応するボタンがついており、高橋君は各非負整数 t について、
スロットが回り始めてからちょうど t 秒後にボタンを 1 つ選んで押す(または何もしない)ことができます。
スロットが回り始めてから t 秒後に i 番目のリールに対応するボタンを押すと、
i 番目のリールは S_i の (t\bmod{10})+1 文字目を表示して止まります。
ただし、t\bmod{10} で t を 10 で割ったあまりを表します。
高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。
制約
- 2\leq N\leq 100
- N は整数
- S_i は
0,1, \ldots,9がちょうど 1 回ずつ現れる長さ 10 の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを出力せよ。
入力例 1
3 1937458062 8124690357 2385760149
出力例 1
6
高橋君は次のようにそれぞれのリールを止めることでスロットが回り始めてから 6 秒後にリールに表示される文字を 8 で揃えることができます。
- スロットの回転開始から 0 秒後に 2 番目のリールに対応するボタンを押します。2 番目のリールは S_2 の (0\bmod{10})+1=1 文字目である
8を表示して止まります。 - スロットの回転開始から 2 秒後に 3 番目のリールに対応するボタンを押します。3 番目のリールは S_3 の (2\bmod{10})+1=3 文字目である
8を表示して止まります。 - スロットの回転開始から 6 秒後に 1 番目のリールに対応するボタンを押します。1 番目のリールは S_1 の (6\bmod{10})+1=7 文字目である
8を表示して止まります。
5 秒以下で全てのリールに表示されている文字を揃える方法はないため、6 を出力します。
入力例 2
5 0123456789 0123456789 0123456789 0123456789 0123456789
出力例 2
40
全てのリールを止めた上で、表示されている文字を揃える必要がある事に注意してください。
Score : 300 points
Problem Statement
There is a slot machine with N reels.
The placement of symbols on the i-th reel is represented by a string S_i of length 10 containing each of 0, 1, \ldots, 9 exactly once.
Each reel has a corresponding button. For each non-negative integer t, Takahashi can press one of the buttons of his choice (or do nothing) t seconds after the reels start spinning.
If the button for the i-th reel is pressed t seconds after the start of the spin, the i-th reel will stop to display the ((t\bmod{10})+1)-th character of S_i.
Here, t\bmod{10} denotes the remainder when t is divided by 10.
Takahashi wants to stop all reels to make them display the same character.
Find the minimum number of seconds needed to achieve his objective after the start of the spin.
Constraints
- 2\leq N\leq 100
- N is an integer.
- S_i is a string of length 10 containing each of
0,1, \ldots,9exactly once.
Input
Input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the minimum number of seconds needed to achieve Takahashi's objective after the start of the spin.
Sample Input 1
3 1937458062 8124690357 2385760149
Sample Output 1
6
Takahashi can make all reels display 8 in 6 seconds after the start of the spin by stopping them as follows.
- 0 seconds after the start of the spin, press the button for the 2-nd reel, making it stop to display the ((0\bmod{10})+1=1)-st character of S_2,
8. - 2 seconds after the start of the spin, press the button for the 3-rd reel, making it stop to display the ((2\bmod{10})+1=3)-rd character of S_3,
8. - 6 seconds after the start of the spin, press the button for the 1-st reel, making it stop to display the ((6\bmod{10})+1=7)-th character of S_1,
8.
There is no way to make all reels display the same character in five seconds or less, so the answer is 6.
Sample Input 2
5 0123456789 0123456789 0123456789 0123456789 0123456789
Sample Output 2
40
Note that he must stop all reels to make them display the same character.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
頂点 1, 頂点 2,\ldots, 頂点 N の N 個の頂点からなる木が与えられます。 i 番目 (1\leq i\lt N) の辺は頂点 u _ i と v _ i を結んでいます。
次の操作を好きな回数繰り返すことを考えます。
- 葉である頂点 v を 1 つ選び、頂点 v およびそれに接続する辺をすべて削除する。
頂点 1 を削除するまでに最小で操作を何回行う必要があるか求めてください。
木とは?
木とは、無向グラフのうち連結であって閉路がないものです。 詳しくはこちらをご覧ください: Wikipedia「木 (数学)」葉とは?
木の葉とは、木の頂点のうち次数がたかだか 1 であるものです。制約
- 2\leq N\leq3\times10^5
- 1\leq u _ i\lt v _ i\leq N\ (1\leq i\lt N)
- 与えられるグラフは木
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {N-1} v _ {N-1}
出力
答えを 1 行で出力せよ。
入力例 1
9 1 2 2 3 2 4 2 5 1 6 6 7 7 8 7 9
出力例 1
5
与えられるグラフは次のようになります。

たとえば、頂点 9,8,7,6,1 の順に選んで操作を行うことで、5 回の操作で頂点 1 を削除することができます。

4 回以下の操作では頂点 1 を削除することはできないため、5 を出力してください。
入力例 2
6 1 2 2 3 2 4 3 5 3 6
出力例 2
1
与えられたグラフにおいて、頂点 1 は葉です。 よって、1 回目の操作で頂点 1 を選んで削除することができます。
入力例 3
24 3 6 7 17 7 20 7 11 14 18 17 21 6 19 5 22 9 24 11 14 6 23 8 17 9 12 4 17 2 15 1 17 3 9 10 16 7 13 2 16 1 16 5 7 1 3
出力例 3
12
Score : 400 points
Problem Statement
You are given a tree with N vertices: vertex 1, vertex 2, \ldots, vertex N. The i-th edge (1\leq i\lt N) connects vertex u _ i and vertex v _ i.
Consider repeating the following operation some number of times:
- Choose one leaf vertex v and delete it along with all incident edges.
Find the minimum number of operations required to delete vertex 1.
What is a tree?
A tree is an undirected graph that is connected and has no cycles. For more details, see: Wikipedia "Tree (graph theory)".What is a leaf?
A leaf in a tree is a vertex with a degree of at most 1.Constraints
- 2\leq N\leq3\times10^5
- 1\leq u _ i\lt v _ i\leq N\ (1\leq i\lt N)
- The given graph is a tree.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {N-1} v _ {N-1}
Output
Print the answer in a single line.
Sample Input 1
9 1 2 2 3 2 4 2 5 1 6 6 7 7 8 7 9
Sample Output 1
5
The given graph looks like this:

For example, you can choose vertices 9,8,7,6,1 in this order to delete vertex 1 in five operations.

Vertex 1 cannot be deleted in four or fewer operations, so print 5.
Sample Input 2
6 1 2 2 3 2 4 3 5 3 6
Sample Output 2
1
In the given graph, vertex 1 is a leaf. Hence, you can choose and delete vertex 1 in the first operation.
Sample Input 3
24 3 6 7 17 7 20 7 11 14 18 17 21 6 19 5 22 9 24 11 14 6 23 8 17 9 12 4 17 2 15 1 17 3 9 10 16 7 13 2 16 1 16 5 7 1 3
Sample Output 3
12
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
この問題は インタラクティブな問題(あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。
正整数 N および 0 以上 2^N 未満の整数 L,R(L\leq R) が与えられます。 ジャッジシステムは、0 以上 99 以下の整数からなる長さ 2^N の数列 A = (A_0, A_1, \dots, A_{2^N-1}) を隠し持っています。
あなたの目標は A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りを求めることです。ただし、あなたは数列 A の要素の値を直接知ることはできません。 その代わりに、ジャッジシステムに対して以下の質問を行うことができます。
- 2^i(j+1)\leq 2^N を満たすように非負整数 i,j を選ぶ。l=2^ij,r=2^i(j+1)-1 として A_l+A_{l+1}+\dots+A_{r} を 100 で割った余りを聞く。
どのような A であっても A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りを特定することができる質問回数の最小値を m とします。m 回以内の質問を行って A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りを求めてください。
制約
- 1\leq N\leq 18
- 0\leq L\leq R\leq 2^N-1
- 入力は全て整数
入出力
この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジシステムが入出力を介して対話を行う形式の問題)です。
最初に、整数 N,L,R を標準入力から受け取ってください。
N L R
次に、A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りを特定できるまで質問を繰り返してください。 質問は、以下の形式で標準出力に出力してください。
? i j
ここで、i,j は以下を満たす必要があります。
- i,j は非負整数
- 2^i(j+1)\leq 2^N
これに対する応答は、次の形式で標準入力から与えられます。
T
ここで、T は質問に対する答えで、l=2^ij,r=2^i(j+1)-1 としたとき A_l+A_{l+1}+\dots+A_{r} を 100 で割った余りです。
ただし、i,j が制約を満たしていないか、質問回数が m 回を超えた場合は T は -1 となります。
ジャッジが -1 を返した場合、プログラムはすでに不正解とみなされています。この場合、ただちにプログラムを終了してください。
A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りが特定出来たら、S を A_L+A_{L+1}+\dots+A_{R} を 100 で割った余りとして以下の形式で出力してください。その後、ただちにプログラムを終了してください。
! S
注意点
- 出力を行うたびに、末尾に改行を入れて標準出力を flush してください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
- 対話の途中で誤った出力形式による出力を行った、あるいはプログラムが途中で終了した場合のジャッジ結果は不定です。
- 解答を出力したらただちにプログラムを終了してください。そうしない場合、ジャッジ結果は不定です。
入出力例
以下は、N=3,L=1,R=5,A=(31,41,59,26,53,58,97,93) の場合の入出力例です。この場合 m=3 であるため、質問を 3 回まで行うことができます。
| 入力 | 出力 | 説明 |
|---|---|---|
3 1 5 | まず整数 N,L,R が与えられます。 | |
? 0 1 | (i,j)=(0,1) として質問を行います。 | |
41 | l=1,r=1 であるため、質問の答えは A_1=41 を 100 で割った余りである 41 です。ジャッジはその値を返します。 | |
? 1 1 | (i,j) = (1,1) として質問を行います。 | |
85 | l=2,r=3 であるため、質問の答えは A_2+A_3=85 を 100 で割った余りである 85 です。ジャッジはその値を返します。 | |
? 1 2 | (i,j) = (1,2) として質問を行います。 | |
11 | l=4,r=5 であるため、質問の答えは A_4+A_5=111 を 100 で割った余りである 11 です。ジャッジはその値を返します。 | |
! 37 | 答えは 37 であるとわかったので、それを出力します。 |
Score : 500 points
Problem Statement
This is an interactive problem (where your program interacts with the judge via input and output).
You are given a positive integer N and integers L and R such that 0 \leq L \leq R < 2^N. The judge has a hidden sequence A = (A_0, A_1, \dots, A_{2^N-1}) consisting of integers between 0 and 99, inclusive.
Your goal is to find the remainder when A_L + A_{L+1} + \dots + A_R is divided by 100. However, you cannot directly know the values of the elements in the sequence A. Instead, you can ask the judge the following question:
- Choose non-negative integers i and j such that 2^i(j+1) \leq 2^N. Let l = 2^i j and r = 2^i (j+1) - 1. Ask for the remainder when A_l + A_{l+1} + \dots + A_r is divided by 100.
Let m be the minimum number of questions required to determine the remainder when A_L + A_{L+1} + \dots + A_R is divided by 100 for any sequence A. You need to find this remainder within m questions.
Constraints
- 1 \leq N \leq 18
- 0 \leq L \leq R \leq 2^N - 1
- All input values are integers.
Input and Output
This is an interactive problem (where your program interacts with the judge via input and output).
First, read the integers N, L, and R from Standard Input:
N L R
Then, repeat asking questions until you can determine the remainder when A_L + A_{L+1} + \dots + A_R is divided by 100. Each question should be printed in the following format:
? i j
Here, i and j must satisfy the following constraints:
- i and j are non-negative integers.
- 2^i(j+1) \leq 2^N
The response to the question will be given in the following format from Standard Input:
T
Here, T is the answer to the question, which is the remainder when A_l + A_{l+1} + \dots + A_r is divided by 100, where l = 2^i j and r = 2^i (j+1) - 1.
If i and j do not satisfy the constraints, or if the number of questions exceeds m, then T will be -1.
If the judge returns -1, your program is already considered incorrect. In this case, terminate the program immediately.
Once you have determined the remainder when A_L + A_{L+1} + \dots + A_R is divided by 100, print the remainder S in the following format and terminate the program immediately:
! S
Notes
- At the end of each output, print a newline and flush Standard Output. Otherwise, the verdict may be TLE.
- If your output is malformed or your program exits prematurely, the verdict will be indeterminate.
- After printing the answer, terminate the program immediately. Otherwise, the verdict will be indeterminate.
Example
Here is an example for N=3, L=1, R=5, and A=(31, 41, 59, 26, 53, 58, 97, 93). In this case, m=3, so you can ask up to three questions.
| Input | Output | Description |
|---|---|---|
3 1 5 | First, the integers N, L, and R are given. | |
? 0 1 | Ask the question with (i, j) = (0, 1). | |
41 | l=1 and r=1, so the response is the remainder when A_1=41 is divided by 100, which is 41. The judge returns this value. | |
? 1 1 | Ask the question with (i, j) = (1, 1). | |
85 | l=2 and r=3, so the response is the remainder when A_2 + A_3 = 85 is divided by 100, which is 85. The judge returns this value. | |
? 1 2 | Ask the question with (i, j) = (1, 2). | |
11 | l=4 and r=5, so the response is the remainder when A_4 + A_5 = 111 is divided by 100, which is 11. The judge returns this value. | |
! 37 | The answer is 37, so print this value. |
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
カードが N 枚あり、i 番目のカードには文字列 S_i が書かれています。
この中からちょうど K 枚選び、好きな順序で繋げてできる文字列のうち辞書順最小のものを求めてください。
制約
- 1 \leq K \leq N \leq 50
- 1 \leq |S_i| \leq 50
- S_i は英小文字からなる
入力
入力は以下の形式で標準入力から与えられる。
N K S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
4 3 ode zaaa r atc
出力例 1
atcoder
カードの中に書かれている文字を、反転させたり並び替えたりすることはできません。
たとえば 1 枚目のカードに書かれている ode を、edo や deo のように使うことはできません。
入力例 2
5 2 z z zzz z zzzzzz
出力例 2
zz
S_i = S_j を満たす i,j(i\neq j) の組が存在することもあります。
Score : 500 points
Problem Statement
We have N cards. The i-th card has a string S_i written on it.
Find the lexicographically smallest string that can be obtained by choosing K of these cards and concatenating them in any order.
Constraints
- 1 \leq K \leq N \leq 50
- 1 \leq |S_i| \leq 50
- S_i consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
N K S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
4 3 ode zaaa r atc
Sample Output 1
atcoder
Note that it is not possible to reverse or permute the string written on a card.
For example, ode written on the first card cannot be used as edo or deo.
Sample Input 2
5 2 z z zzz z zzzzzz
Sample Output 2
zz
There may be a pair i, j (i\neq j) such that S_i = S_j.