A - Attack

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

体力が A の敵がいます。あなたは、1 回攻撃をすることで敵の体力を B 減らすことが出来ます。

敵の体力を 0 以下にするためには、最小で何回攻撃をする必要があるでしょうか?

制約

  • 1 \le A,B \le 10^{18}
  • A, B は整数である。

入力

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

A B

出力

答えを出力せよ。


入力例 1

7 3

出力例 1

3

3 回攻撃すると敵の体力が -2 となります。

2 回攻撃しただけでは敵の体力は 1 であるため、3 回攻撃する必要があります。


入力例 2

123456789123456789 987654321

出力例 2

124999999

入力例 3

999999999999999998 2

出力例 3

499999999999999999

Score : 100 points

Problem Statement

There is an enemy with stamina A. Every time you attack the enemy, its stamina reduces by B.

At least how many times do you need to attack the enemy to make its stamina 0 or less?

Constraints

  • 1 \le A,B \le 10^{18}
  • A and B are integers.

Input

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

A B

Output

Print the answer.


Sample Input 1

7 3

Sample Output 1

3

Attacking three times make the enemy's stamina -2.

Attacking only twice makes the stamina 1, so you need to attack it three times.


Sample Input 2

123456789123456789 987654321

Sample Output 2

124999999

Sample Input 3

999999999999999998 2

Sample Output 3

499999999999999999
B - Weather Forecast

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

明日からの 7 日間の天気予報を表す文字列 S が与えられます。
i 日後の予報は S の i 文字目が o であるとき晴れ、x であるとき雨です。

N 日後の天気予報が晴れかどうかを教えてください。

制約

  • N は 1 以上 7 以下の整数
  • S は長さ 7 の文字列であり、o と x のみからなる

入力

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

N
S

出力

N 日後の天気予報が晴れであるとき Yes を、雨であるとき No を出力せよ。


入力例 1

4
oooxoox

出力例 1

No

明日からの 7 日間の天気予報は順に、晴れ、晴れ、晴れ、雨、晴れ、晴れ、雨です。
特に、今日から 4 日後の天気予報は雨です。


入力例 2

7
ooooooo

出力例 2

Yes

Score : 100 points

Problem Statement

You are given a string S, which represents a weather forecast for the seven days starting tomorrow.
The i-th of those seven days is forecast to be sunny if the i-th character of S is o, and rainy if that character is x.

Tell us whether the N-th day is forecast to be sunny.

Constraints

  • N is an integer between 1 and 7 (inclusive).
  • S is a string of length 7 consisting of o and x.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print Yes if the N-th of the seven days starting tomorrow is forecast to be sunny, and No if that day is forecast to be rainy.


Sample Input 1

4
oooxoox

Sample Output 1

No

The forecast for each of the seven days starting tomorrow is as follows: sunny, sunny, sunny, rainy, sunny, sunny, rainy.
In particular, the fourth day is forecast to be rainy.


Sample Input 2

7
ooooooo

Sample Output 2

Yes
C - Hard Calculation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

正整数 A,B が与えられます。
A+B を(十進法で)計算する時、繰り上がりが生じないなら Easy 、生じるなら Hard と出力してください。

制約

  • A,B は整数
  • 1 \le A,B \le 10^{18}

入力

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

A B

出力

繰り上がりが生じないなら Easy 、生じるなら Hard と出力せよ。


入力例 1

229 390

出力例 1

Hard

229+390 を計算する際、十の位から百の位へと繰り上がりが発生します。よって、答えは Hard です。


入力例 2

123456789 9876543210

出力例 2

Easy

繰り上がりは発生しません。答えは Easy です。
また、入力が 32bit 整数に収まらないこともあります。

Score : 200 points

Problem Statement

You are given positive integers A and B.
Let us calculate A+B (in decimal). If it does not involve a carry, print Easy; if it does, print Hard.

Constraints

  • A and B are integers.
  • 1 \le A,B \le 10^{18}

Input

Input is given from Standard Input in the following format:

A B

Output

If the calculation does not involve a carry, print Easy; if it does, print Hard.


Sample Input 1

229 390

Sample Output 1

Hard

When calculating 229+390, we have a carry from the tens digit to the hundreds digit, so the answer is Hard.


Sample Input 2

123456789 9876543210

Sample Output 2

Easy

We do not have a carry here; the answer is Easy.
Note that the input may not fit into a 32-bit integer.

D - 1D Keyboard

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

26 個のキーが数直線上に並んだキーボードがあります。

このキーボードの配列は ABCDEFGHIJKLMNOPQRSTUVWXYZ を並べ替えた文字列 S で表されます。 文字 S_x に対応するキーが座標 x (1 \leq x \leq 26) にあります。 ここで、S_x は S の x 文字目を表します。

