A - Seismic magnitude scales

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

配点 : 100

問題文

地震のマグニチュードは、その地震のエネルギーの大きさを対数で表した値です。マグニチュードが 1 増える度にエネルギーは約 32 倍になることが知られています。
ここではマグニチュードが 1 増える度に地震のエネルギーがちょうど 32 倍になるとします。このとき、マグニチュード A の地震のエネルギーの大きさはマグニチュード B の地震のエネルギーの大きさの何倍ですか?

制約

  • 3\leq B\leq A\leq 9
  • A , B は整数

入力

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

A B

出力

答えを整数で出力せよ。


入力例 1

6 4

出力例 1

1024

64 より 2 だけ大きいので、 マグニチュード 6 の地震はマグニチュード 4 の地震と比べて 32\times 32=1024 倍のエネルギーを持っています。


入力例 2

5 5

出力例 2

1

マグニチュードが同じなのでエネルギーの大きさも同じです。

Score : 100 points

Problem Statement

The magnitude of an earthquake is a logarithmic scale of the energy released by the earthquake. It is known that each time the magnitude increases by 1, the amount of energy gets multiplied by approximately 32.
Here, we assume that the amount of energy gets multiplied by exactly 32 each time the magnitude increases by 1. In this case, how many times is the amount of energy of a magnitude A earthquake as much as that of a magnitude B earthquake?

Constraints

  • 3\leq B\leq A\leq 9
  • A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the answer as an integer.


Sample Input 1

6 4

Sample Output 1

1024

6 is 2 greater than 4, so a magnitude 6 earthquake has 32\times 32=1024 times as much energy as a magnitude 4 earthquake has.


Sample Input 2

5 5

Sample Output 2

1

Earthquakes with the same magnitude have the same amount of energy.

B - typo

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

配点 : 200

問題文

文字列 S, T が与えられます。以下の操作を高々 1行うことで、ST と一致させることができるかを判定してください。

  • S の隣り合う 2 文字を選び、入れ替える。

なお、上記の操作を一度も行わないことも可能です。

制約

  • S, T はそれぞれ英小文字のみからなる、長さ 2 以上 100 以下の文字列
  • S の長さと T の長さは等しい

入力

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

S
T

出力

問題文中の操作を高々 1 回行うことで ST と一致させることができるなら Yes を、できないなら No を出力せよ。


入力例 1

abc
acb

出力例 1

Yes

S2 文字目と 3 文字目を入れ替えることで、ST と一致させることができます。


入力例 2

aabb
bbaa

出力例 2

No

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


入力例 3

abcde
abcde

出力例 3

Yes

ST は既に一致しています。

Score : 200 points

Problem Statement

You are given two strings S and T. Determine whether it is possible to make S and T equal by doing the following operation at most once:

  • choose two adjacent characters in S and swap them.

Note that it is allowed to choose not to do the operation.

Constraints

  • Each of S and T is a string of length between 2 and 100 (inclusive) consisting of lowercase English letters.
  • S and T have the same length.

Input

Input is given from Standard Input in the following format:

S
T

Output

If it is possible to make S and T equal by doing the operation in Problem Statement at most once, print Yes; otherwise, print No.


Sample Input 1

abc
acb

Sample Output 1

Yes

You can swap the 2-nd and 3-rd characters of S to make S and T equal.


Sample Input 2

aabb
bbaa

Sample Output 2

No

There is no way to do the operation to make S and T equal.


Sample Input 3

abcde
abcde

Sample Output 3

Yes

S and T are already equal.

C - Select Mul

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

配点 : 300

問題文

整数 N が与えられます。N の各桁の数字を取り出して並べ(並べる順序は好きに変えてよい)、2 つの正整数に分離することを考えましょう。

例えば、123 という整数に対しては以下の 6 通りの分離の仕方が考えられます。

  • 123
  • 213
  • 132
  • 312
  • 231
  • 321

なお、ここで分離されたあとの 2 整数に leading zero が含まれていてはなりません。例えば、101 という整数を 1012 つに分離することはできません。また上述の「正整数に分離する」という条件より、1011102 つに分離することもできません。

適切に N を分離したとき、分離後の 2 数の積の最大値はいくらになりますか?

制約

  • N1 以上 10^9 以下の整数
  • N には 0 でない桁が 2 つ以上含まれる

入力

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

N

出力

分離後の 2 数の積の最大値を出力せよ。


入力例 1

123

出力例 1

