A - Chord

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

英大文字からなる長さ 3 の文字列 S が与えられます。SACEBDFCEGDFAEGBFACGBD のいずれかと等しいとき Yes を、そうでないとき No を出力してください。

制約

  • S は英大文字からなる長さ 3 の文字列

入力

入力は以下の形式で標準入力から与えられる。

S

出力

SACEBDFCEGDFAEGBFACGBD のいずれかと等しいとき Yes を、そうでないとき No を出力せよ。


入力例 1

ABC

出力例 1

No

S = ABC のとき、SACEBDFCEGDFAEGBFACGBD のいずれとも等しくないので No を出力します。


入力例 2

FAC

出力例 2

Yes

入力例 3

XYX

出力例 3

No

Score : 100 points

Problem Statement

Given a length-3 string S consisting of uppercase English letters, print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.

Constraints

  • S is a length-3 string consisting of uppercase English letters.

Input

The input is given from Standard Input in the following format:

S

Output

Print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.


Sample Input 1

ABC

Sample Output 1

No

When S = ABC, S does not equal any of ACE, BDF, CEG, DFA, EGB, FAC, and GBD, so No should be printed.


Sample Input 2

FAC

Sample Output 2

Yes

Sample Input 3

XYX

Sample Output 3

No
B - Full Moon

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

高橋くんは満月が好きです。

今日を 1 日目とすると、今日以降で満月を見られる最初の日は M 日目です。以後は P 日ごと、つまり M+P 日目、M+2P 日目、\ldots に満月を見られます。

1 日目から N 日目まで(両端を含む)の中で、高橋くんが満月を見られる日の数を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq M \leq P \leq 2\times 10^5
  • 入力される数値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N M P

出力

答えを整数として出力せよ。


入力例 1

13 3 5

出力例 1

3

満月を見られる日は、3 日目、8 日目、13 日目、18 日目、\ldots です。

1 日目から 13 日目までの中で高橋くんが満月を見られる日は、3 日目、8 日目、13 日目の 3 個です。


入力例 2

5 6 6

出力例 2

0

高橋くんが満月を見られる日が存在しない場合もあります。


入力例 3

200000 314 318

出力例 3

628

Score : 100 points

Problem Statement

Takahashi likes full moons.

Let today be day 1. The first day on or after today on which he can see a full moon is day M. After that, he can see a full moon every P days, that is, on day M+P, day M+2P, and so on.

Find the number of days between day 1 and day N, inclusive, on which he can see a full moon.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq M \leq P \leq 2\times 10^5
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M P

Output

Print the answer as an integer.


Sample Input 1

13 3 5

Sample Output 1

3

He can see a full moon on day 3, 8, 13, 18, and so on.

From day 1 to 13, he can see a full moon on three days: day 3, 8, and 13.


Sample Input 2

5 6 6

Sample Output 2

0

There may be no days he can see a full moon.


Sample Input 3

200000 314 318

Sample Output 3

628
C - LOOKUP

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

英小文字からなる文字列 S,T が与えられるので、 TS の(連続する)部分文字列かどうか判定してください。

なお、文字列 X に以下の操作を 0 回以上施して文字列 Y が得られる時、またその時に限り YX の(連続する)部分文字列であると言います。

  • 以下の 2 つの操作から 1 つを選択して、その操作を行う。
    • X の先頭の 1 文字を削除する。
    • X の末尾の 1 文字を削除する。

例えば tagvoltage の(連続する)部分文字列ですが、 aceatcoder の(連続する)部分文字列ではありません。

制約

  • S,T は英小文字からなる
  • 1 \le |S|,|T| \le 100 ( |X| は文字列 X の長さ )

入力

入力は以下の形式で標準入力から与えられる。

S
T

出力

TS の(連続する)部分文字列なら Yes 、そうでないなら No と出力せよ。


入力例 1

voltage
tag

出力例 1

Yes

tagvoltage の(連続する)部分文字列です。


入力例 2

atcoder
ace

出力例 2

No

aceatcoder の(連続する)部分文字列ではありません。


入力例 3

gorilla
gorillagorillagorilla

出力例 3

No

入力例 4

toyotasystems
toyotasystems

出力例 4

Yes

S=T である場合もあります。

Score : 200 points

Problem Statement

You are given strings S and T consisting of lowercase English letters. Determine whether T is a (contiguous) substring of S.

