A - Station and Bus

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

AtCoder 市には 3 つの駅があり、1, 2, 3 の番号がつけられています。

これらの駅は、それぞれ鉄道会社A, Bのいずれかが管理しています。管理状況は長さ 3 の文字列 S で表され、駅 iS_iA のとき鉄道会社 A が、B のとき鉄道会社 B が管理しています。

鉄道会社 A が管理している駅と、鉄道会社 B が管理している駅の間には、交通の便のためにバスを運行することになりました。

実際にバスが運行することになる駅の組み合わせが存在するかどうかを判定してください。

制約

  • SA または B から成る
  • |S| = 3

入力

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

S

出力

バスが運行することになる駅の組み合わせが存在する場合は Yes を、存在しない場合は No を出力せよ。


入力例 1

ABA

出力例 1

Yes

1, 3 は鉄道会社 A が、駅 2 は鉄道会社 B が管理しています。

1, 2 間と駅 2, 3 間でバスが運行するので、Yes を出力してください。


入力例 2

BBA

出力例 2

Yes

1, 2 は鉄道会社 B が、駅 3 は鉄道会社 A が管理しています。

1, 3 間と駅 2, 3 間でバスが運行するので、Yes を出力してください。


入力例 3

BBB

出力例 3

No

駅は全て鉄道会社 B が管理しているので、バスは運行しません。よって No を出力してください。

Score : 100 points

Problem Statement

In AtCoder City, there are three stations numbered 1, 2, and 3.

Each of these stations is operated by one of the two railway companies, A and B. A string S of length 3 represents which company operates each station. If S_i is A, Company A operates Station i; if S_i is B, Company B operates Station i.

To improve the transportation condition, for each pair of a station operated by Company A and one operated by Company B, there will be a bus service connecting them.

Determine if there is a pair of stations that will be connected by a bus service.

Constraints

  • Each character of S is A or B.
  • |S| = 3

Input

Input is given from Standard Input in the following format:

S

Output

If there is a pair of stations that will be connected by a bus service, print Yes; otherwise, print No.


Sample Input 1

ABA

Sample Output 1

Yes

Company A operates Station 1 and 3, while Company B operates Station 2.

There will be a bus service between Station 1 and 2, and between Station 2 and 3, so print Yes.


Sample Input 2

BBA

Sample Output 2

Yes

Company B operates Station 1 and 2, while Company A operates Station 3.

There will be a bus service between Station 1 and 3, and between Station 2 and 3, so print Yes.


Sample Input 3

BBB

Sample Output 3

No

Company B operates all the stations. Thus, there will be no bus service, so print No.

B - Count Balls

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋君は青と赤の 2 色のボールを持っており、これらを一列に並べようとしています。

初め、列にボールはありません。

根気のある高橋君は、次の操作を 10^{100} 回繰り返します。

  • 列の末尾に、A 個の青いボールを加える。その後、列の末尾に B 個の赤いボールを加える。

こうしてできる列の先頭から N 個のボールのうち、青いボールの個数はいくつでしょうか。

制約

  • 1 \leq N \leq 10^{18}
  • A, B \geq 0
  • 0 < A + B \leq 10^{18}
  • 入力は全て整数である

入力

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

N A B

出力

列の先頭から N 個のボールのうち、青いボールの個数を出力せよ。


入力例 1

8 3 4

出力例 1

4

青いボールをb、赤いボールを rで表すと、列の先頭から 8 個のボールは bbbrrrrb であるので、このうち青いボールは 4 個です。


入力例 2

8 0 4

出力例 2

0

そもそも赤いボールしか並んでいません。


入力例 3

6 2 4

出力例 3

2

bbrrrr のうち青いボールは 2 個です。

Score : 200 points

Problem Statement

Takahashi has many red balls and blue balls. Now, he will place them in a row.

Initially, there is no ball placed.

Takahashi, who is very patient, will do the following operation 10^{100} times:

  • Place A blue balls at the end of the row of balls already placed. Then, place B red balls at the end of the row.

How many blue balls will be there among the first N balls in the row of balls made this way?

