Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
6 桁の正整数 N が与えられます。
この整数が以下の条件を全て満たすか判定してください。
- N の各桁のうち、 1 は丁度 1 つである。
- N の各桁のうち、 2 は丁度 2 つである。
- N の各桁のうち、 3 は丁度 3 つである。
制約
- N は 100000 \le N \le 999999 を満たす整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
N が問題文中の条件を全て満たすなら Yes 、そうでないなら No と 1 行に出力せよ。
入力例 1
123233
出力例 1
Yes
123233 は問題文中の条件を満たすので、 Yes と出力します。
入力例 2
123234
出力例 2
No
123234 は問題文中の条件を満たさないので、 No と出力します。
入力例 3
323132
出力例 3
Yes
入力例 4
500000
出力例 4
No
Score : 100 points
Problem Statement
You are given a 6-digit positive integer N.
Determine whether N satisfies all of the following conditions.
- Among the digits of N, the digit 1 appears exactly once.
- Among the digits of N, the digit 2 appears exactly twice.
- Among the digits of N, the digit 3 appears exactly three times.
Constraints
- N is an integer satisfying 100000 \le N \le 999999.
Input
The input is given from Standard Input in the following format:
N
Output
Print Yes if N satisfies all the conditions described in the problem statement, and No otherwise, in one line.
Sample Input 1
123233
Sample Output 1
Yes
123233 satisfies the conditions in the problem statement, so print Yes.
Sample Input 2
123234
Sample Output 2
No
123234 does not satisfy the conditions in the problem statement, so print No.
Sample Input 3
323132
Sample Output 3
Yes
Sample Input 4
500000
Sample Output 4
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
(1,2,…,N) を並び替えた数列 P と整数 X が与えられます。 数列 P の i 番目の項の値は P_i です。 P_k = X を満たす k を出力してください。
制約
- 1 \leq N \leq 100
- 1 \leq X \leq N
- P は (1,2,…,N) を並び替えてできる数列
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X P_1 P_2 \ldots P_N
出力
答えを出力せよ。
入力例 1
4 3 2 3 1 4
出力例 1
2
P = (2,3,1,4) なので、P_2 = 3 です。したがって、2 を出力します。
入力例 2
5 2 3 5 1 4 2
出力例 2
5
入力例 3
6 6 1 2 3 4 5 6
出力例 3
6
Score : 100 points
Problem Statement
You are given a sequence P that is a permutation of (1,2,…,N), and an integer X. The i-th term of P has a value of P_i. Print k such that P_k = X.
Constraints
- 1 \leq N \leq 100
- 1 \leq X \leq N
- P is a permutation of (1,2,…,N).
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N X P_1 P_2 \ldots P_N
Output
Print the answer.
Sample Input 1
4 3 2 3 1 4
Sample Output 1
2
We have P = (2,3,1,4), so P_2 = 3. Thus, you should print 2.
Sample Input 2
5 2 3 5 1 4 2
Sample Output 2
5
Sample Input 3
6 6 1 2 3 4 5 6
Sample Output 3
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
拡張 A 文字列、拡張 B 文字列、拡張 C 文字列および拡張 ABC 文字列を以下のように定義します。
- 文字列 S が拡張 A 文字列であるとは、S のすべての文字が
Aであることをいいます。 - 文字列 S が拡張 B 文字列であるとは、S のすべての文字が
Bであることをいいます。 - 文字列 S が拡張 C 文字列であるとは、S のすべての文字が
Cであることをいいます。 - 文字列 S が拡張 ABC 文字列であるとは、ある拡張 A 文字列 S _ A 、拡張 B 文字列 S _ B 、拡張 C 文字列 S _ C が存在して、S _ A,S _ B,S _ C をこの順に連結した文字列が S と等しいことをいいます。
例えば、ABC や A 、AAABBBCCCCCCC などは拡張 ABC 文字列ですが、ABBAAAC 、BBBCCCCCCCAAA などは拡張 ABC 文字列ではありません。
空文字列は拡張 A 文字列でも拡張 B 文字列でも拡張 C 文字列でもあることに注意してください。
A, B, C からなる文字列 S が与えられます。
S が拡張 ABC 文字列ならば Yes を、そうでなければ No を出力してください。
制約
- S は
A,B,Cからなる文字列 - 1\leq|S|\leq 100\ (|S| は文字列 S の長さ)
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S が拡張 ABC 文字列ならば Yes を、そうでなければ No を出力せよ。
入力例 1
AAABBBCCCCCCC
出力例 1
Yes
AAABBBCCCCCCC は長さ 3 の拡張 A 文字列 AAA 、長さ 3 の拡張 B 文字列 BBB 、長さ 7 の拡張 C 文字列 CCCCCCC をこの順に連結した文字列なので、拡張 ABC 文字列です。
よって、Yes を出力してください。
入力例 2
ACABABCBC
出力例 2
No
どのような拡張 A 文字列 S _ A, 拡張 B 文字列 S _ B, 拡張 C 文字列 S _ C についても、S _ A,S _ B,S _ C をこの順に連結した文字列が ACABABCBC と等しくなることはありません。
よって、No を出力してください。
入力例 3
A
出力例 3
Yes
入力例 4
ABBBBBBBBBBBBBCCCCCC
出力例 4
Yes
Score: 200 points
Problem Statement
We define Extended A strings, Extended B strings, Extended C strings, and Extended ABC strings as follows:
- A string S is an Extended A string if all characters in S are
A. - A string S is an Extended B string if all characters in S are
B. - A string S is an Extended C string if all characters in S are
C. - A string S is an Extended ABC string if there is an Extended A string S_A, an Extended B string S_B, and an Extended C string S_C such that the string obtained by concatenating S_A, S_B, S_C in this order equals S.
For example, ABC, A, and AAABBBCCCCCCC are Extended ABC strings, but ABBAAAC and BBBCCCCCCCAAA are not.
Note that the empty string is an Extended A string, an Extended B string, and an Extended C string.
You are given a string S consisting of A, B, and C.
If S is an Extended ABC string, print Yes; otherwise, print No.
Constraints
- S is a string consisting of
A,B, andC. - 1\leq|S|\leq 100 (|S| is the length of the string S.)
Input
The input is given from Standard Input in the following format:
S
Output
If S is an Extended ABC string, print Yes; otherwise, print No.
Sample Input 1
AAABBBCCCCCCC
Sample Output 1
Yes
AAABBBCCCCCCC is an Extended ABC string because it is a concatenation of an Extended A string of length 3, AAA, an Extended B string of length 3, BBB, and an Extended C string of length 7, CCCCCCC, in this order.
Thus, print Yes.
Sample Input 2
ACABABCBC
Sample Output 2
No
There is no triple of Extended A string S_A, Extended B string S_B, and Extended C string S_C such that the string obtained by concatenating S_A, S_B, and S_C in this order equals ACABABCBC.
Therefore, print No.
Sample Input 3
A
Sample Output 3
Yes
Sample Input 4
ABBBBBBBBBBBBBCCCCCC
Sample Output 4
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
注:この問題は F 問題とほぼ同じ設定です。本文中で太字で示されている部分および制約のみが異なります。
あなたはあるリングを両手で握っています。 このリングは N\ (N\geq 3) 個のパーツ 1,2,\dots,N によって構成されており、パーツ i とパーツ i+1 (1\leq i\leq N-1)、およびパーツ 1 とパーツ N がそれぞれ隣接しています。
最初、左手はパーツ 1 を、右手はパーツ 2 を握っています。 あなたは、1 回の 操作 で以下のことを行えます。
- 片方の手を、今握っているパーツに隣接するいずれかのパーツに移動する。ただし、移動先にもう一方の手がない場合に限る。
以下の図は、初期状態およびそこから行える操作と行えない操作の例を示したもので、リングの各パーツに書き込まれた数はそのパーツの番号を、L と書かれた丸は左手を、R と書かれた丸は右手を示しています。