A string Y is said to be a (contiguous) substring of X if and only if Y can be obtained by performing the operation below on X zero or more times.

  • Do one of the following.
    • Delete the first character in X.
    • Delete the last character in X.

For instance, tag is a (contiguous) substring of voltage, while ace is not a (contiguous) substring of atcoder.

Constraints

  • S and T consist of lowercase English letters.
  • 1 \le |S|,|T| \le 100 (|X| denotes the length of a string X.)

Input

The input is given from Standard Input in the following format:

S
T

Output

If T is a (contiguous) substring of S, print Yes; otherwise, print No.


Sample Input 1

voltage
tag

Sample Output 1

Yes

tag is a (contiguous) substring of voltage.


Sample Input 2

atcoder
ace

Sample Output 2

No

ace is not a (contiguous) substring of atcoder.


Sample Input 3

gorilla
gorillagorillagorilla

Sample Output 3

No

Sample Input 4

toyotasystems
toyotasystems

Sample Output 4

Yes

It is possible that S=T.

D - Couples

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 150

問題文

2N 人の人が横一列に並んでおり、左から i 番目の人は色 A_i の服を着ています。ここで、服の色は 1 から NN 色であり、それぞれの色についてちょうど 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

条件を満たす i132 個です。

実際、色 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
E - Snake Numbers

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 350

問題文

10 以上の正整数のうち、十進数表記したときに先頭の桁(最も大きい位)の数字がそれ以外のどの桁の数字よりも真に大きくなるようなものを ヘビ数 とよびます。 例えば、31201 はヘビ数ですが、35202 はヘビ数ではありません。

L 以上 R 以下のヘビ数が何個あるか求めてください。

制約

  • 10\leq L \leq R \leq 10^{18}
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

L R

出力

答えを出力せよ。


入力例 1

97 210

出力例 1

6

97 以上 210 以下のヘビ数は、97,98,100,200,201,2106 個です。


入力例 2

1000 9999

出力例 2

2025

入力例 3

252509054433933519 760713016476190692

出力例 3

221852052834757

Score : 350 points

Problem Statement

A positive integer not less than 10 whose top digit (the most significant digit) in decimal representation is strictly larger than every other digit in that number is called a Snake number. For example, 31 and 201 are Snake numbers, but 35 and 202 are not.

Find how many Snake numbers exist between L and R, inclusive.

Constraints

  • 10 \leq L \leq R \leq 10^{18}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

L R

Output

Print the answer.


Sample Input 1

97 210

Sample Output 1

6

The Snake numbers between 97 and 210, inclusive, are 97, 98, 100, 200, 201, and 210: there are six.


Sample Input 2

1000 9999

Sample Output 2

2025

Sample Input 3

252509054433933519 760713016476190692

Sample Output 3

221852052834757
F - AtCoder Magics

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 350

問題文

高橋くんは、カードゲーム「AtCoder Magics」のカードを N 枚持っています。i 番目のカードをカード i と呼ぶことにします。各カードには強さとコストのパラメーターがあり、カード i の強さは A_i で、コストは C_i です。

高橋くんは、弱いカードは要らないので捨てることにしました。具体的には、以下の操作をできなくなるまで繰り返します。

  • 2 つのカード x, y であって、 A_x > A_y かつ C_x < C_y であるようなものを選ぶ。カード y を捨てる。

操作ができなくなったとき、捨てられなかったカードの集合は一意に定まることが証明できます。これを求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, C_i \leq 10^9
  • A_1, A_2, \dots ,A_N は全て異なる
  • C_1, C_2, \dots ,C_N は全て異なる
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 C_1
A_2 C_2
\vdots
A_N C_N

出力

捨てられなかったカードは m 枚あり、それらの番号が昇順に i_1, i_2, \dots ,i_m であったとする。このとき、以下の形式で出力せよ。

m
i_1 i_2 \cdots i_m

入力例 1

3
2 4
1 1
3 2

出力例 1

2
2 3

カード 1, 3 に注目すると、 A_1 < A_3 かつ C_1 > C_3 なのでカード 1 を捨てることができます。

それ以上操作をすることはできません。このときカード 2, 3 が残っているので、これらを出力します。


入力例 2

5
1 1
10 2
100 3
1000 4
10000 5

出力例 2

5
1 2 3 4 5