Constraints

  • 1 \leq N \leq 10^{18}
  • A, B \geq 0
  • 0 < A + B \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of blue balls that will be there among the first N balls in the row of balls.


Sample Input 1

8 3 4

Sample Output 1

4

Let b denote a blue ball, and r denote a red ball. The first eight balls in the row will be bbbrrrrb, among which there are four blue balls.


Sample Input 2

8 0 4

Sample Output 2

0

He placed only red balls from the beginning.


Sample Input 3

6 2 4

Sample Output 3

2

Among bbrrrr, there are two blue balls.

C - Tax Increase

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

消費税率が 8 %のとき A 円、10 %のとき B 円の消費税が課されるような商品の税抜き価格を求めてください。

ただし、税抜き価格は正の整数でなければならないものとし、消費税の計算において小数点以下は切り捨てて計算するものとします。

条件を満たす税抜き価格が複数存在する場合は最も小さい金額を出力してください。また、条件を満たす税抜き価格が存在しない場合は -1 と出力してください。

制約

  • 1 \leq A \leq B \leq 100
  • A, B は整数である

入力

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

A B

出力

条件を満たす税抜き価格が存在する場合は最小の金額を表す整数を、存在しない場合は -1 を出力せよ。


入力例 1

2 2

出力例 1

25

税抜き価格が 25 円の場合、

  • 消費税率が 8 %のとき消費税は \lfloor 25 \times 0.08 \rfloor = \lfloor 2 \rfloor = 2 円です。

  • 消費税率が 10 %のとき消費税は \lfloor 25 \times 0.1 \rfloor = \lfloor 2.5 \rfloor = 2 円です。

よって 25 円は条件を満たし、また 26 円のときなども条件を満たしますが、これが最小であるので 25 を出力してください。


入力例 2

8 10

出力例 2

100

税抜き価格が 100 円の場合、

  • 消費税率が 8 %のとき消費税は \lfloor 100 \times 0.08 \rfloor = 8 円です。

  • 消費税率が 10 %のとき消費税は \lfloor 100 \times 0.1 \rfloor = 10 円です。


入力例 3

19 99

出力例 3

-1

条件を満たす税抜き価格は存在しないので、-1 を出力してください。

Score : 300 points

Problem Statement

Find the price of a product before tax such that, when the consumption tax rate is 8 percent and 10 percent, the amount of consumption tax levied on it is A yen and B yen, respectively. (Yen is the currency of Japan.)

Here, the price before tax must be a positive integer, and the amount of consumption tax is rounded down to the nearest integer.

If multiple prices satisfy the condition, print the lowest such price; if no price satisfies the condition, print -1.

Constraints

  • 1 \leq A \leq B \leq 100
  • A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If there is a price that satisfies the condition, print an integer representing the lowest such price; otherwise, print -1.


Sample Input 1

2 2

Sample Output 1

25

If the price of a product before tax is 25 yen, the amount of consumption tax levied on it is:

  • When the consumption tax rate is 8 percent: \lfloor 25 \times 0.08 \rfloor = \lfloor 2 \rfloor = 2 yen.
  • When the consumption tax rate is 10 percent: \lfloor 25 \times 0.1 \rfloor = \lfloor 2.5 \rfloor = 2 yen.

Thus, the price of 25 yen satisfies the condition. There are other possible prices, such as 26 yen, but print the minimum such price, 25.


Sample Input 2

8 10

Sample Output 2

100

If the price of a product before tax is 100 yen, the amount of consumption tax levied on it is:

  • When the consumption tax rate is 8 percent: \lfloor 100 \times 0.08 \rfloor = 8 yen.
  • When the consumption tax rate is 10 percent: \lfloor 100 \times 0.1 \rfloor = 10 yen.

Sample Input 3

19 99

Sample Output 3

-1

There is no price before tax satisfying this condition, so print -1.

D - String Formation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

高橋君は、英小文字から成る文字列 S を持っています。

この S から始めて、ある与えられた手順に従って文字列を作ることにしました。

