E - Loong Tracking

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

配点 : 300

問題文

高橋君は座標平面上で龍を操作するゲームを作成しました。

龍は 1 から N までの番号がついた N 個のパーツからなり、パーツ 1 と呼びます。

最初パーツ i は座標 (i,0) にあります。以下のクエリを Q 個処理してください。

  • 1 C: 頭を方向 C1 移動させる。ここで、CR, L, U, D のいずれかであり、それぞれ x 軸正方向、x 軸負方向、y 軸正方向、y 軸負方向を意味する。頭以外の全てのパーツは前のパーツに追従するように動く。すなわち、パーツ i\ \ (2\leq i \leq N) は移動前にパーツ i-1 があった座標に移動する。
  • 2 p: パーツ p のある座標を求める。

制約

  • 2 \leq N \leq 10^6
  • 1 \leq Q \leq 2\times 10^5
  • 1 種類目のクエリにおいて、CR, L, U, D のいずれか
  • 2 種類目のクエリにおいて、1\leq p \leq N
  • 入力に含まれる数値は全て整数

入力

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

N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

各クエリは以下の 2 種類のいずれかの形式である。

1 C
2 p

出力

2 種類目のクエリの回数を q として q 行出力せよ。
i 行目には、i 番目のそのようなクエリに対する答えの座標を (x,y) としたとき、x,y を空白区切りで出力せよ。


入力例 1

5 9
2 3
1 U
2 3
1 R
1 D
2 3
1 L
2 1
2 5

出力例 1

3 0
2 0
1 1
1 0
1 0

2 種類目のクエリを処理する各タイミングにおいて、パーツの位置は次のようになっています。

図

複数のパーツが同じ座標に存在しうることに注意してください。

Score : 300 points

Problem Statement

Takahashi has created a game where the player controls a dragon on a coordinate plane.

The dragon consists of N parts numbered 1 to N, with part 1 being called the head.

Initially, part i is located at the coordinates (i,0). Process Q queries as follows.

  • 1 C: Move the head by 1 in direction C. Here, C is one of R, L, U, and D, which represent the positive x-direction, negative x-direction, positive y-direction, and negative y-direction, respectively. Each part other than the head moves to follow the part in front of it. That is, part i (2\leq i \leq N) moves to the coordinates where part i-1 was before the move.
  • 2 p: Find the coordinates of part p.

Constraints

  • 2 \leq N \leq 10^6
  • 1 \leq Q \leq 2\times 10^5
  • For the first type of query, C is one of R, L, U, and D.
  • For the second type of query, 1\leq p \leq N.
  • All numerical input values are integers.

Input

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

N Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

Each query is in one of the following two formats:

1 C
2 p

Output

Print q lines, where q is the number of queries of the second type.
The i-th line should contain x and y separated by a space, where (x,y) are the answer to the i-th such query.


Sample Input 1

5 9
2 3
1 U
2 3
1 R
1 D
2 3
1 L
2 1
2 5

Sample Output 1

3 0
2 0
1 1
1 0
1 0

At each time when processing the second type of query, the parts are at the following positions:

Figure

Note that multiple parts may exist at the same coordinates.

F - Blue Spring

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

配点 : 300

問題文

高橋君は N 日間の鉄道旅行を計画しています。
高橋君はそれぞれの日について、運賃の通常料金を払うか、1 日周遊パスを 1 枚使用するか選ぶことができます。

ここで、1\leq i\leq N について、i 日目の旅行にかかる運賃の通常料金は F_i 円です。
一方、1 日周遊パスは D 枚セットで P 円で発売されており、何セットでも購入することが可能ですが、D 枚単位でしか購入することができません。
また、購入したパスは 1 枚ずつ好きな日に使うことができ、旅行が終了した時点で余っていても構いません。

N 日間の旅行でかかる金額、すなわち 1 日周遊パスの購入にかかった代金と、1 日周遊パスを利用しなかった日における運賃の通常料金の合計金額の和としてあり得る最小値を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq D\leq 2\times 10^5
  • 1\leq P\leq 10^9
  • 1\leq F_i\leq 10^9
  • 入力はすべて整数

入力

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

N D P
F_1 F_2 \ldots F_N

出力

N 日間の旅行でかかる金額としてあり得る最小値を出力せよ。


入力例 1

5 2 10
7 1 6 3 6

出力例 1

20

1 日周遊パスを 1 セットだけ購入し、1 日目と 3 日目に使用すると、合計金額は (10\times 1)+(0+1+0+3+6)=20 となり、このときかかる金額が最小となります。
よって、20 を出力します。


入力例 2