この場合、どのカードも捨てることができません。


入力例 3

6
32 101
65 78
2 29
46 55
103 130
52 40

出力例 3

4
2 3 5 6

Score : 350 points

Problem Statement

Takahashi has N cards from the card game "AtCoder Magics." The i-th card will be called card i. Each card has two parameters: strength and cost. Card i has a strength of A_i and a cost of C_i.

He does not like weak cards, so he will discard them. Specifically, he will repeat the following operation until it can no longer be performed:

  • Choose two cards x and y such that A_x > A_y and C_x < C_y. Discard card y.

It can be proved that the set of remaining cards when the operations can no longer be performed is uniquely determined. Find this set of cards.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i, C_i \leq 10^9
  • A_1, A_2, \dots ,A_N are all distinct.
  • C_1, C_2, \dots ,C_N are all distinct.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 C_1
A_2 C_2
\vdots
A_N C_N

Output

Let there be m remaining cards, cards i_1, i_2, \dots, i_m, in ascending order. Print these in the following format:

m
i_1 i_2 \cdots i_m

Sample Input 1

3
2 4
1 1
3 2

Sample Output 1

2
2 3

Focusing on cards 1 and 3, we have A_1 < A_3 and C_1 > C_3, so card 1 can be discarded.

No further operations can be performed. At this point, cards 2 and 3 remain, so print them.


Sample Input 2

5
1 1
10 2
100 3
1000 4
10000 5

Sample Output 2

5
1 2 3 4 5

In this case, no cards can be discarded.


Sample Input 3

6
32 101
65 78
2 29
46 55
103 130
52 40

Sample Output 3

4
2 3 5 6
G - Prime Sum Game

実行時間制限: 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
H - Sorting Queries

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

空の列 A があります。クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。

  • 1 x : A の最後尾に x を追加する。
  • 2 : A の最初の要素を出力する。その後、その要素を削除する。このクエリが与えられるとき、A は空でないことが保証される。
  • 3 : A を昇順にソートする。

制約

  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq x \leq 10^9
  • クエリ 2 が与えられるとき、A は空でない。
  • 入力は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

Q
\mathrm{query} 1
\mathrm{query} 2
\vdots
\mathrm{query} Q

i 番目のクエリ \mathrm{query} i では、まずクエリの種類 c_i1, 2, 3 のいずれか)が与えられる。 c_i = 1 の場合はさらに整数 x が追加で与えられる。

すなわち、各クエリは以下に示す 3 つの形式のいずれかである。

1 x
2
3

出力