手順は Q 回の操作から成ります。操作 i(1 \leq i \leq Q) では、まず整数 T_i が与えられます。

  • T_i = 1 のとき : 文字列 S の前後を反転する。

  • T_i = 2 のとき : 追加で整数 F_i と英小文字 C_i が与えられる。

    • F_i = 1 のとき : 文字列 S の先頭に C_i を追加する。
    • F_i = 2 のとき : 文字列 S の末尾に C_i を追加する。

高橋君のために、手順の後に最終的にできる文字列を求めてあげてください。

制約

  • 1 \leq |S| \leq 10^5
  • S は英小文字から成る
  • 1 \leq Q \leq 2 \times 10^5
  • T_i = 1 または 2
  • F_i = 1 または 2
  • C_i は英小文字である

入力

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

S
Q
Query_1
:
Query_Q

3 行目から Q + 2 行目の Query_i は、以下の 2 つのいずれかである。

1

T_i = 1 として操作を行うことを表す。

2 F_i C_i

T_i = 2 として操作を行うことを表す。

出力

手順の後に最終的にできる文字列を出力せよ。


入力例 1

a
4
2 1 p
1
2 2 c
1

出力例 1

cpa

Q = 4 回の操作を行います。初め Sa です。

  • 操作 1 : S の先頭に p を追加する。Spa となる。

  • 操作 2 : S の前後を反転する。Sap となる。

  • 操作 3 : S の末尾に c を追加する。Sapc となる。

  • 操作 4 : S の前後を反転する。Scpa となる。

よって最終的にできる文字列は cpa となります。


入力例 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

出力例 2

aabc

Q = 6 回の操作を行います。初め Sa です。

  • 操作 1 : Saa となる。

  • 操作 2 : Sbaa となる。

  • 操作 3 : Saab となる。

  • 操作 4 : Saabc となる。

  • 操作 5 : Scbaa となる。

  • 操作 6 : Saabc となる。

よって最終的にできる文字列は aabc となります。


入力例 3

y
1
2 1 x

出力例 3

xy

Score : 400 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

Starting with this string, he will produce a new one in the procedure given as follows.

The procedure consists of Q operations. In Operation i (1 \leq i \leq Q), an integer T_i is provided, which means the following:

  • If T_i = 1: reverse the string S.

  • If T_i = 2: An integer F_i and a lowercase English letter C_i are additionally provided.

    • If F_i = 1 : Add C_i to the beginning of the string S.
    • If F_i = 2 : Add C_i to the end of the string S.

Help Takahashi by finding the final string that results from the procedure.

Constraints

  • 1 \leq |S| \leq 10^5
  • S consists of lowercase English letters.
  • 1 \leq Q \leq 2 \times 10^5
  • T_i = 1 or 2.
  • F_i = 1 or 2, if provided.
  • C_i is a lowercase English letter, if provided.

Input

Input is given from Standard Input in the following format:

S
Q
Query_1
:
Query_Q

In the 3-rd through the (Q+2)-th lines, Query_i is one of the following:

1

which means T_i = 1, and:

2 F_i C_i

which means T_i = 2.

Output

Print the resulting string.


Sample Input 1

a
4
2 1 p
1
2 2 c
1

Sample Output 1

cpa

There will be Q = 4 operations. Initially, S is a.

  • Operation 1: Add p at the beginning of S. S becomes pa.

  • Operation 2: Reverse S. S becomes ap.

  • Operation 3: Add c at the end of S. S becomes apc.

  • Operation 4: Reverse S. S becomes cpa.

Thus, the resulting string is cpa.


Sample Input 2

a
6
2 2 a
2 1 b
1
2 2 c
1
1

Sample Output 2

aabc

There will be Q = 6 operations. Initially, S is a.

  • Operation 1: S becomes aa.

  • Operation 2: S becomes baa.

  • Operation 3: S becomes aab.

  • Operation 4: S becomes aabc.

  • Operation 5: S becomes cbaa.

  • Operation 6: S becomes aabc.

Thus, the resulting string is aabc.


Sample Input 3

y
1
2 1 x

Sample Output 3

xy
E - Divisible Substring

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

高橋君は 0 から 9 までの数字から成る長さ N の文字列 S を持っています。