あなたは今から与えられる Q 個の指示に順番に従う必要があります。 i\ (1\leq i\leq Q) 個目の指示は文字 H_i および整数 T_i によって表され、その意味は以下の通りです:
- 操作を何回か(0 回でもよい)行うことで、H_i が
Lならば左手、Rならば右手が、パーツ T_i を握っている状態にする。 このとき、H_i によって指定された手ではない方の手を 動かしてはならない。
なお、達成可能な指示しか与えられないことが保証されます。
詳細
この問題の設定の下では、各 i について、i 個目の指示に従う直前でのそれぞれの手の位置が一意に定まることが証明できます。 このとき、左手の位置をパーツ l_i、右手の位置をパーツ r_i とおくと、H_i=L ならば T_i\neq r_i が、H_i= R ならば T_i\neq l_i がそれぞれ保証されます。
すべての指示に従うために必要な操作回数の合計の最小値を求めてください。
制約
- 3\leq N \leq 100
- 1\leq Q \leq 100
- H_i は
LまたはR - 1\leq T_i\leq N
- N,Q,T_i は整数
- 達成可能な指示しか与えられない(詳細は問題文を参照してください)
入力
入力は以下の形式で標準入力から与えられる。
N Q H_1 T_1 H_2 T_2 \vdots H_Q T_Q
出力
すべての指示に従うために必要な操作回数の合計の最小値を出力せよ。
入力例 1
6 3 R 4 L 5 R 6
出力例 1
8