あなたはこのキーボードを使って ABCDEFGHIJKLMNOPQRSTUVWXYZ をこの順で右手人差し指で一度だけ入力します。 ある文字を入力するためには、その文字に対応するキーの座標に指を移動させてキーを押す必要があります。

はじめ、指は A に対応するキーの座標にあります。A に対応するキーを押してから、Z に対応するキーを押すまでの指の移動距離の合計として考えられる最小値を求めてください。ただし、 キーを押す動作は移動距離に含まれません。

制約

  • S は ABCDEFGHIJKLMNOPQRSTUVWXYZ を並べ替えた文字列である。

入力

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

S

出力

答えを出力せよ。


入力例 1

ABCDEFGHIJKLMNOPQRSTUVWXYZ

出力例 1

25

A のキーを押してから Z のキーを押すまで指を 1 ずつ正の方向に移動させる必要があり、このときの移動距離の合計は 25 です。25 未満の総移動距離でキーを全て入力することはできないため、25 と出力します。


入力例 2

MGJYIZDKSBHPVENFLQURTCWOAX

出力例 2

223

Score : 200 points

Problem Statement

There is a keyboard with 26 keys arranged on a number line.

The arrangement of this keyboard is represented by a string S, which is a permutation of ABCDEFGHIJKLMNOPQRSTUVWXYZ. The key corresponding to the character S_x is located at coordinate x (1 \leq x \leq 26). Here, S_x denotes the x-th character of S.

You will use this keyboard to input ABCDEFGHIJKLMNOPQRSTUVWXYZ in this order, typing each letter exactly once with your right index finger. To input a character, you need to move your finger to the coordinate of the key corresponding to that character and press the key.

Initially, your finger is at the coordinate of the key corresponding to A. Find the minimal possible total traveled distance of your finger from pressing the key for A to pressing the key for Z. Here, pressing a key does not contribute to the distance.

Constraints

  • S is a permutation of ABCDEFGHIJKLMNOPQRSTUVWXYZ.

Input

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

S

Output

Print the answer.


Sample Input 1

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Sample Output 1

25

From pressing the key for A to pressing the key for Z, you need to move your finger 1 unit at a time in the positive direction, resulting in a total traveled distance of 25. It is impossible to press all keys with a total traveled distance less than 25, so print 25.


Sample Input 2

MGJYIZDKSBHPVENFLQURTCWOAX

Sample Output 2

223
E - Monotonically Increasing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

長さ N かつ全ての要素が 1 以上 M 以下である整数列のうち、狭義単調増加であるものを全て辞書順に出力してください。

注記

ある 2 個の異なる長さの等しい整数列 A_1,A_2,\dots,A_N と B_1,B_2,\dots,B_N が以下を満たすとき、またその時に限り辞書順で A が B より早いと定義されます。

  • ある整数 i(1 \le i \le N) が存在し、1 \le j < i である全ての整数 j に対し A_j=B_j が成り立ち、かつ A_i < B_i が成り立つ。

ある整数列 A_1,A_2,\dots,A_N は以下を満たすとき、またその時に限り狭義単調増加です。

  • 全ての整数 i(1 \le i \le N-1) に対し A_i < A_{i+1} が成り立つ。

制約

  • 1 \le N \le M \le 10
  • 入力は全て整数。

入力

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

N M

出力

条件を満たす整数列を一行に一つずつ、辞書順に出力せよ(出力例を参考にせよ)。


入力例 1

2 3

出力例 1

1 2 
1 3 
2 3 

条件を満たす数列は (1,2),(1,3),(2,3) の 3 個です。これらを辞書順で早い方から出力します。


入力例 2

3 5

出力例 2

1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5 

Score : 300 points

Problem Statement

Print all strictly increasing integer sequences of length N where all elements are between 1 and M (inclusive), in lexicographically ascending order.

Notes

For two integer sequences of the same length A_1,A_2,\dots,A_N and B_1,B_2,\dots,B_N, A is said to be lexicographically earlier than B if and only if:

  • there is an integer i (1 \le i \le N) such that A_j=B_j for all integers j satisfying 1 \le j < i, and A_i < B_i.

An integer sequence A_1,A_2,\dots,A_N is said to be strictly increasing if and only if:

  • A_i < A_{i+1} for all integers i (1 \le i \le N-1).

Constraints

  • 1 \le N \le M \le 10
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the sought sequences in lexicographically ascending order, each in its own line (see Sample Outputs).


Sample Input 1

2 3

Sample Output 1