3 1 10
1 2 3

出力例 2

6

3 日間すべてにおいて運賃の通常料金を支払ったときに最小となります。


入力例 3

8 3 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

出力例 3

3000000000

1 日周遊パスを 3 セット購入し、8 日間すべてにおいて 1 日周遊パスを利用したときに最小となります。
答えが 32 bit 整数型に収まらないことがあることに注意してください。

Score : 300 points

Problem Statement

Takahashi is planning an N-day train trip.
For each day, he can pay the regular fare or use a one-day pass.

Here, for 1\leq i\leq N, the regular fare for the i-th day of the trip is F_i yen.
On the other hand, a batch of D one-day passes is sold for P yen. You can buy as many passes as you want, but only in units of D.
Each purchased pass can be used on any day, and it is fine to have some leftovers at the end of the trip.

Find the minimum possible total cost for the N-day trip, that is, the cost of purchasing one-day passes plus the total regular fare for the days not covered by one-day passes.

Constraints

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

Input

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

N D P
F_1 F_2 \ldots F_N

Output

Print the minimum possible total cost for the N-day trip.


Sample Input 1

5 2 10
7 1 6 3 6

Sample Output 1

20

If he buys just one batch of one-day passes and uses them for the first and third days, the total cost will be (10\times 1)+(0+1+0+3+6)=20, which is the minimum cost needed.
Thus, print 20.


Sample Input 2

3 1 10
1 2 3

Sample Output 2

6

The minimum cost is achieved by paying the regular fare for all three days.


Sample Input 3

8 3 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

3000000000

The minimum cost is achieved by buying three batches of one-day passes and using them for all eight days.
Note that the answer may not fit into a 32-bit integer type.

G - Divide by 2 or 3

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

配点 : 400

問題文

正整数列 A=(a_1,a_2,\ldots,a_N) が与えられます。
あなたは以下の操作のうち 1 つを選んで行うことを 0 回以上何度でも繰り返せます。

  • 1 \leq i \leq N かつ a_i2 の倍数であるような整数 i を選び、a_i\frac{a_i}{2} に置き換える
  • 1 \leq i \leq N かつ a_i3 の倍数であるような整数 i を選び、a_i\frac{a_i}{3} に置き換える

あなたの目標は Aa_1=a_2=\ldots=a_N を満たす状態にすることです。
目標を達成するために必要な操作の回数の最小値を求めてください。ただし、どのように操作を行っても目標を達成できない場合、代わりに -1 と出力してください。

制約

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

入力

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

N
a_1 a_2 \ldots a_N

出力

答えを出力せよ。


入力例 1

3
1 4 3

出力例 1

3

次のように操作をすると 3 回で目標を達成でき、これが最小の回数です。

  • a_i2 の倍数であるような整数 i として 2 を選び、a_2\frac{a_2}{2} に置き換える。A(1,2,3) となる。
  • a_i2 の倍数であるような整数 i として 2 を選び、a_2\frac{a_2}{2} に置き換える。A(1,1,3) となる。
  • a_i3 の倍数であるような整数 i として 3 を選び、a_3\frac{a_3}{3} に置き換える。A(1,1,1) となる。

入力例 2

3
2 7 6

出力例 2

-1

どのように操作を行っても目標を達成できません。


入力例 3

6
1 1 1 1 1 1

出力例 3

0

Score : 400 points

Problem Statement

You are given a sequence of positive integers: A=(a_1,a_2,\ldots,a_N).
You can choose and perform one of the following operations any number of times, possibly zero.

  • Choose an integer i such that 1 \leq i \leq N and a_i is a multiple of 2, and replace a_i with \frac{a_i}{2}.
  • Choose an integer i such that 1 \leq i \leq N and a_i is a multiple of 3, and replace a_i with \frac{a_i}{3}.

Your objective is to make A satisfy a_1=a_2=\ldots=a_N.
Find the minimum total number of times you need to perform an operation to achieve the objective. If there is no way to achieve the objective, print -1 instead.

Constraints

  • 2 \leq N \leq 1000
  • 1 \leq a_i \leq 10^9
  • All values in the input 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

3
1 4 3

Sample Output 1

3

Here is a way to achieve the objective in three operations, which is the minimum needed.

  • Choose an integer i=2 such that a_i is a multiple of 2, and replace a_2 with \frac{a_2}{2}. A becomes (1,2,3).
  • Choose an integer i=2 such that a_i is a multiple of 2, and replace a_2 with \frac{a_2}{2}. A becomes (1,1,3).
  • Choose an integer i=3 such that a_i is a multiple of 3, and replace a_3 with \frac{a_3}{3}. A becomes (1,1,1).