c_i = 2 を満たすクエリの回数を q として q 行出力せよ。
j (1 \leq j \leq q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

8
1 4
1 3
1 2
1 1
3
2
1 0
2

出力例 1

1
2

入力例 1 において、 i 番目のクエリを処理した後の A の状態を i 行目に示すと以下のようになります。

  • (4)
  • (4, 3)
  • (4, 3, 2)
  • (4, 3, 2, 1)
  • (1, 2, 3, 4)
  • (2, 3, 4)
  • (2, 3, 4, 0)
  • (3, 4, 0)

入力例 2

9
1 5
1 5
1 3
2
3
2
1 6
3
2

出力例 2

5
3
5

入力例 2 において、 i 番目のクエリを処理した後の A の状態を i 行目に示すと以下のようになります。

  • (5)
  • (5, 5)
  • (5, 5, 3)
  • (5, 3)
  • (3, 5)
  • (5)
  • (5, 6)
  • (5, 6)
  • (6)

Score : 500 points

Problem Statement

We have an empty sequence A. You will be given Q queries, which should be processed in the order they are given. Each query is of one of the three kinds below:

  • 1 x : Append x to the end of A.
  • 2 : Print the element at the beginning of A. Then, delete that element. It is guaranteed that A will not empty when this query is given.
  • 3 : Sort A in ascending order.

Constraints

  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq x \leq 10^9
  • A will not be empty when a query 2 is given.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
\mathrm{query} 1
\mathrm{query} 2
\vdots
\mathrm{query} Q

The i-th query, \mathrm{query} i, begins with the kind of query c_i (1, 2, or 3). If c_i = 1, the line additionally has an integer x.

In other words, each query is in one of the three formats below.

1 x
2
3

Output

Print q lines, where q is the number of queries with c_i = 2.
The j-th line (1 \leq j \leq q) should contain the response for the j-th such query.


Sample Input 1

8
1 4
1 3
1 2
1 1
3
2
1 0
2

Sample Output 1

1
2

The i-th line below shows the contents of A after the i-th query is processed in Sample Input 1.

  • (4)
  • (4, 3)
  • (4, 3, 2)
  • (4, 3, 2, 1)
  • (1, 2, 3, 4)
  • (2, 3, 4)
  • (2, 3, 4, 0)
  • (3, 4, 0)

Sample Input 2

9
1 5
1 5
1 3
2
3
2
1 6
3
2

Sample Output 2

5
3
5

The i-th line below shows the contents of A after the i-th query is processed in Sample Input 2.

  • (5)
  • (5, 5)
  • (5, 5, 3)
  • (5, 3)
  • (3, 5)
  • (5)
  • (5, 6)
  • (5, 6)
  • (6)
I - Double Sum 3

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 525

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

1\le L\le R\le N を満たす整数の組 (L,R) に対し f(L,R) を以下のように定義します。

  • 何も書かれていない黒板に R-L+1 個の整数 A_L,A_{L+1},\ldots,A_R を順に書き込む。
  • 以下の操作を黒板に書かれた整数が全て消えるまで繰り返す。
    • 整数 l,r を選ぶ。ただし、 l\le r かつ黒板に l 以上 r 以下の整数が全て 1 つ以上書かれているように l,r を選ぶ必要がある。その後、黒板に書かれた l 以上 r 以下の整数を全て消す。
  • 黒板に書かれた整数が全て消えるまでに必要な操作回数の最小値を f(L,R) とする。

\displaystyle \sum_{L=1}^N\sum_{R=L}^N f(L,R) を求めてください。

制約

  • 1\le N\le 3\times 10^5
  • 1\le A_i\le N
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

4
1 3 1 4

出力例 1

16

例えば (L,R)=(1,4) の場合、以下の手順で f(L,R) を計算することができます。

  • 黒板に 1,3,1,4 が書かれている。
  • (l,r)=(1,1) を選び、黒板に書かれた 1 を全て消す。黒板には 3,4 が書かれた状態になる。
  • (l,r)=(3,4) を選び、黒板に書かれた 3,4 を全て消す。黒板は何も書かれていない状態になる。
  • 2 回未満の操作で黒板の整数を全て消すことはできないので、f(1,4)=2 である。

同様の計算で、例えば f(2,4)=2, f(1,1)=1 なども分かります。

\displaystyle \sum_{L=1}^N\sum_{R=L}^N f(L,R)=16 なので、 16 を出力してください。


入力例 2

5
3 1 4 2 4

出力例 2

23

入力例 3

10
5 1 10 9 2 5 6 9 1 6

出力例 3

129

Score : 525 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N.

For each integer pair (L,R) with 1 \le L \le R \le N, define f(L,R) as follows:

  • Start with an empty blackboard. Write the R-L+1 integers A_L, A_{L+1}, \ldots, A_R on the blackboard in order.
  • Repeat the following operation until all integers on the blackboard are erased:
    • Choose integers l, r with l \le r such that every integer from l through r appears at least once on the blackboard. Then, erase all integers from l through r that are on the blackboard.
  • Let f(L,R) be the minimum number of such operations needed to erase all the integers from the blackboard.

Find \displaystyle \sum_{L=1}^N \sum_{R=L}^N f(L,R).

Constraints

  • 1 \le N \le 3 \times 10^5
  • 1 \le A_i \le N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

4
1 3 1 4

Sample Output 1

16

For example, in the case of (L,R)=(1,4):

  • The blackboard has 1,3,1,4.
  • Choose (l,r)=(1,1) and erase all occurrences of 1. The blackboard now has 3,4.
  • Choose (l,r)=(3,4) and erase all occurrences of 3 and 4. The blackboard becomes empty.
  • It cannot be done in fewer than two operations, so f(1,4) = 2.

Similarly, you can find f(2,4)=2, f(1,1)=1, etc.

\displaystyle \sum_{L=1}^N \sum_{R=L}^N f(L,R) = 16, so print 16.


Sample Input 2

5
3 1 4 2 4

Sample Output 2

23

Sample Input 3

10
5 1 10 9 2 5 6 9 1 6

Sample Output 3

129