1 2 
1 3 
2 3 

The sought sequences are (1,2),(1,3),(2,3), which should be printed in lexicographically ascending order.


Sample Input 2

3 5

Sample Output 2

1 2 3 
1 2 4 
1 2 5 
1 3 4 
1 3 5 
1 4 5 
2 3 4 
2 3 5 
2 4 5 
3 4 5 
F - Insert and Erase A

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

英大文字からなる文字列 S,T が与えられます。

あなたは以下の 2 種類の操作を好きな順序で好きな回数(0 回でも良い)行うことができます。

  • S の好きな位置(先頭および末尾を含む)に文字 A を 1 つ挿入する。
  • S に含まれる文字 A を 1 つ選んで削除する。なお、残った文字は元の順序を保ったまま連結される。

S を T に一致させることが可能かどうか判定し、可能な場合は必要な操作回数の合計の最小値を求めてください。

制約

  • S,T は英大文字からなる長さ 1 以上 3\times 10^5 以下の文字列

入力

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

S
T

出力

S を T に一致させることが可能ならば必要な操作回数の合計の最小値を、不可能ならば -1 を出力せよ。


入力例 1

ABC
BACA

出力例 1

3

以下のように、合計 3 回の操作で S を T に一致させることが可能です。

  • S の 2 文字目と 3 文字目の間に A を 1 つ挿入する。S= ABAC となる。
  • S の 1 文字目にある A を削除する。S= BAC となる。
  • S の末尾に A を 1 つ挿入する。S= BACA となる。

合計 2 回以下の操作で S を T に一致させることはできないため、答えは 3 です。


入力例 2

ABC
ARC

出力例 2

-1

どのように操作を行っても、S を T に一致させることはできません。


入力例 3

ABC
ABC

出力例 3

0

1 回も操作を行う必要がありません。


入力例 4

AAAWAZAAAABAAAU
AWAAZABAAAAAUA

出力例 4

9

Score : 300 points

Problem Statement

You are given strings S and T consisting of uppercase English letters.

You may perform the following two types of operations any number of times (possibly zero) in any order:

  • Insert one character A at any position in S (possibly the beginning or the end).
  • Choose one character A in S and delete it. The remaining characters are concatenated in their original order.

Determine whether it is possible to make S equal to T, and if so, find the minimum total number of operations required.

Constraints

  • S and T are strings of uppercase English letters with length between 1 and 3\times 10^5, inclusive.

Input

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

S
T

Output

If it is possible to make S equal to T, output the minimum total number of operations required; otherwise, output -1.


Sample Input 1

ABC
BACA

Sample Output 1

3

It is possible to make S equal to T in three operations in total, as follows:

  • Insert one A between the second and third characters of S. Now, S= ABAC.
  • Delete the first character of S, which is A. Now, S= BAC.
  • Insert one A at the end of S. Now, S= BACA.

It is impossible to make S equal to T in two or fewer operations, so the answer is 3.


Sample Input 2

ABC
ARC

Sample Output 2

-1

No matter how operations are performed, it is impossible to make S equal to T.


Sample Input 3

ABC
ABC

Sample Output 3

0

No operations need to be performed.


Sample Input 4

AAAWAZAAAABAAAU
AWAAZABAAAAAUA

Sample Output 4

9
G - I Hate Non-integer Number

Time Limit: 2.5 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

項数が N の正整数列 A=(a_1,\ldots,a_N) が与えられます。
A の項を 1 個以上選ぶ方法は 2^N-1 通りありますが、そのうち選んだ項の平均値が整数であるものが何通りかを 998244353 で割った余りを求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq a_i \leq 10^9
  • 入力はすべて整数

入力

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

N
a_1 \ldots a_N

出力

答えを出力せよ。


入力例 1

3
2 6 2

出力例 1

6

A の項を選ぶ方法それぞれに対する平均値は以下のようになります。

  • a_1 のみを選んだ場合、平均値は \frac{a_1}{1}=\frac{2}{1} = 2 であり、整数である。

  • a_2 のみを選んだ場合、平均値は \frac{a_2}{1}=\frac{6}{1} = 6 であり、整数である。

  • a_3 のみを選んだ場合、平均値は \frac{a_3}{1}=\frac{2}{1} = 2 であり、整数である。

  • a_1 と a_2 を選んだ場合、平均値は \frac{a_1+a_2}{2}=\frac{2+6}{2} = 4 であり、整数である。

  • a_1 と a_3 を選んだ場合、平均値は \frac{a_1+a_3}{2}=\frac{2+2}{2} = 2 であり、整数である。

  • a_2 と a_3 を選んだ場合、平均値は \frac{a_2+a_3}{2}=\frac{6+2}{2} = 4 であり、整数である。

  • a_1 と a_2 と a_3 を選んだ場合、平均値は \frac{a_1+a_2+a_3}{3}=\frac{2+6+2}{3} = \frac{10}{3} であり、整数ではない。

