Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋君はティーポットです。
高橋君はティーポットなので、紅茶なら喜んで受け入れますが、それ以外の液体を入れようとすると拒否してしまいます。
高橋君に S という名前の液体を入れることが出来るか判定してください。
英小文字からなる長さ N の文字列 S が与えられます。
S が tea で終わる文字列であるかどうかを判定してください。
制約
- 1 \leq N \leq 20
- N は整数
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
S が tea で終わる文字列であれば Yes を、そうでなければ No を出力せよ。
入力例 1
8 greentea
出力例 1
Yes
greentea は tea で終わる文字列です。
入力例 2
6 coffee
出力例 2
No
coffee は tea で終わる文字列ではありません。
入力例 3
3 tea
出力例 3
Yes
入力例 4
1 t
出力例 4
No
Score : 100 points
Problem Statement
Takahashi is a teapot.
Since he is a teapot, he will gladly accept tea, but will refuse any other liquid.
Determine whether you can pour a liquid named S into him.
You are given a string S of length N consisting of lowercase English letters.
Determine whether S is a string that ends with tea.
Constraints
- 1 \leq N \leq 20
- N is an integer.
- S is a string of length N consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
N S
Output
If S is a string that ends with tea, print Yes; otherwise, print No.
Sample Input 1
8 greentea
Sample Output 1
Yes
greentea is a string that ends with tea.
Sample Input 2
6 coffee
Sample Output 2
No
coffee is not a string that ends with tea.
Sample Input 3
3 tea
Sample Output 3
Yes
Sample Input 4
1 t
Sample Output 4
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ 3 の文字列 S が与えられます。
S に 1 度だけ含まれる文字を 1 つ出力してください。
但し、そのような文字が存在しない場合は代わりに -1 と出力してください。
制約
- S は英小文字のみからなる 3 文字の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。正解が複数ある場合、どれを出力してもよい。
入力例 1
pop
出力例 1
o
pop に o は 1 度だけ含まれます。
入力例 2
abc
出力例 2
a
abc に a, b, c はどれも 1 度だけ含まれるので、どれを出力しても構いません。
入力例 3
xxx
出力例 3
-1
xxx に 1 度だけ含まれる文字はありません。
Score : 100 points
Problem Statement
You are given a string S of length 3.
Print a character that occurs only once in S.
If there is no such character, print -1 instead.
Constraints
- S is a string of length 3 consisting of lowercase English letters.
Input
Input is given from Standard Input in the following format:
S
Output
Print the answer. If multiple solutions exist, you may print any of them.
Sample Input 1
pop
Sample Output 1
o
o occurs only once in pop.
Sample Input 2
abc
Sample Output 2
a
a, b, and c occur once each in abc, so you may print any of them.
Sample Input 3
xxx
Sample Output 3
-1
No character occurs only once in xxx.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 150 点
問題文
2N 人の人が横一列に並んでおり、左から i 番目の人は色 A_i の服を着ています。ここで、服の色は 1 から N の N 色であり、それぞれの色についてちょうど 2 人の人がその色の服を着ています。
i=1,2,\ldots,N のうち、以下の条件を満たすものは何通りあるか求めてください。
- 色 i の服を着た二人の人の間にはちょうど一人いる。
制約
- 2\leq N\leq 100
- 1\leq A_i \leq N
- A には 1 以上 N 以下の整数全てがそれぞれ 2 個ずつ含まれる
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
A_1 A_2 \ldots A_{2N}
出力
答えを出力せよ。
入力例 1
3 1 2 1 3 2 3
出力例 1
2
条件を満たす i は 1 と 3 の 2 個です。
実際、色 1 の服を着ているのは左から 1 番目の人と左から 3 番目の人で、間にちょうど一人います。
入力例 2
2 1 1 2 2
出力例 2
0
条件を満たす i が存在しない場合もあります。
入力例 3
4 4 3 2 3 2 1 4 1
出力例 3
3
Score : 150 points
Problem Statement
There are 2N people standing in a row, and the person at the i-th position from the left is wearing clothes of color A_i. Here, the clothes have N colors from 1 to N, and exactly two people are wearing clothes of each color.
Find how many of the integers i=1,2,\ldots,N satisfy the following condition:
- There is exactly one person between the two people wearing clothes of color i.
Constraints
- 2 \leq N \leq 100
- 1 \leq A_i \leq N
- Each integer from 1 through N appears exactly twice in A.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
A_1 A_2 \ldots A_{2N}
Output
Print the answer.
Sample Input 1
3 1 2 1 3 2 3
Sample Output 1
2
There are two values of i that satisfy the condition: 1 and 3.
In fact, the people wearing clothes of color 1 are at the 1st and 3rd positions from the left, with exactly one person in between.
Sample Input 2
2 1 1 2 2
Sample Output 2
0
There may be no i that satisfies the condition.
Sample Input 3
4 4 3 2 3 2 1 4 1
Sample Output 3
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
高橋君は改札機の利用履歴を集計しました。 しかし、高橋君はうっかりいくつかの入退場記録を消してしまいました。 高橋君は消してしまった記録の復元を試みようとしています。
i, o のみからなる文字列 S が与えられます。
S の任意の位置に文字を 0 文字以上挿入することで、変更後の文字列が以下の条件を満たすようにしたいです。
- 長さが偶数であり、奇数文字目が
iで偶数文字目がoである。
挿入する必要のある文字数の最小値を求めて下さい。なお、問題の制約下で、有限個の文字を適切に挿入することで、S が条件をみたすようにできることが証明できます。
制約
- S は
i,oからなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
ioi
出力例 1
1
3 文字目のあとに o を挿入して ioio とすることで、条件を満たすことができます。0 文字以下の挿入で条件を満たすことはできません。
入力例 2
iioo
出力例 2
2
1 文字目のあとに o を、3 文字目のあとに i を挿入することで、条件を満たすことができます。1 文字以下の挿入で条件を満たすことはできません。
入力例 3
io
出力例 3
0
S がすでに条件を満たします。
Score : 250 points
Problem Statement
Takahashi aggregated usage records from ticket gates. However, he accidentally erased some records of entering and exiting stations. He is trying to restore the erased records.
You are given a string S consisting of i and o. We want to insert zero or more characters at arbitrary positions in S so that the resulting string satisfies the following conditions:
- Its length is even, and every odd-numbered (1st, 3rd, ...) character is
iwhile every even-numbered (2nd, 4th, ...) character iso.
Find the minimum number of characters that need to be inserted. It can be proved under the constraints of this problem that by inserting an appropriate finite number of characters, S can be made to satisfy the conditions.
Constraints
- S is a string of length between 1 and 100, consisting of
iando.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
ioi
Sample Output 1
1
We can insert o after the 3rd character to form ioio to satisfy the conditions. The conditions cannot be satisfied by inserting zero or fewer characters.
Sample Input 2
iioo
Sample Output 2
2
We can insert o after the 1st character and i after the 3rd character to satisfy the conditions. The conditions cannot be satisfied by inserting one or fewer characters.
Sample Input 3
io
Sample Output 3
0
S already satisfies the conditions.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
非負整数 K に対して、以下のようにレベル K のカーペットを定義します。
- レベル 0 のカーペットは黒いマス 1 個のみからなる 1\times 1 のグリッドである。
- K>0 のとき、レベル K のカーペットは 3^K\times 3^K のグリッドである。
このグリッドを 3^{K-1}\times 3^{K-1} のブロック 9 個に分割したとき、
- 中央のブロックはすべて白いマスからなる。
- 他の 8 個のブロックは、レベル (K-1) のカーペットである。
非負整数 N が与えられます。
レベル N のカーペットを出力の形式に従って出力してください。
制約
- 0\leq N \leq 6
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
3^N 行出力せよ。
i 行目 (1\leq i\leq 3^N) には、. と # からなる長さ 3^N の文字列 S_i を出力せよ。
S_i の j 文字目 (1\leq j\leq 3^N) は、レベル N のカーペットの上から i 行目かつ左から j 列目のマスが黒いとき # とし、白いとき . とせよ。
入力例 1
1
出力例 1
### #.# ###
レベル 1 のカーペットは次のような 3\times 3 のグリッドです。