63

問題文中にある通り、以下の 6 通りの分離の仕方が考えられます。

  • 123
  • 213
  • 132
  • 312
  • 231
  • 321

積はそれぞれ 36, 63, 26, 62, 23, 32 であり、この中の最大値は 63 です。


入力例 2

1010

出力例 2

100

考えられる分離の仕方は以下の 2 通りです。

  • 1001
  • 1010

いずれの場合にも積は 100 となります。


入力例 3

998244353

出力例 3

939337176

Score : 300 points

Problem Statement

You are given an integer N. Consider permuting the digits in N and separate them into two positive integers.

For example, for the integer 123, there are six ways to separate it, as follows:

  • 12 and 3,
  • 21 and 3,
  • 13 and 2,
  • 31 and 2,
  • 23 and 1,
  • 32 and 1.

Here, the two integers after separation must not contain leading zeros. For example, it is not allowed to separate the integer 101 into 1 and 01. Additionally, since the resulting integers must be positive, it is not allowed to separate 101 into 11 and 0, either.

What is the maximum possible product of the two resulting integers, obtained by the optimal separation?

Constraints

  • N is an integer between 1 and 10^9 (inclusive).
  • N contains two or more digits that are not 0.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible product of the two integers after separation.


Sample Input 1

123

Sample Output 1

63

As described in Problem Statement, there are six ways to separate it:

  • 12 and 3,
  • 21 and 3,
  • 13 and 2,
  • 31 and 2,
  • 23 and 1,
  • 32 and 1.

The products of these pairs, in this order, are 36, 63, 26, 62, 23, 32, with 63 being the maximum.


Sample Input 2

1010

Sample Output 2

100

There are two ways to separate it:

  • 100 and 1,
  • 10 and 10.

In either case, the product is 100.


Sample Input 3

998244353

Sample Output 3

939337176
D - Online games

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

配点 : 400

問題文

あるオンラインゲームがあり、 N 人のプレイヤーが登録しています。
サービス開始日から 10^{100} 日を迎えた今日、 開発者である高橋君がログイン履歴を調べたところ、 i 番目のプレイヤーはサービス開始日を 1 日目として、 A_i 日目から B_i 日間連続でログインし、 それ以外の日はログインしていなかったことが判明しました。 すなわち、i 番目のプレイヤーはサービス開始日から、A_i , A_i+1 , \ldots , A_i+B_i-1 日目に、 かつそれらの日にのみログインしていたことが分かりました。
1\leq k\leq N をみたす各整数 k について、 サービス開始日から今日までの間で、ちょうど k 人がログインしていた日数を答えてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力は全て整数である。

入力

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

N
A_1 B_1
A_2 B_2
:
A_N B_N

出力

次のように空白区切りで N 個の整数を出力せよ。

D_1 D_2 \cdots D_N

ただし、 D_i はちょうど i 人がゲームにログインしていた日数を表す。


入力例 1

3
1 2
2 3
3 1

出力例 1

2 2 0

1 番目のプレイヤーは 1 日目と 2 日目に、 2 番目のプレイヤーは 2 日目と 3 日目と 4 日目に、 3 番目のプレイヤーは 3 日目だけにログインしています。 よって、1, 4 日目には 1 人が、2, 3 日目には 2 人がログインしており、 それ以外の日は誰もログインしていない事が分かります。 答えはちょうど 1 人がログインした日数が 2 日、 ちょうど 2 人がログインした日数が 2 日、 ちょうど 3 人がログインした日数が 0 日となります。


入力例 2

2
1000000000 1000000000
1000000000 1000000000

出力例 2

0 1000000000

2 人以上のプレイヤーがちょうど同じ期間にログインしていることもあり得ます。

Score : 400 points

Problem Statement

There is an online game with N registered players.
Today, which is the 10^{100}-th day since its launch, the developer Takahashi examined the users' login history. It turned out that the i-th player logged in for B_i consecutive days from Day A_i, where Day 1 is the launch day, and did not log in for the other days. In other words, the i-th player logged in on Day A_i, A_i+1, \ldots, A_i+B_i-1, and only on those days.
For each integer k such that 1\leq k\leq N, find the number of days on which exactly k players logged in.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print N integers with spaces in between, as follows:

D_1 D_2 \cdots D_N

Here, D_i denotes the number of days on which exactly k players logged in.


Sample Input 1

3
1 2
2 3
3 1

Sample Output 1

2 2 0