以下のように操作を行うことで、Q 個の指示すべてに順番に従うことができます。
- 右手をパーツ 2\rightarrow 3\rightarrow 4 と移動させることで、1 番目の指示に従う。
- 左手をパーツ 1\rightarrow 6\rightarrow 5 と移動させることで、2 番目の指示に従う。
- 右手をパーツ 4\rightarrow 3\rightarrow 2\rightarrow 1\rightarrow 6 と移動させることで、3 番目の指示に従う。
このとき行う操作回数の合計は 2+2+4=8 であり、これが最小です。 (3 番目の指示に従う際に右手をパーツ 4\rightarrow 5\rightarrow 6 と移動させることはできないことに注意してください。)
入力例 2
100 2 L 1 R 2
出力例 2
0
操作を 1 度も行わずに指示に従うことができる場合もあります。
入力例 3
30 8 R 23 R 26 R 29 L 20 R 29 R 19 L 7 L 16
出力例 3
92
Score : 250 points
Problem Statement
Note: This problem has almost the same setting as Problem F. Only the parts in bold in the main text and constraints differ.
You are holding a ring with both hands. This ring consists of N\ (N \geq 3) parts numbered 1,2,\dots,N, where parts i and i+1 (1 \leq i \leq N-1) are adjacent, and parts 1 and N are also adjacent.
Initially, your left hand is holding part 1, and your right hand is holding part 2. In one operation, you can do the following:
- Move one of your hands to an adjacent part of the part it is currently holding. However, you can do this only if the other hand is not on the destination part.
The following figure shows the initial state and examples of operations that can and cannot be made from there. The number written on each part of the ring represents the part number, and the circles labeled L and R represent your left and right hands, respectively.

You need to follow Q instructions given to you in order. The i-th (1 \leq i \leq Q) instruction is represented by a character H_i and an integer T_i, meaning the following:
- Perform some number of operations (possibly zero) so that your left hand (if H_i is
L) or your right hand (if H_i isR) is holding part T_i. Here, you must not move the other hand not specified by H_i.
It is guaranteed that only achievable instructions are given.
Details
Under the settings of this problem, it can be proved that the positions of both hands are uniquely determined just before following the i-th instruction for each i. At that time, if we denote the positions of the left and right hands as parts l_i and r_i, respectively, it is guaranteed that T_i \neq r_i when H_i isL, and T_i \neq l_i when H_i is R.
Find the minimum total number of operations required to follow all the instructions.
Constraints
- 3 \leq N \leq 100
- 1 \leq Q \leq 100
- H_i is
LorR. - 1 \leq T_i \leq N
- N, Q, and T_i are integers.
- Only achievable instructions are given (see the problem statement for details).
Input
The Input is given from Standard Input in the following format:
N Q H_1 T_1 H_2 T_2 \vdots H_Q T_Q
Output
Print the minimum total number of operations required to follow all the instructions.
Sample Input 1
6 3 R 4 L 5 R 6
Sample Output 1
8