これを出力形式にしたがって出力すると出力例のようになります。
入力例 2
2
出力例 2
######### #.##.##.# ######### ###...### #.#...#.# ###...### ######### #.##.##.# #########
レベル 2 のカーペットは 9\times 9 のグリッドとなります。
Score : 250 points
Problem Statement
For a non-negative integer K, we define a level-K carpet as follows:
- A level-0 carpet is a 1 \times 1 grid consisting of a single black cell.
- For K > 0, a level-K carpet is a 3^K \times 3^K grid. When this grid is divided into nine 3^{K-1} \times 3^{K-1} blocks:
- The central block consists entirely of white cells.
- The other eight blocks are level-(K-1) carpets.
You are given a non-negative integer N.
Print a level-N carpet according to the specified format.
Constraints
- 0 \leq N \leq 6
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print 3^N lines.
The i-th line (1 \leq i \leq 3^N) should contain a string S_i of length 3^N consisting of . and #.
The j-th character of S_i (1 \leq j \leq 3^N) should be # if the cell at the i-th row from the top and j-th column from the left of a level-N carpet is black, and . if it is white.
Sample Input 1
1
Sample Output 1
### #.# ###
A level-1 carpet is a 3 \times 3 grid as follows:

When output according to the specified format, it looks like the sample output.
Sample Input 2
2
Sample Output 2
######### #.##.##.# ######### ###...### #.#...#.# ###...### ######### #.##.##.# #########
A level-2 carpet is a 9 \times 9 grid.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
長さ N の整数列 A,B が与えられます。1 以上 N 以下の整数 i,j を選んで、 A_i + B_j の値を最大化してください。
制約
- 1 \leq N \leq 5 \times 10^5
- |A_i| \leq 10^9\,(i=1,2,\dots,N)
- |B_j| \leq 10^9\,(j=1,2,\dots,N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
A_i + B_j の最大値を出力せよ。
入力例 1
2 -1 5 3 -7
出力例 1
8
(i,j)=(1,1),(1,2),(2,1),(2,2) に対する A_i+B_j の値はそれぞれ 2,-8,8,-2 であり、(i,j)=(2,1) が最大値 8 を達成します。
入力例 2
6 15 12 3 -13 -1 -19 7 17 -13 -10 18 4
出力例 2
33
Score : 250 points
Problem Statement
You are given two integer sequences A and B, each of length N. Choose integers i, j (1 \leq i, j \leq N) to maximize the value of A_i + B_j.
Constraints
- 1 \leq N \leq 5 \times 10^5
- |A_i| \leq 10^9 (i=1,2,\dots,N)
- |B_j| \leq 10^9 (j=1,2,\dots,N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
Output
Print the maximum possible value of A_i + B_j.
Sample Input 1
2 -1 5 3 -7
Sample Output 1
8
For (i,j) = (1,1), (1,2), (2,1), (2,2), the values of A_i + B_j are 2, -8, 8, -2 respectively, and (i,j) = (2,1) achieves the maximum value 8.
Sample Input 2
6 15 12 3 -13 -1 -19 7 17 -13 -10 18 4
Sample Output 2
33
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
0 および 1 からなる長さ N の文字列 S が与えられます。
あなたは S に対して以下の操作を好きな回数(0 回でもよい)行うことができます。
- 先頭または末尾の 1 文字を削除し、その文字を反転して(
0ならば1に、1ならば0に変えて)、好きな位置に挿入し直す。 より厳密には、r(0)=1,r(1)=0として、以下のいずれかを行う。(ここで、S_i は S の i 文字目を表す。)- 好きな i\ (1\leq i\leq N) を選んで、S を S_2\dots S_i\,r(S_1)\,S_{i+1}\dots S_N に変更する。
- 好きな i\ (0\leq i\leq N-1) を選んで、S を S_1\dots S_i\,r(S_N)\,S_{i+1}\dots S_{N-1} に変更する。
S の全ての文字を同じ文字にするために必要な操作回数の最小値を求めてください。 なお、そのような操作列が必ず存在することは証明可能です。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T\leq 2\times 10^5
- 2\leq N \leq 5\times 10^5
- T,N は整数
- S は
0および1からなる長さ N の文字列 - 全てのテストケースにおける N の総和は 5\times 10^5 以下
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
\text{case}_i は i 番目のテストケースを表す。各テストケースは以下の形式で与えられる。
N S
出力
T 行出力せよ。i 行目 (1\leq i\leq T) には i 番目のテストケースに対する答えを出力せよ。
入力例 1
3 5 01001 3 000 15 110010111100101
出力例 1
4 0 16
1 番目のテストケースについて、例えば以下のように 4 回の操作をすることで S の全ての文字を 0 にすることができます。
3 回以下の操作で S の全ての文字を同じ文字にすることはできないので、答えは 4 です。
- 先頭の文字
0を削除し、1を(削除した後の S における)1 文字目と 2 文字目の間に挿入する。S=11001になる。 - 先頭の文字
1を削除し、0を(削除した後の S における)2 文字目と 3 文字目の間に挿入する。S=10001になる。 - 末尾の文字
1を削除し、0を(削除した後の S における)末尾に挿入する。S=10000になる。 - 先頭の文字
1を削除し、0を(削除した後の S における)先頭に挿入する。S=00000になる。
2 番目のテストケースについて、はじめから S の全ての文字が同じ文字であるため、操作は 1 回も必要ありません。
Score : 400 points
Problem Statement
You are given a string S of length N consisting of 0 and 1.
You can perform the following operation on S any number of times (possibly zero):
- Delete the first or last character, flip it (change
0to1or1to0), and insert it back at any position. More formally, let r(0)=1and r(1)=0, and perform one of the following: (Here, S_i denotes the i-th character of S.)- Choose any i\ (1\leq i\leq N) and change S to S_2\dots S_i\,r(S_1)\,S_{i+1}\dots S_N.
- Choose any i\ (0\leq i\leq N-1) and change S to S_1\dots S_i\,r(S_N)\,S_{i+1}\dots S_{N-1}.
Find the minimum number of operations required to make all characters of S the same. It can be proved that such a sequence of operations always exists.
You are given T test cases, so solve each of them.
Constraints
- 1\leq T\leq 2\times 10^5
- 2\leq N \leq 5\times 10^5
- T and N are integers.
- S is a string of length N consisting of
0and1. - The sum of N over all test cases is at most 5\times 10^5.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
\text{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 (1\leq i\leq T) should contain the answer for the i-th test case.
Sample Input 1
3 5 01001 3 000 15 110010111100101
Sample Output 1
4 0 16
For the first test case, for example, you can make all characters of S into 0 with four operations as follows.
It is impossible to make all characters of S the same with three or fewer operations, so the answer is 4.
- Delete the first character
0, and insert1between the 1st and 2nd characters (in S after deletion). S becomes11001. - Delete the first character
1, and insert0between the 2nd and 3rd characters (in S after deletion). S becomes10001. - Delete the last character
1, and insert0at the end (in S after deletion). S becomes10000. - Delete the first character
1, and insert0at the beginning (in S after deletion). S becomes00000.
For the second test case, all characters of S are the same from the beginning, so no operation is needed.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
変数 X と、X の値を変更する N 種類の操作があります。操作 i は整数の組 (T_i,A_i) で表され、意味は次の通りです。
- T_i=1 のとき、X の値を X\ {\rm and}\ A_i に置き換える。
- T_i=2 のとき、X の値を X\ {\rm or}\ A_i に置き換える。
- T_i=3 のとき、X の値を X\ {\rm xor}\ A_i に置き換える。
変数 X を値 C で初期化した状態から、以下の処理を順に実行してください。
- 操作 1 を行い、操作後の X の値を出力する。
- 続けて、操作 1,2 を順に行い、操作後の X の値を出力する。
- 続けて、操作 1,2,3 を順に行い、操作後の X の値を出力する。
- \vdots
- 続けて、操作 1,2,\ldots,N を順に行い、操作後の X の値を出力する。
{\rm and}, {\rm or}, {\rm xor} とは
非負整数 A, B の {\rm and}, {\rm or}, {\rm xor} は、以下のように定義されます。- A\ {\rm and}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち両方が 1 であれば 1、そうでなければ 0 である。
- A\ {\rm or}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも一方が 1 であれば 1、そうでなければ 0 である。
- A\ {\rm xor}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうちちょうど一方が 1 であれば 1、そうでなければ 0 である。
制約
- 1 \leq N \leq 2\times 10^5
- 1\leq T_i \leq 3
- 0\leq A_i \lt 2^{30}
- 0\leq C \lt 2^{30}
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N C T_1 A_1 T_2 A_2 \vdots T_N A_N
出力
問題文中の指示に従って N 行出力せよ。
入力例 1
3 10 3 3 2 5 1 12
出力例 1
9 15 12
最初、X の値は 10 です。
- 操作 1 を行うと X の値は 9 になります。
- 続けて操作 1 を行うと X の値は 10 になり、さらに操作 2 を行うと 15 になります。
- 続けて操作 1 を行うと X の値は 12 になり、さらに操作 2 を行うと 13 に、さらに続けて操作 3 を行うと 12 になります。
入力例 2
9 12 1 1 2 2 3 3 1 4 2 5 3 6 1 7 2 8 3 9
出力例 2
0 2 1 0 5 3 3 11 2
Score : 500 points
Problem Statement
We have a variable X and N kinds of operations that change the value of X. Operation i is represented as a pair of integers (T_i,A_i), and is the following operation:
- if T_i=1, it replaces the value of X with X\ {\rm and}\ A_i;
- if T_i=2, it replaces the value of X with X\ {\rm or}\ A_i;
- if T_i=3, it replaces the value of X with X\ {\rm xor}\ A_i.
Initialize X with the value of C and execute the following procedures in order:
- Perform Operation 1, and then print the resulting value of X.
- Next, perform Operation 1, 2 in this order, and then print the value of X.
- Next, perform Operation 1, 2, 3 in this order, and then print the value of X.
- \vdots
- Next, perform Operation 1, 2, \ldots, N in this order, and then print the value of X.
What are {\rm and}, {\rm or}, {\rm xor}?
The {\rm and}, {\rm or}, {\rm xor} of non-negative integers A and B are defined as follows:
- When A\ {\rm and}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if both of the digits in that place of A and B are 1, and 0 otherwise.
- When A\ {\rm or}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if at least one of the digits in that place of A and B is 1, and 0 otherwise.
- When A\ {\rm xor}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of the digits in that place of A and B is 1, and 0 otherwise.
Constraints
- 1 \leq N \leq 2\times 10^5
- 1\leq T_i \leq 3
- 0\leq A_i \lt 2^{30}
- 0\leq C \lt 2^{30}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N C T_1 A_1 T_2 A_2 \vdots T_N A_N
Output
Print N lines, as specified in the Problem Statement.
Sample Input 1
3 10 3 3 2 5 1 12
Sample Output 1
9 15 12
The initial value of X is 10.
- Operation 1 changes X to 9.
- Next, Operation 1 changes X to 10, and then Operation 2 changes it to 15.
- Next, Operation 1 changes X to 12, and then Operation 2 changes it to 13, and then Operation 3 changes it to 12.
Sample Input 2
9 12 1 1 2 2 3 3 1 4 2 5 3 6 1 7 2 8 3 9
Sample Output 2
0 2 1 0 5 3 3 11 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 550 点
問題文
正整数 N,K および長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。
\displaystyle \sum_{1\leq l\leq r\leq N} \Bigg(\sum_{l\leq i\leq r} A_i\Bigg)^K の値を 998244353 で割った余りを求めてください。
制約
- 1\leq N \leq 2\times 10^5
- 1\leq K \leq 10
- 0\leq A_i < 998244353
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
3 2 3 1 2
出力例 1
75
求める値は A_1^2+A_2^2+A_3^2+(A_1+A_2)^2+(A_2+A_3)^2+(A_1+A_2+A_3)^2=3^2+1^2+2^2+4^2+3^2+6^2=75 です。
入力例 2
1 10 0
出力例 2
0
入力例 3
10 5 91 59 85 60 57 72 12 3 27 16
出力例 3
428633385
998244353 で割った余りを求めることに注意してください。
Score : 550 points
Problem Statement
You are given positive integers N, K, and an integer sequence of length N: A = (A_1, A_2, \dots, A_N).
Find \displaystyle \sum_{1\leq l\leq r\leq N} \Bigg(\sum_{l\leq i\leq r} A_i\Bigg)^K, modulo 998244353.
Constraints
- 1\leq N \leq 2\times 10^5
- 1\leq K \leq 10
- 0 \leq A_i < 998244353
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
3 2 3 1 2
Sample Output 1
75
The value is A_1^2+A_2^2+A_3^2+(A_1+A_2)^2+(A_2+A_3)^2+(A_1+A_2+A_3)^2=3^2+1^2+2^2+4^2+3^2+6^2=75.
Sample Input 2
1 10 0
Sample Output 2
0
Sample Input 3
10 5 91 59 85 60 57 72 12 3 27 16
Sample Output 3
428633385
Be sure to find the sum modulo 998244353.