The first player logged in on Day 1, 2, the second player logged in on Day 2, 3, 4, and the third player logged in on Day 3 only.

Thus, we can see that Day 1, 4 had 1 player logged in, Day 2, 3 had 2 players logged in, and the other days had no players logged in.

The answer is: there were 2 days with exactly 1 player logged in, 2 days with exactly 2 players logged in, and 0 days with exactly 3 players logged in.


Sample Input 2

2
1000000000 1000000000
1000000000 1000000000

Sample Output 2

0 1000000000

There may be two or more players who logged in during the same period.

E - LEQ

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

配点 : 500

問題文

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

A の連続するとは限らない、長さが 2 以上である部分列 A'=(A'_1,A'_2,\ldots,A'_k) のうち以下の条件を満たすものの個数を求めてください。

  • A'_1 \leq A'_k

なお、この値は非常に大きくなることがあるため、998244353 で割ったあまりを出力してください。

ただし、2 つの部分列は、列として同じであっても、取り出す添字が異なる場合は区別されます。

制約

  • 2 \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

A の連続するとは限らない、長さが 2 以上である部分列のうち問題文中の条件を満たすものの個数を、998244353 で割ったあまりを出力せよ。


入力例 1

3
1 2 1

出力例 1

3

A=(1,2,1) の連続するとは限らない、長さが 2 以上である部分列は (1,2), (1,1), (2,1), (1,2,1)4 通りあります。

そのうち問題文中の条件を満たすものは、(1,2), (1,1), (1,2,1)3 通りです。


入力例 2

3
1 2 2

出力例 2

4

列として同じであっても、取り出す添字が異なる場合 2 つの部分列は区別されることに注意してください。

この入出力例において、問題文中の条件を満たすような部分列は (1,2), (1,2), (2,2), (1,2,2)4 通りです。


入力例 3

3
3 2 1

出力例 3

0

問題文中の条件を満たすような部分列が存在しない場合もあります。


入力例 4

10
198495780 28463047 859606611 212983738 946249513 789612890 782044670 700201033 367981604 302538501

出力例 4

830

Score : 500 points

Problem Statement

Given is a sequence of N integers: A = (A_1, A_2, \dots, A_N).

Find the number of (not necessarily contiguous) subsequences A'=(A'_1,A'_2,\ldots,A'_k) of length at least 2 that satisfy the following condition:

  • A'_1 \leq A'_k.

Since the count can be enormous, print it modulo 998244353.

Here, two subsequences are distinguished when they originate from different sets of indices, even if they are the same as sequences.

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • 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 A_2 \ldots A_N

Output

Print the number of (not necessarily contiguous) subsequences A'=(A'_1,A'_2,\ldots,A'_k) of length at least 2 that satisfy the condition in Problem Statement.


Sample Input 1

3
1 2 1

Sample Output 1

3

A=(1,2,1) has four (not necessarily contiguous) subsequences of length at least 2: (1,2), (1,1), (2,1), (1,2,1).

Three of them, (1,2), (1,1), (1,2,1), satisfy the condition in Problem Statement.


Sample Input 2

3
1 2 2

Sample Output 2

4

Note that two subsequences are distinguished when they originate from different sets of indices, even if they are the same as sequences.

In this Sample, there are four subsequences, (1,2), (1,2), (2,2), (1,2,2), that satisfy the condition.


Sample Input 3

3
3 2 1

Sample Output 3

0

There may be no subsequence that satisfies the condition.


Sample Input 4

10
198495780 28463047 859606611 212983738 946249513 789612890 782044670 700201033 367981604 302538501

Sample Output 4

830
F - Diameter set

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

配点 : 500

問題文

N 頂点からなる木が与えられます。 頂点は 1 , 2 , \ldots , N と番号付けられており、 1\leq i\leq N-1 について、i 本目の辺は頂点 U_i と頂点 V_i を結んでいます。 木の直径を D とするとき、木の頂点のうち 2 点以上を選んで赤く塗る方法であって、 赤く塗られたどの頂点の間の距離も D であるようなものの数を 998244353 で割った余りを求めてください。

ただし、木の 2 頂点の間の距離は一方から他方へ移動するときに用いる辺の本数の最小値であり、 木の直径は任意の 2 頂点の間の距離の最大値として定められます。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq U_i,V_i \leq N
  • U_i \neq V_i
  • 入力は全て整数である。
  • 与えられるグラフは木である。

入力

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

N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

出力

