実行時間制限: 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 が与えられます。
A\leq B\leq C かつ ABC\leq N であるような正の整数の組 (A,B,C) の個数を求めてください。
なお、制約の条件下で答えは 2^{63} 未満であることが保証されます。
制約
- 1 \leq N \leq 10^{11}
- N は整数である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
4
出力例 1
5
条件を満たす組は (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2) の 5 つです。
入力例 2
100
出力例 2
323
入力例 3
100000000000
出力例 3
5745290566750
Score : 300 points
Problem Statement
You are given a positive integer N.
Find the number of triples of positive integers (A, B, C) such that A\leq B\leq C and ABC\leq N.
The Constraints guarantee that the answer is less than 2^{63}.
Constraints
- 1 \leq N \leq 10^{11}
- N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
4
Sample Output 1
5
There are five such triples: (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2).
Sample Input 2
100
Sample Output 2
323
Sample Input 3
100000000000
Sample Output 3
5745290566750
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
高橋君と青木君が次のようなゲームをします。
- まず、高橋君が A 以上 B 以下の好きな整数を選び、青木君に伝える
- 次に、青木君が C 以上 D 以下の好きな整数を選ぶ
- 二人の選んだ整数の和が素数なら青木君の勝ち、そうでなければ高橋君の勝ち
二人が最適な戦略を取るとき、どちらが勝ちますか?
制約
- 1 \leq A \leq B \leq 100
- 1 \leq C \leq D \leq 100
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
A B C D
出力
二人が最適な戦略をとったとき、高橋君が勝つなら Takahashi、青木君が勝つなら Aoki を出力せよ。
入力例 1
2 3 3 4
出力例 1
Aoki
例えば高橋君が 2 を選んだときは、青木君は 3 を選ぶことで、和を素数である 5 にすることができます。
入力例 2
1 100 50 60
出力例 2
Takahashi
最適な戦略を取ると高橋君が必ず勝ちます。
入力例 3
3 14 1 5
出力例 3
Aoki
Score : 400 points
Problem Statement
Takahashi and Aoki are playing a game.
- First, Takahashi chooses an integer between A and B (inclusive) and tells it to Aoki.
- Next, Aoki chooses an integer between C and D (inclusive).
- If the sum of these two integers is a prime, then Aoki wins; otherwise, Takahashi wins.
When the two players play optimally, which player will win?
Constraints
- 1 \leq A \leq B \leq 100
- 1 \leq C \leq D \leq 100
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
A B C D
Output
If Takahashi wins when the two players play optimally, print Takahashi; if Aoki wins, print Aoki.
Sample Input 1
2 3 3 4
Sample Output 1
Aoki
For example, if Takahashi chooses 2, Aoki can choose 3 to make the sum 5, which is a prime.
Sample Input 2
1 100 50 60
Sample Output 2
Takahashi
If they play optimally, Takahashi always wins.
Sample Input 3
3 14 1 5
Sample Output 3
Aoki
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
整数列 A とノートがあります。ノートには 10^9 枚のページがあります。
Q 個のクエリが与えられます。各クエリは下記の 4 種類のいずれかです。
ADD x : 整数 x を A の末尾に追加する。
DELETE : A の末尾の要素を削除する。ただし、A が空である場合は何もしない。
SAVE y : ノートの y ページ目に書かれている数列を消し、代わりに現在の A を y ページ目に書き込む。
LOAD z : A をノートの z ページ目に書かれている数列で置き換える。
はじめ、A は空列であり、ノートのすべてのページには空列の情報が書かれています。 その初期状態から、Q 個のクエリを与えられる順に実行し、各クエリの実行直後における A の末尾の要素を出力してください。
なお、入出力の量が多くなる場合があるので、高速な方法で入出力を行うことを推奨します。
制約
- 1 \leq Q \leq 5 \times 10^5
- 1 \leq x, y, z \leq 10^9
- Q, x, y, z は整数
- 与えられるクエリは問題文中の 4 種類のいずれか
入力
入力は以下の形式で標準入力から与えられる。
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
出力
下記の形式にしたがい、i = 1, 2, \ldots, Q について、i 番目までのクエリを実行した直後の A の末尾の要素 X_i を( A が空の場合は X_i := -1 とする)出力せよ。
X_1 X_2 \ldots X_Q
入力例 1
11 ADD 3 SAVE 1 ADD 4 SAVE 2 LOAD 1 DELETE DELETE LOAD 2 SAVE 1 LOAD 3 LOAD 1
出力例 1
3 3 4 4 3 -1 -1 4 4 -1 4
はじめ、A は空列、すなわち A = () であり、ノートのすべてのページには空列の情報が書かれています。
- 1 番目のクエリによって、 A の末尾に 3 が追加され、A = (3) となります。
- 2 番目のクエリによって、ノートの 1 ページ目に書かれた数列が (3) になります。A は変わらず A = (3) です。
- 3 番目のクエリによって、 A の末尾に 4 が追加され、A = (3, 4) となります。
- 4 番目のクエリによって、ノートの 2 ページ目に書かれた数列が (3, 4) になります。A は変わらず A = (3, 4) です。
- 5 番目のクエリによって、 A がノートの 1 ページ目に書かれた数列 (3) で置き換えられ、A = (3) となります。
- 6 番目のクエリによって、 A の末尾の要素が削除され、A = () となります。
- 7 番目のクエリでは、A がすでに空であるので何もしません。A は変わらず A = () です。
- 8 番目のクエリによって、 A がノートの 2 ページ目に書かれた数列 (3, 4) で置き換えられ、A = (3, 4) となります。
- 9 番目のクエリによって、ノートの 1 ページ目に書かれた数列が (3, 4) になります。A は変わらず A = (3, 4) です。
- 10 番目のクエリによって、 A がノートの 3 ページ目に書かれた数列 () で置き換えられ、A = () となります。
- 11 番目のクエリによって、 A がノートの 1 ページ目に書かれた数列 (3, 4) で置き換えられ、A = (3, 4) となります。
入力例 2
21 ADD 4 ADD 3 DELETE ADD 10 LOAD 7 SAVE 5 SAVE 5 ADD 4 ADD 4 ADD 5 SAVE 5 ADD 2 DELETE ADD 1 SAVE 5 ADD 7 ADD 8 DELETE ADD 4 DELETE LOAD 5
出力例 2
4 3 4 10 -1 -1 -1 4 4 5 5 2 5 1 1 7 8 7 4 7 1
Score : 500 points
Problem Statement
We have an integer sequence A and a notebook. The notebook has 10^9 pages.
You are given Q queries. Each query is of one of the following four kinds:
ADD x: append an integer x to the tail of A.
DELETE: remove the last term of A if A is not empty; do nothing otherwise.
SAVE y: erase the sequence recorded on the y-th page of the notebook, and record the current A onto the y-th page.
LOAD z: replace A with the sequence recorded on the z-th page of the notebook.
Initially, A is an empty sequence, and an empty sequence is recorded on each page of the notebook. Process Q queries successively in the given order and print the last term of A after processing each query.
The use of fast input and output methods is recommended because of potentially large input and output.
Constraints
- 1 \leq Q \leq 5 \times 10^5
- 1 \leq x, y, z \leq 10^9
- Q, x, y, and z are integers.
- Each of the given queries is of one of the four kinds in the Problem Statement.
Input
The input is given from Standard Input in the following format:
Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Output
For each i = 1, 2, \ldots, Q, let X_i be the last element of A after processing up to the i-th query, or let X_i := -1 if A is empty, and print them in the following format:
X_1 X_2 \ldots X_Q
Sample Input 1
11 ADD 3 SAVE 1 ADD 4 SAVE 2 LOAD 1 DELETE DELETE LOAD 2 SAVE 1 LOAD 3 LOAD 1
Sample Output 1
3 3 4 4 3 -1 -1 4 4 -1 4
Initially, A is an empty sequence, so A = (), and an empty sequence is recorded on each page of the notebook.
- By the 1-st query, 3 is appended to the tail of A, resulting in A = (3).
- By the 2-nd query, the sequence recorded on the 1-st page of the notebook becomes (3). It remains that A = (3).
- By the 3-rd query, 4 is appended to the tail of A, resulting in A = (3, 4).
- By the 4-th query, the sequence recorded on the 2-nd page of the notebook becomes (3, 4). It remains that A = (3, 4).
- By the 5-th query, A is replaced by (3), which is recorded on the 1-st page of the notebook, resulting in A = (3).
- By the 6-th query, the last term of A is removed, resulting in A = ().
- By the 7-th query, nothing happens because A is already empty. It remains that A = ().
- By the 8-th query, A is replaced by (3,4), which is recorded on the 2-nd page of the notebook, resulting in A = (3, 4).
- By the 9-th query, the sequence recorded on the 1-st page of the notebook becomes (3, 4). It remains that A = (3, 4).
- By the 10-th query, A is replaced by (), which is recorded on the 3-rd page of the notebook, resulting in A = ().
- By the 11-th query, A is replaced by (3, 4), which is recorded on the 1-st page of the notebook, resulting in A = (3, 4).
Sample Input 2
21 ADD 4 ADD 3 DELETE ADD 10 LOAD 7 SAVE 5 SAVE 5 ADD 4 ADD 4 ADD 5 SAVE 5 ADD 2 DELETE ADD 1 SAVE 5 ADD 7 ADD 8 DELETE ADD 4 DELETE LOAD 5
Sample Output 2
4 3 4 10 -1 -1 -1 4 4 5 5 2 5 1 1 7 8 7 4 7 1
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
o と x からなる長さ N の文字列 S と、整数 M,K が与えられます。
S には少なくとも 1 つの x が含まれることが保証されます。
S を M 個連結して得られる長さ NM の文字列を T とします。
T に含まれる x のうち丁度 K 個を選んで o に変えることを考えます。
あなたの目標は、変更後の T に o のみからなるできるだけ長い連続部分文字列が含まれるようにすることです。
o のみからなる連続部分文字列の長さとして達成可能な最大値を求めてください。
制約
- N,M,K は整数
- 1 \le N \le 3 \times 10^5
- 1 \le M \le 10^9
- x を文字列 T に含まれる
xの総数としたとき、 1 \le K \le x - S は
oとxからなる長さ N の文字列 - S には少なくとも 1 つの
xが含まれる
入力
入力は以下の形式で標準入力から与えられる。
N M K S
出力
答えを整数として出力せよ。
入力例 1
10 1 2 ooxxooooox
出力例 1
9
S= ooxxooooox 、 T= ooxxooooox です。
3 文字目と 4 文字目の x を o に変更することにより、変更後の T= ooooooooox となります。
このとき o のみからなる長さ 9 の連続部分文字列が得られ、これが達成可能な最大値です。
入力例 2
5 3 4 oxxox
出力例 2
8
S= oxxox 、 T= oxxoxoxxoxoxxox です。
5,7,8,10 文字目の x を o に変更することにより、変更後の T= oxxooooooooxxox となります。
このとき o のみからなる長さ 8 の連続部分文字列が得られ、これが達成可能な最大値です。
入力例 3
30 1000000000 9982443530 oxoxooxoxoxooxoxooxxxoxxxooxox
出力例 3
19964887064
Score : 500 points
Problem Statement
You are given a string S of length N consisting of o and x, and integers M and K.
S is guaranteed to contain at least one x.
Let T be the string of length NM obtained by concatenating M copies of S.
Consider replacing exactly K x's in T with o.
Your objective is to have as long a contiguous substring consisting of o as possible in the resulting T.
Find the maximum length of a contiguous substring consisting of o that you can obtain.
Constraints
- N, M, and K are integers.
- 1 \le N \le 3 \times 10^5
- 1 \le M \le 10^9
- 1 \le K \le x, where x is the number of
x's in the string T. - S is a string of length N consisting of
oandx. - S has at least one
x.
Input
The input is given from Standard Input in the following format:
N M K S
Output
Print the answer as an integer.
Sample Input 1
10 1 2 ooxxooooox
Sample Output 1
9
S= ooxxooooox and T= ooxxooooox.
Replacing x at the third and fourth characters with o makes T= ooooooooox.
Now we have a length-9 contiguous substring consisting of o, which is the longest possible.
Sample Input 2
5 3 4 oxxox
Sample Output 2
8
S= oxxox and T= oxxoxoxxoxoxxox.
Replacing x at the 5,7,8 and 10-th characters with o makes T= oxxooooooooxxox.
Now we have a length-8 contiguous substring consisting of o, which is the longest possible.
Sample Input 3
30 1000000000 9982443530 oxoxooxoxoxooxoxooxxxoxxxooxox
Sample Output 3
19964887064