Sample Input 2

3
2 7 6

Sample Output 2

-1

There is no way to achieve the objective.


Sample Input 3

6
1 1 1 1 1 1

Sample Output 3

0
H - Integer Sequence Fair

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

配点 : 500

問題文

整数列を一堂に集めてその優劣を定める、整数列品評会が行われます。 品評会では、1 以上 K 以下の整数からなる長さ N の整数列すべてが審査対象となり、 審査対象の数列それぞれに対して 1 以上 M 以下の整数の点数をつけます。

「審査対象の数列それぞれに対して 1 以上 M 以下の整数の点数をつける方法」が何通りあるかを 998244353 で割ったあまりを出力してください。

ただし、2 つの方法が異なるとは「審査対象となるある数列 A = (A_1, A_2, \ldots, A_N) が存在して、 A に対してつける点数が 2 つの方法で異なる」ことを言います。

制約

  • 1 \leq N, K, M \leq 10^{18}
  • N, K, M は整数

入力

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

N K M

出力

「審査対象の数列それぞれに対して 1 以上 M 以下の整数の点数をつける方法」が何通りあるかを 998244353 で割ったあまりを出力せよ。


入力例 1

2 2 2

出力例 1

16

審査対象となる数列は、(1, 1), (1, 2), (2, 1), (2, 2)4 つです。「審査対象の数列それぞれに対して 1 以上 2 以下の整数の点数をつける方法」は、以下の 16 通りあります。

  • (1, 1)1 点、(1, 2)1 点、(2, 1)1 点、(2, 2)1 点をつける方法
  • (1, 1)1 点、(1, 2)1 点、(2, 1)1 点、(2, 2)2 点をつける方法
  • (1, 1)1 点、(1, 2)1 点、(2, 1)2 点、(2, 2)1 点をつける方法
  • (1, 1)1 点、(1, 2)1 点、(2, 1)2 点、(2, 2)2 点をつける方法
  • \cdots
  • (1, 1)2 点、(1, 2)2 点、(2, 1)2 点、(2, 2)2 点をつける方法

よって、16 を出力します。


入力例 2

3 14 15926535

出力例 2

109718301

998244353 で割ったあまりを出力することに注意してください。

Score : 500 points

Problem Statement

Integer Sequence Exhibition is taking place, where integer sequences are gathered in one place and evaluated. Here, every integer sequence of length N consisting of integers between 1 and K (inclusive) is evaluated and given an integer score between 1 and M (inclusive).

Print the number, modulo 998244353, of ways to give each of the evaluated sequences a score between 1 and M.

Here, two ways are said to be different when there is an evaluated sequence A = (A_1, A_2, \ldots, A_N) that is given different scores by the two ways.

Constraints

  • 1 \leq N, K, M \leq 10^{18}
  • N, K, and M are integers.

Input

Input is given from Standard Input in the following format:

N K M

Output

Print the number, modulo 998244353, of ways to give each of the evaluated sequences a score between 1 and M.


Sample Input 1

2 2 2

Sample Output 1

16

Four sequences are evaluated: (1, 1), (1, 2), (2, 1), and (2, 2). There are 16 ways to give each of these sequences a score between 1 and 2, as follows.

  • Give 1 to (1, 1), 1 to (1, 2), 1 to (2, 1), and 1 to (2, 2)
  • Give 1 to (1, 1), 1 to (1, 2), 1 to (2, 1), and 2 to (2, 2)
  • Give 1 to (1, 1), 1 to (1, 2), 2 to (2, 1), and 1 to (2, 2)
  • Give 1 to (1, 1), 1 to (1, 2), 2 to (2, 1), and 2 to (2, 2)
  • \cdots
  • Give 2 to (1, 1), 2 to (1, 2), 2 to (2, 1), and 2 to (2, 2)

Thus, we print 16.


Sample Input 2

3 14 15926535

Sample Output 2

109718301

Be sure to print the count modulo 998244353.

I - Random Update Query

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

配点 : 550

問題文

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

A に対して、i = 1, 2, \ldots, M の順に下記の操作を行います。

  • まず、L_i 以上 R_i 以下の整数を等確率でランダムに 1 個を選び、それを p とおく。
  • そして、A_p の値を整数 X_i に変更する。

上記の手順を行った後の最終的な A における、A_i の期待値 \text{mod } 998244353 を、 各 i = 1, 2, \ldots, N について出力してください。

期待値 \text{mod } 998244353 の定義