答えを出力せよ。


入力例 1

5
1 2
1 3
1 4
4 5

出力例 1

2

与えられた木は 5 頂点からなり、直径は 3 です。
2 頂点の組であって、その間の距離が 3 であるようなものは (2,5) , (3,5) しか存在しないため、 条件をみたす塗り方は \lbrace 2,5\rbrace \lbrace 3,5\rbrace 2 通りとなります。
\lbrace 2,3,5\rbrace は頂点 2 と頂点 3 の間の距離が 2 であるため条件をみたさないことに注意してください。


入力例 2

4
1 2
1 3
1 4

出力例 2

4

直径は 2 であり、条件をみたす塗り方は \lbrace 2,3\rbrace , \lbrace 2,4\rbrace , \lbrace 3,4\rbrace , \lbrace 2,3,4\rbrace 4 通りとなります。

Score : 500 points

Problem Statement

Given is a tree with N vertices. The vertices are numbered 1, 2, \ldots, N, and for each 1\leq i\leq N-1, the i-th edge connects Vertex U_i and Vertex V_i. Let D be the diameter of the tree. Find the number, modulo 998244353, of ways to choose two or more of the vertices and paint them red so that all distances between two red vertices are D.

Here, the distance between two vertices is the minimum number of edges that must be traversed to travel from one vertex to the other, and the diameter of the tree is the maximum distance between two vertices.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq U_i,V_i \leq N
  • U_i \neq V_i
  • All values in input are integers.
  • The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
U_1 V_1
U_2 V_2
\vdots
U_{N-1} V_{N-1}

Output

Print the answer.


Sample Input 1

5
1 2
1 3
1 4
4 5

Sample Output 1

2

The given tree has five vertices and a diameter of 3.
There are just two pairs of vertices whose distance is 3: (2,5) and (3,5), so there are two ways to paint the tree to satisfy the condition: \lbrace 2,5\rbrace and \lbrace 3,5\rbrace .
Note that painting 2,3,5 does not satisfy the condition since the distance between Vertex 2 and Vertex 3 is 2.


Sample Input 2

4
1 2
1 3
1 4

Sample Output 2

4

The diameter is 2, and the four ways to paint the tree to satisfy the condition are: \lbrace 2,3\rbrace , \lbrace 2,4\rbrace , \lbrace 3,4\rbrace , \lbrace 2,3,4\rbrace .

G - Jumping sequence

実行時間制限: 5 sec / メモリ制限: 1024 MB

問題文

無限に広がる 2 次元の座標平面を考えます。 高橋君は最初 (0,0) に立っており、今から上下左右いずれかの方向を選んでジャンプすることを N 回行います。 それぞれのジャンプで移動する距離は定まっており、具体的には i 回目のジャンプでは距離 D_i を移動します。 N 回のジャンプの後で、ちょうど位置 (A,B) にいるようにすることは可能か判定し、 可能ならばそのような移動方法を 1 つ示してください。

ただし、現在の位置が (X,Y) のときに、それぞれの方向を選んで距離 D のジャンプをしたときの着地地点はそれぞれ以下の通りです。

  • 上方向 : (X,Y) \to (X,Y+D)
  • 下方向 : (X,Y) \to (X,Y-D)
  • 左方向 : (X,Y) \to (X-D,Y)
  • 右方向 : (X,Y) \to (X+D,Y)

制約

  • 1 \leq N \leq 2000
  • \lvert A\rvert, \lvert B\rvert \leq 3.6\times 10^6
  • 1 \leq D_i \leq 1800
  • 入力は全て整数である。

入力

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

N A B
D_1 D_2 \ldots D_N

出力

条件をみたす移動方法が存在するならば Yes 、そうでないならば No1 行目に出力せよ。
Yes の場合は 2 行目に条件をみたす移動方法を、U , D , L , R からなる長さ N の文字列 S として出力せよ。
ただし、Si 文字目が

  • U のとき、 i 回目のジャンプでは上方向に移動する
  • D のとき、 i 回目のジャンプでは下方向に移動する
  • L のとき、 i 回目のジャンプでは左方向に移動する
  • R のとき、 i 回目のジャンプでは右方向に移動する

ことを指す。


入力例 1

3 2 -2
1 2 3

出力例 1

Yes
LDR

1 回目のジャンプで左方向に、2 回目のジャンプで下方向に、3 回目のジャンプで右方向にジャンプすると、 (0,0)\to(-1,0)\to(-1,-2)\to(2,-2) と高橋君は動き、 最終的に (2,-2) に到達しているためこれは条件をみたしています。