以上より、6 通りの選び方が条件を満たします。


入力例 2

5
5 5 5 5 5

出力例 2

31

どのように A の項を 1 個以上選んでも平均値が 5 になります。

Score : 400 points

Problem Statement

You are given a sequence of positive integers A=(a_1,\ldots,a_N) of length N.
There are (2^N-1) ways to choose one or more terms of A. How many of them have an integer-valued average? Find the count modulo 998244353.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq a_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 \ldots a_N

Output

Print the answer.


Sample Input 1

3
2 6 2

Sample Output 1

6

For each way to choose terms of A, the average is obtained as follows:

  • If just a_1 is chosen, the average is \frac{a_1}{1}=\frac{2}{1} = 2, which is an integer.

  • If just a_2 is chosen, the average is \frac{a_2}{1}=\frac{6}{1} = 6, which is an integer.

  • If just a_3 is chosen, the average is \frac{a_3}{1}=\frac{2}{1} = 2, which is an integer.

  • If a_1 and a_2 are chosen, the average is \frac{a_1+a_2}{2}=\frac{2+6}{2} = 4, which is an integer.

  • If a_1 and a_3 are chosen, the average is \frac{a_1+a_3}{2}=\frac{2+2}{2} = 2, which is an integer.

  • If a_2 and a_3 are chosen, the average is \frac{a_2+a_3}{2}=\frac{6+2}{2} = 4, which is an integer.

  • If a_1, a_2, and a_3 are chosen, the average is \frac{a_1+a_2+a_3}{3}=\frac{2+6+2}{3} = \frac{10}{3}, which is not an integer.

Therefore, 6 ways satisfy the condition.


Sample Input 2

5
5 5 5 5 5

Sample Output 2

31

Regardless of the choice of one or more terms of A, the average equals 5.

H - A > B substring

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

A, B, C の 3 種類の文字からなる長さ N の文字列 S が与えられます。

S の空でない連続する部分文字列は \dfrac{N(N+1)}2 個ありますが、そのうち A が B よりも多く含まれるものはいくつあるか求めてください。

2 つの部分文字列は、S から取り出す場所が異なれば文字列として等しくても区別して数えることに注意してください。

部分文字列とは

S の部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、AB は ABC の部分文字列ですが、AC は ABC の部分文字列ではありません。

制約

  • 1\le N\le5\times10 ^ 5
  • S は A, B, C からなる長さ N の文字列
  • N は整数

入力

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

N
S

出力

S の連続する部分文字列のうち A が B よりも多く含まれるものの個数を出力せよ。


入力例 1

10
ACBBCABCAB

出力例 1

8

以下の 8 つの部分文字列が条件を満たします。

  • A:S の 1 文字目から 1 文字目
  • AC:S の 1 文字目から 2 文字目
  • CA:S の 5 文字目から 6 文字目
  • CABCA:S の 5 文字目から 9 文字目
  • A:S の 6 文字目から 6 文字目
  • ABCA:S の 6 文字目から 9 文字目
  • CA:S の 8 文字目から 9 文字目
  • A:S の 9 文字目から 9 文字目

これら以外の部分文字列は条件を満たさないため、8 を出力してください。

A や CA は複数箇所から取り出すことができますが、取り出す場所が異なれば区別して数えることに注意してください。


入力例 2

4
CCBC

出力例 2

0

条件を満たす部分文字列が存在しないこともあります。


入力例 3

36
CABACBBBBBAABABACCBCABCCABAABABBCBAC

出力例 3

136

Score : 450 points

Problem Statement

You are given a string S of length N consisting of three kinds of characters: A, B, and C.

There are \dfrac{N(N+1)}2 non-empty contiguous substrings of S. Find how many of them contain more As than Bs.

Note that two substrings are counted separately if they are taken from different positions in S, even if they are equal as strings.

What is a substring?

A substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, AB is a substring of ABC, but AC is not a substring of ABC.

Constraints

  • 1\le N\le5\times10 ^ 5
  • S is a string of length N consisting of A, B, and C.
  • N is an integer.

Input

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

N
S

Output

Print the number of contiguous substrings of S that contain more As than Bs.


Sample Input 1

10
ACBBCABCAB

Sample Output 1

8