この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

  • 入力される値は全て整数
  • 1 \leq N, M \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 \leq L_i \leq R_i \leq N
  • 0 \leq X_i \leq 10^9

入力

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

N M
A_1 A_2 \ldots A_N
L_1 R_1 X_1
L_2 R_2 X_2
\vdots
L_M R_M X_M

出力

i = 1, 2, \ldots, N に対する最終的な A_i の期待値 E_i を、 下記の形式の通りに空白区切りで出力せよ。

E_1 E_2 \ldots E_N

入力例 1

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

出力例 1

499122179 1 665496238 665496236 5

A = (3, 1, 4, 1, 5) である初期状態からはじめ、下記の通りに 2 個の操作が行われます。

  • まず、1 個目の操作で、A_1A_2 のどちらか一方が等確率でランダムに選ばれ、その値が 2 に変更されます。
  • その後、2 個目の操作で、A_2, A_3, A_4 のうちいずれか 1 個が等確率でランダムに選ばれ、その値が 0 に変更されます。

その結果、最終的な A の各要素の期待値は (E_1, E_2, E_3, E_4, E_5) = (\frac{5}{2}, 1, \frac{8}{3}, \frac{2}{3}, 5) です。


入力例 2

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

出力例 2

5 6

入力例 3

20 20
998769066 273215338 827984962 78974225 994243956 791478211 891861897 680427073 993663022 219733184 570206440 43712322 66791680 164318676 209536492 137458233 289158777 461179891 612373851 330908158
12 18 769877494
9 13 689822685
6 13 180913148
2 16 525285434
2 14 98115570
14 17 622616620
8 12 476462455
13 17 872412050
14 15 564176146
7 13 143650548
2 5 180435257
4 10 82903366
1 2 643996562
8 10 262860196
10 14 624081934
11 13 581257775
9 19 381806138
3 12 427930466
6 19 18249485
14 19 682428942

出力例 3

821382814 987210378 819486592 142238362 447960587 678128197 687469071 405316549 318941070 457450677 426617745 712263899 939619994 228431878 307695685 196179692 241456697 12668393 685902422 330908158

Score : 550 points

Problem Statement

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

We will perform the following operation on A for i = 1, 2, \ldots, M in this order.

  • First, choose an integer between L_i and R_i, inclusive, uniformly at random and denote it as p.
  • Then, change the value of A_p to the integer X_i.

For the final sequence A after the above procedure, print the expected value, modulo 998244353, of A_i for each i = 1, 2, \ldots, N.

How to print expected values modulo 998244353

It can be proved that the expected values sought in this problem are always rational. Furthermore, the constraints of this problem guarantee that if each of those expected values is expressed as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353.

Now, there is a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Report this z.

Constraints

  • All input values are integers.
  • 1 \leq N, M \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 \leq L_i \leq R_i \leq N
  • 0 \leq X_i \leq 10^9

Input

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

N M
A_1 A_2 \ldots A_N
L_1 R_1 X_1
L_2 R_2 X_2
\vdots
L_M R_M X_M

Output

Print the expected values E_i of the final A_i for i = 1, 2, \ldots, N in the format below, separated by spaces.

E_1 E_2 \ldots E_N

Sample Input 1

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

Sample Output 1

499122179 1 665496238 665496236 5

We start from the initial state A = (3, 1, 4, 1, 5) and perform the following two operations.

  • The first operation chooses A_1 or A_2 uniformly at random, and changes its value to 2.
  • Then, the second operation chooses one of A_2, A_3, A_4 uniformly at random, and changes its value to 0.

As a result, the expected values of the elements in the final A are (E_1, E_2, E_3, E_4, E_5) = (\frac{5}{2}, 1, \frac{8}{3}, \frac{2}{3}, 5).


Sample Input 2

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

Sample Output 2

5 6

Sample Input 3

20 20
998769066 273215338 827984962 78974225 994243956 791478211 891861897 680427073 993663022 219733184 570206440 43712322 66791680 164318676 209536492 137458233 289158777 461179891 612373851 330908158
12 18 769877494
9 13 689822685
6 13 180913148
2 16 525285434
2 14 98115570
14 17 622616620
8 12 476462455
13 17 872412050
14 15 564176146
7 13 143650548
2 5 180435257
4 10 82903366
1 2 643996562
8 10 262860196
10 14 624081934
11 13 581257775
9 19 381806138
3 12 427930466
6 19 18249485
14 19 682428942

Sample Output 3

821382814 987210378 819486592 142238362 447960587 678128197 687469071 405316549 318941070 457450677 426617745 712263899 939619994 228431878 307695685 196179692 241456697 12668393 685902422 330908158