入力例 2

2 1 0
1 6

出力例 2

No

2 回のジャンプの後でちょうど (1,0) にいるようにすることはできません。


入力例 3

5 6 7
1 3 5 7 9

出力例 3

Yes
LRLUR

Score : 600 points

Problem Statement

Consider an infinite two-dimensional coordinate plane. Takahashi, who is initially standing at (0,0), will do N jumps in one of the four directions he chooses every time: up, down, left, or right. The length of each jump is fixed. Specifically, the i-th jump should cover the distance of D_i. Determine whether it is possible to be exactly at (A, B) after N jumps. If it is possible, show one way to do so.

Here, for each direction, a jump of length D from (X, Y) takes him to the following point:

  • up: (X,Y) \to (X,Y+D)
  • down: (X,Y) \to (X,Y-D)
  • left: (X,Y) \to (X-D,Y)
  • right: (X,Y) \to (X+D,Y).

Constraints

  • 1 \leq N \leq 2000
  • \lvert A\rvert, \lvert B\rvert \leq 3.6\times 10^6
  • 1 \leq D_i \leq 1800
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B
D_1 D_2 \ldots D_N

Output

In the first line, print Yes if there is a desired sequence of jumps, and No otherwise.
In the case of Yes, print in the second line a desired sequence of jumps as a string S of length N consisting of U , D , L , R, as follows:

  • if the i-th jump is upward, the i-th character should be U;
  • if the i-th jump is downward, the i-th character should be D;
  • if the i-th jump is to the left, the i-th character should be L;
  • if the i-th jump is to the right, the i-th character should be R.

Sample Input 1

3 2 -2
1 2 3

Sample Output 1

Yes
LDR

If he jumps left, down, right in this order, Takahashi moves (0,0)\to(-1,0)\to(-1,-2)\to(2,-2) and ends up at (2, -2), which is desired.


Sample Input 2

2 1 0
1 6

Sample Output 2

No

It is impossible to be exactly at (1, 0) after the two jumps.


Sample Input 3

5 6 7
1 3 5 7 9

Sample Output 3

Yes
LRLUR
H - Count Multiset

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

配点 : 600

問題文

正の整数 N, M が与えられます。

k=1,2,\ldots,N について以下の値を求め、998244353 で割ったあまりをそれぞれ出力してください。

  • k 個の正整数からなる多重集合 A のうち、以下の 2 つの条件をすべて満たすものの個数
    • A に含まれる要素の総和は N
    • 任意の正整数 x について、Ax を高々 M 個しか含まない

制約

  • 1 \leq M \leq N \leq 5000
  • 入力はすべて整数

入力

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

N M

出力

N 行に渡って出力せよ。i\ (1 \leq i \leq N) 行目には、k=i の場合の答えを出力すること。


入力例 1

4 2

出力例 1

1
2
1
0
  • k=1 のとき、問題文中の条件を満たすような多重集合 A\{4\}1 通りです。
  • k=2 のとき、問題文中の条件を満たすような多重集合 A\{1,3\}\{2,2\}2 通りです。
  • k=3 のとき、問題文中の条件を満たすような多重集合 A\{1,1,2\}1 通りです。
  • k=4 のとき、問題文中の条件を満たすような多重集合 A1 つも存在しません。

入力例 2

7 7

出力例 2

1
3
4
3
2
1
1

Score : 600 points

Problem Statement

Given are positive integers N and M.

For each k=1,2,\ldots,N, find the following number and print it modulo 998244353.

  • The number of multisets A containing k positive integers that satisfy both of the following conditions:
    • the sum of the elements of A is N;
    • for every positive integer x, A contains at most M occurrences of x.

Constraints

  • 1 \leq M \leq N \leq 5000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print N lines; the i-th line (1 \leq i \leq N) should contain the answer for the case k=i.


Sample Input 1

4 2

Sample Output 1

1
2
1
0
  • For k=1, there is one multiset A that satisfies the conditions: \{4\}.
  • For k=2, there are two multisets A that satisfy the conditions: \{1,3\} and \{2,2\}.
  • For k=3, there is one multiset A that satisfies the conditions: \{1,1,2\}.
  • For k=4, there is no multiset A that satisfies the conditions.

Sample Input 2

7 7

Sample Output 2

1
3
4
3
2
1
1