The following eight substrings satisfy the condition:

  • A: 1st to 1st character of S
  • AC: 1st to 2nd character of S
  • CA: 5th to 6th character of S
  • CABCA: 5th to 9th character of S
  • A: 6th to 6th character of S
  • ABCA: 6th to 9th character of S
  • CA: 8th to 9th character of S
  • A: 9th to 9th character of S

All other substrings do not satisfy the condition, so print 8.

Note that A and CA can be taken from multiple positions, but they are counted separately if taken from different positions.


Sample Input 2

4
CCBC

Sample Output 2

0

There may be no substrings that satisfy the condition.


Sample Input 3

36
CABACBBBBBAABABACCBCABCCABAABABBCBAC

Sample Output 3

136
I - Buildings 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550 点

問題文

ビル 1, ビル 2, \ldots, ビル N の N 棟がこの順で東西に一列に並んでおり、ビル 1 が最も西に、ビル N が最も東に建っています。ビル i\ (1\leq i\leq N) の高さは H_i です。

整数の組 (i,j)\ (1\leq i\lt j\leq N) に対して、以下の条件を満たすとき ビル i からビル j を見ることができます。

  • ビル i とビル j の間にビル j より高いビルが存在しない。すなわち、H_k\gt H_j を満たす整数 k\ (i\lt k\lt j) が存在しない。

Q 個の質問に答えてください。i 番目の質問では整数の組 (l_i,r_i)\ (l_i\lt r_i) が与えられるので、ビル r_i より東にあるビル(ビル r_i+1, ビル r_i+2,\ldots,ビル N )のうちビル l_i とビル r_i の両方から見ることができるものの個数を答えてください。

制約

  • 2\leq N\leq 2\times 10^5
  • 1\leq Q\leq 2\times 10^5
  • 1\leq H_i\leq N
  • H_i\neq H_j\ (i\neq j)
  • 1\leq l_i\lt r_i\leq N
  • 入力は全て整数

入力

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

N Q
H_1 H_2 \ldots H_N
l_1 r_1
l_2 r_2
\vdots
l_Q r_Q

出力

Q 行出力せよ。i\ (1\leq i\leq Q) 行目には i 番目の質問に対する答えを出力せよ。


入力例 1

5 3
2 1 4 3 5
1 2
3 5
1 4

出力例 1

2
0
1
  • 1 つ目の質問について、ビル 2 より東にあるビルのうち ビル 1 とビル 2 の両方から見ることができるものはビル 3,5 の 2 つです。
  • 2 つ目の質問について、ビル 5 より東にあるビルは存在しません。
  • 3 つ目の質問について、ビル 4 より東にあるビルのうち、ビル 1,4 の両方から見ることができるビルはビル 5 の 1 つです。

入力例 2

10 10
2 1 5 3 4 6 9 8 7 10
3 9
2 5
4 8
5 6
3 8
2 10
7 8
6 7
8 10
4 10

出力例 2

1
3
1
2
1
0
1
1
0
0

Score : 550 points

Problem Statement

There are N buildings, building 1, building 2, \ldots, building N, arranged in this order in a straight line from west to east. Building 1 is the westernmost, and building N is the easternmost. The height of building i\ (1\leq i\leq N) is H_i.

For a pair of integers (i,j)\ (1\leq i\lt j\leq N), building j can be seen from building i if the following condition is satisfied.

  • There is no building taller than building j between buildings i and j. In other words, there is no integer k\ (i\lt k\lt j) such that H_k > H_j.

You are given Q queries. In the i-th query, given a pair of integers (l_i,r_i)\ (l_i\lt r_i), find the number of buildings to the east of building r_i (that is, buildings r_i + 1, r_i + 2, \ldots, N) that can be seen from both buildings l_i and r_i.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq H_i \leq N
  • H_i\neq H_j\ (i\neq j)
  • 1 \leq l_i < r_i \leq N
  • All input values are integers.

Input

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

N Q
H_1 H_2 \ldots H_N
l_1 r_1
l_2 r_2
\vdots
l_Q r_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer to the i-th query.


Sample Input 1

5 3
2 1 4 3 5
1 2
3 5
1 4

Sample Output 1

2
0
1
  • For the first query, among the buildings to the east of building 2, buildings 3 and 5 can be seen from both buildings 1 and 2, so the answer is 2.
  • For the second query, there are no buildings to the east of building 5.
  • For the third query, among the buildings to the east of building 4, building 5 can be seen from both buildings 1 and 4, so the answer is 1.

Sample Input 2

10 10
2 1 5 3 4 6 9 8 7 10
3 9
2 5
4 8
5 6
3 8
2 10
7 8
6 7
8 10
4 10

Sample Output 2

1
3
1
2
1
0
1
1
0
0