素数 P が好きな高橋君は、S の空でない連続する部分文字列 N \times (N + 1) / 2 個のうち、十進表記の整数と見なした際に P で割り切れるものの個数を知りたくなりました。

ただし部分文字列は先頭が 0 であっても良いものとし、文字列として等しい場合や、整数と見なした際に等しい場合も、部分文字列の S 内の位置で区別します。

高橋君のためにこの個数を計算してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • S は数字から成る
  • |S| = N
  • 2 \leq P \leq 10000
  • P は素数である

入力

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

N P
S

出力

S の空でない連続する部分文字列であって、十進表記の整数と見なした際に P で割り切れるものの個数を出力せよ。


入力例 1

4 3
3543

出力例 1

6

S = 3543 です。S の空でない連続する部分文字列は次の 10 個があります。

  • 33 で割り切れる。

  • 353 で割り切れない。

  • 3543 で割り切れる。

  • 35433 で割り切れる。

  • 53 で割り切れない。

  • 543 で割り切れる。

  • 5433 で割り切れる。

  • 43 で割り切れない。

  • 433 で割り切れない。

  • 33 で割り切れる。

このうち 3 で割り切れるものは 6 個であるので、6 を出力してください。


入力例 2

4 2
2020

出力例 2

10

S = 2020 です。S の空でない連続する部分文字列は 10 個ありますが、その全てが 2 で割り切れるので 10 を出力してください。

先頭が 0 である部分文字列も許容されることに注意してください。


入力例 3

20 11
33883322005544116655

出力例 3

68

Score : 500 points

Problem Statement

Takahashi has a string S of length N consisting of digits from 0 through 9.

He loves the prime number P. He wants to know how many non-empty (contiguous) substrings of S - there are N \times (N + 1) / 2 of them - are divisible by P when regarded as integers written in base ten.

Here substrings starting with a 0 also count, and substrings originated from different positions in S are distinguished, even if they are equal as strings or integers.

Compute this count to help Takahashi.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • S consists of digits.
  • |S| = N
  • 2 \leq P \leq 10000
  • P is a prime number.

Input

Input is given from Standard Input in the following format:

N P
S

Output

Print the number of non-empty (contiguous) substrings of S that are divisible by P when regarded as an integer written in base ten.


Sample Input 1

4 3
3543

Sample Output 1

6

Here S = 3543. There are ten non-empty (contiguous) substrings of S:

  • 3: divisible by 3.

  • 35: not divisible by 3.

  • 354: divisible by 3.

  • 3543: divisible by 3.

  • 5: not divisible by 3.

  • 54: divisible by 3.

  • 543: divisible by 3.

  • 4: not divisible by 3.

  • 43: not divisible by 3.

  • 3: divisible by 3.

Six of these are divisible by 3, so print 6.


Sample Input 2

4 2
2020

Sample Output 2

10

Here S = 2020. There are ten non-empty (contiguous) substrings of S, all of which are divisible by 2, so print 10.

Note that substrings beginning with a 0 also count.


Sample Input 3

20 11
33883322005544116655

Sample Output 3

68
F - Removing Robots

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

数直線上に、1 から N の番号のついた N 個のロボットが置かれています。ロボット i は座標 X_i に存在し、起動すると D_i だけ正の方向に移動し、移動を終えると同時に数直線上から取り除かれます。全てのロボットの移動速度は同じで、大きさは無視できます。

イタズラ好きの高橋君は、数直線上にロボットが残っている限り、好きなだけ以下の操作を行うことができます。(1 回も行わないことも可能です)

  • ロボットを 1 つ選び起動する。移動中のロボットが存在するときは行えない。

ロボット i が移動する過程で、数直線上の座標 X_i 以上 X_i + D_i 未満の範囲に残っている別のロボット j と接触したら、ロボット j も起動されて移動を開始します。この処理は再帰的に繰り返されます。

高橋君が操作を繰り返した後、数直線上に残っているロボットの組み合わせとして考えられるものは何通り存在するでしょうか。答えは非常に大きくなる場合があるので、998244353 で割った余りを出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • -10^9 \leq X_i \leq 10^9
  • 1 \leq D_i \leq 10^9
  • X_i \neq X_j (i \neq j)
  • 入力は全て整数である