By performing the following operations, you can follow all Q instructions in order.
- Move your right hand as part 2 \rightarrow 3 \rightarrow 4 to follow the first instruction.
- Move your left hand as part 1 \rightarrow 6 \rightarrow 5 to follow the second instruction.
- Move your right hand as part 4 \rightarrow 3 \rightarrow 2 \rightarrow 1 \rightarrow 6 to follow the third instruction.
In this case, the total number of operations is 2+2+4=8, which is the minimum. (Note that when following the third instruction, you cannot move your right hand as part 4 \rightarrow 5 \rightarrow 6.)
Sample Input 2
100 2 L 1 R 2
Sample Output 2
0
There are cases where you can follow the instructions without performing any operations.
Sample Input 3
30 8 R 23 R 26 R 29 L 20 R 29 R 19 L 7 L 16
Sample Output 3
92
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
N 種類の薬品 1,2,\dots,N があります。あなたの目標はこれらが全て混ざった状態にすることです。
0, 1 からなる長さ 2^N-1 の文字列 S が与えられます。この文字列は次の情報を表します。
- まず、 1 種類以上の薬品が混ざった状態 i ( 1 \le i \le 2^N-1 ) を次のように定義する。
- i を 2 進法で表記した際に下から k ( 1 \le k \le N ) 桁目が 1 である、またその時に限り、薬品 k が含まれている。
- 例えば、 13 を 2 進法で表記すると 1101_{(2)} となるため、状態 13 は薬品 1,3,4 が混ざった状態を表現します。
- S の i 文字目が
0であるとき、状態 i は 安全 である。 - S の i 文字目が
1であるとき、状態 i は 危険 である。
あなたは次の操作を使って薬品を混ぜ合わせます。
- まず、空の瓶を用意する。
- 次に、以下を繰り返す。
- まだ瓶に注いでいない薬品を 1 種類選択し、瓶に注ぐ。
- この時、瓶の中で混ざった薬品が危険な状態であってはならない。
この操作によって全ての薬品が混ざった状態を作れるか判定してください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- T は 1 以上 40000 以下の整数
- N は 1 以上 18 以下の整数
- S は
0,1からなる長さ 2^N-1 の文字列 - ひとつの入力に含まれる |S| = 2^N-1 の総和は 5 \times 10^5 を超えない
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i は i 番目のテストケースを表す。 各テストケースは以下の形式で与えられる。
N S
出力
T 行出力せよ。i 行目 には i 番目のテストケースに対する答えを出力せよ。
各テストケースについて、全ての薬品が混ざった状態を作れるなら Yes 、作れないなら No と出力せよ。
入力例 1
5 3 0010000 3 0010110 1 1 2 100 4 001110010101110
出力例 1
Yes No No Yes Yes
この入力には 5 個のテストケースが含まれます。
1 番目のケースは次の通りです。
- 3 種類の薬品が存在する。
- 薬品 1,2 が混ざった状態 3 のみが危険で、他の状態は安全である。
- 例えば、以下の手順で全ての薬品が混ざった状態を作ることができます。
- はじめに、瓶に薬品 2 を注ぐ。瓶の中で薬品 2 のみが混ざっており、これは状態 2 なので安全である。
- 次に、瓶に薬品 3 を注ぐ。瓶の中で薬品 2,3 が混ざっており、これは状態 6 なので安全である。
- 最後に、瓶に薬品 1 を注ぐ。瓶の中で薬品 1,2,3 が混ざっており、これは状態 7 なので安全である。
2 番目のケースは次の通りです。
- 3 種類の薬品が存在する。
- 薬品 1,2 が混ざった状態 3 、薬品 1,3 が混ざった状態 5 、薬品 2,3 が混ざった状態 6 が危険で、他の状態は安全である。
- このケースについて、全ての薬品が混ざった状態を作ることはできません。
3 番目のケースは次の通りです。
- 1 種類の薬品が存在する。
- 薬品 1 のみが混ざった状態 1 が危険であるため、全ての薬品が混ざった状態を作ることはできません。
Score : 350 points
Problem Statement
There are N types of chemicals 1,2,\dots,N. Your goal is to create a state where all of them are mixed.
You are given a string S of length 2^N-1 consisting of 0 and 1, which represents the following information:
- First, define state i (1 \le i \le 2^N-1) where one or more types of chemicals are mixed as follows:
- When the k-th digit (1 \le k \le N) from the bottom in the binary representation of i is 1, and only then, chemical k is included.
- For example, 13 in binary is 1101_{(2)}, so state 13 represents a state where chemicals 1,3,4 are mixed.
- When the i-th character of S is
0, state i is safe. - When the i-th character of S is
1, state i is dangerous.
You mix chemicals using the following operation:
- First, prepare an empty bottle.
- Next, repeat the following:
- Choose one type of chemical that has not yet been poured into the bottle and pour it into the bottle.
- At this time, the chemicals mixed in the bottle must not be in a dangerous state.
Determine whether this operation can create a state where all chemicals are mixed.
You are given T test cases; solve each of them.
Constraints
- T is an integer between 1 and 40000, inclusive.
- N is an integer between 1 and 18, inclusive.
- S is a string of length 2^N-1 consisting of
0and1. - The sum of |S| = 2^N-1 in each input does not exceed 5 \times 10^5.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i represents the i-th test case. Each test case is given in the following format:
N S
Output
Output T lines. The i-th line should contain the answer for the i-th test case.
For each test case, if it is possible to create a state where all chemicals are mixed, print Yes; otherwise, print No.
Sample Input 1
5 3 0010000 3 0010110 1 1 2 100 4 001110010101110
Sample Output 1
Yes No No Yes Yes
This input contains five test cases.
The 1st case is as follows:
- There are three types of chemicals.
- Only state 3 where chemicals 1,2 are mixed is dangerous, and the other states are safe.
- For example, you can create a state where all chemicals are mixed with the following procedure:
- First, pour chemical 2 into the bottle. Only chemical 2 is mixed in the bottle, which is state 2, so it is safe.
- Next, pour chemical 3 into the bottle. Chemicals 2,3 are mixed in the bottle, which is state 6, so it is safe.
- Finally, pour chemical 1 into the bottle. Chemicals 1,2,3 are mixed in the bottle, which is state 7, so it is safe.
The 2nd case is as follows:
- There are three types of chemicals.
- State 3 where chemicals 1,2 are mixed, state 5 where chemicals 1,3 are mixed, and state 6 where chemicals 2,3 are mixed are dangerous, and the other states are safe.
- For this case, it is impossible to create a state where all chemicals are mixed.
The 3rd case is as follows:
- There is one type of chemical.
- Since state 1 where only chemical 1 is mixed is dangerous, it is impossible to create a state where all chemicals are mixed.