入力

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

N
X_1 D_1
:
X_N D_N

出力

数直線上に残っているロボットの組み合わせとして考えられるものの個数を 998244353 で割った余りを出力せよ。


入力例 1

2
1 5
3 3

出力例 1

3

数直線上に残っているロボットの組み合わせとしては、\{1, 2\}, \{1\}, \{\}3 通りが考えられます。

これらは次のように操作すると実現されます。

  • 高橋君はロボットを起動しない。このとき、ロボット \{1, 2\} が残ります。

  • 高橋君がロボット 1 を起動する。このとき、ロボット 1 が移動する過程でロボット 2 を起動します。最終的にロボットは 1 つも残っていません。もしくは、高橋君がロボット 2 を起動した後ロボット 1 を起動しても、ロボットが残っていない状態にすることができます。

  • 高橋君がロボット 2 を起動し、操作を終了する。このとき、ロボット \{1\} が残ります。


入力例 2

3
6 5
-1 10
3 3

出力例 2

5

数直線上に残っているロボットの組み合わせとしては、\{1, 2, 3\}, \{1, 2\}, \{2\}, \{2, 3\}, \{\}5 通りが考えられます。


入力例 3

4
7 10
-10 3
4 3
-4 3

出力例 3

16

どのロボットも他のロボットに影響しません。


入力例 4

20
-8 1
26 4
0 5
9 1
19 4
22 20
28 27
11 8
-3 20
-25 17
10 4
-18 27
24 28
-11 19
2 27
-2 18
-1 12
-24 29
31 29
29 7

出力例 4

110

組み合わせとして考えられるものの個数を 998244353 で割った余りを出力することに注意してください。

Score : 600 points

Problem Statement

There are N robots numbered 1 to N placed on a number line. Robot i is placed at coordinate X_i. When activated, it will travel the distance of D_i in the positive direction, and then it will be removed from the number line. All the robots move at the same speed, and their sizes are ignorable.

Takahashi, who is a mischievous boy, can do the following operation any number of times (possibly zero) as long as there is a robot remaining on the number line.

  • Choose a robot and activate it. This operation cannot be done when there is a robot moving.

While Robot i is moving, if it touches another robot j that is remaining in the range [X_i, X_i + D_i) on the number line, Robot j also gets activated and starts moving. This process is repeated recursively.

How many possible sets of robots remaining on the number line are there after Takahashi does the operation some number of times? Compute this count modulo 998244353, since it can be enormous.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • -10^9 \leq X_i \leq 10^9
  • 1 \leq D_i \leq 10^9
  • X_i \neq X_j (i \neq j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 D_1
:
X_N D_N

Output

Print the number of possible sets of robots remaining on the number line, modulo 998244353.


Sample Input 1

2
1 5
3 3

Sample Output 1

3

There are three possible sets of robots remaining on the number line: \{1, 2\}, \{1\}, and \{\}.

These can be achieved as follows:

  • If Takahashi activates nothing, the robots \{1, 2\} will remain.

  • If Takahashi activates Robot 1, it will activate Robot 2 while moving, after which there will be no robots on the number line. This state can also be reached by activating Robot 2 and then Robot 1.

  • If Takahashi activates Robot 2 and finishes doing the operation, the robot \{1\} will remain.


Sample Input 2

3
6 5
-1 10
3 3

Sample Output 2

5

There are five possible sets of robots remaining on the number line: \{1, 2, 3\}, \{1, 2\}, \{2\}, \{2, 3\}, and \{\}.


Sample Input 3

4
7 10
-10 3
4 3
-4 3

Sample Output 3

16

None of the robots influences others.


Sample Input 4

20
-8 1
26 4
0 5
9 1
19 4
22 20
28 27
11 8
-3 20
-25 17
10 4
-18 27
24 28
-11 19
2 27
-2 18
-1 12
-24 29
31 29
29 7

Sample Output 4

110

Remember to print the count modulo 998244353.