A - A Recursive Function

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

非負整数 x に対し定義される関数 f(x) は以下の条件を満たします。

  • f(0) = 1
  • 任意の正整数 k に対し f(k) = k \times f(k-1)

このとき、 f(N) を求めてください。

制約

  • N は 0 \le N \le 10 を満たす整数

入力

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

N

出力

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


入力例 1

2

出力例 1

2

f(2) = 2 \times f(1) = 2 \times 1 \times f(0) = 2 \times 1 \times 1 = 2 です。


入力例 2

3

出力例 2

6

f(3) = 3 \times f(2) = 3 \times 2 = 6 です。


入力例 3

0

出力例 3

1

入力例 4

10

出力例 4

3628800

Score : 100 points

Problem Statement

A function f(x) defined for non-negative integer x satisfies the following conditions:

  • f(0) = 1;
  • f(k) = k \times f(k-1) for all positive integers k.

Find f(N).

Constraints

  • N is an integer such that 0 \le N \le 10.

Input

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

N

Output

Print the answer as an integer.


Sample Input 1

2

Sample Output 1

2

We have f(2) = 2 \times f(1) = 2 \times 1 \times f(0) = 2 \times 1 \times 1 = 2.


Sample Input 2

3

Sample Output 2

6

We have f(3) = 3 \times f(2) = 3 \times 2 = 6.


Sample Input 3

0

Sample Output 3

1

Sample Input 4

10

Sample Output 4

3628800
B - Online Shopping

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

AtCoder 社は、オンラインショップでグッズを販売しています。

高橋君はそこで N 種類の商品を購入することにしました。
1 以上 N 以下の整数 i について、i 種類目の商品は 1 個 P_i 円で、高橋君は Q_i 個購入します。

また、高橋君は送料を支払う必要があります。
送料は購入する商品の合計金額が S 円以上なら 0 円、そうでないならば K 円です。

高橋君が支払う金額は購入する商品の合計金額と送料の和です。
高橋君が支払う金額を求めてください。

制約

  • 1\leq N\leq 100
  • 1\leq S\leq 10000
  • 1\leq K\leq 10000
  • 1\leq P_i\leq 10000
  • 1\leq Q_i\leq 100
  • 入力はすべて整数

入力

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

N S K
P_1 Q_1
P_2 Q_2
\vdots
P_N Q_N

出力

高橋君がオンラインショッピングで支払う金額を出力せよ。


入力例 1

2 2000 500
1000 1
100 6

出力例 1

2100

高橋君は 1 個 1000 円の商品を 1 個と、 1 個 100 円の商品を 6 つ購入します。
よって、購入する商品の合計金額は 1000\times 1+100\times 6=1600 円となります。
このとき購入する商品の合計金額は 2000 円未満であるため、送料は 500 円となります。
よって、高橋君の支払う金額は 1600+500=2100 円となります。


入力例 2

3 2000 500
1000 1
100 6
5000 1

出力例 2

6600

購入する商品の合計金額は 1000\times 1+100\times 6+5000\times 1=6600 円となります。
このとき購入する商品の合計金額は 2000 円以上であるため、送料は 0 円となります。
よって、高橋君の支払う金額は 6600+0=6600 円となります。


入力例 3

2 2000 500
1000 1
1000 1

出力例 3

2000

1 個あたりの価格が同じ商品が複数存在することもあります。

Score : 100 points

Problem Statement

AtCoder Inc. sells merchandise through its online shop.

Takahashi has decided to purchase N types of products from there.
For each integer i from 1 to N, the i-th type of product has a price of P_i yen each, and he will buy Q_i of this.

Additionally, he must pay a shipping fee.
The shipping fee is 0 yen if the total price of the products purchased is S yen or above, and K yen otherwise.

He will pay the total price of the products purchased plus the shipping fee.
Calculate the amount he will pay.

Constraints

  • 1\leq N\leq 100
  • 1\leq S\leq 10000
  • 1\leq K\leq 10000
  • 1\leq P_i\leq 10000
  • 1\leq Q_i\leq 100
  • All input values are integers.

Input

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

N S K
P_1 Q_1
P_2 Q_2
\vdots
P_N Q_N

Output

Print the amount Takahashi will pay for online shopping.


Sample Input 1

2 2000 500
1000 1
100 6

Sample Output 1

2100

Takahashi buys one product for 1000 yen and six products for 100 yen each.
Thus, the total price of the products is 1000\times 1+100\times 6=1600 yen.
Since the total amount for the products is less than 2000 yen, the shipping fee will be 500 yen.
Therefore, the amount Takahashi will pay is 1600+500=2100 yen.


Sample Input 2

3 2000 500
1000 1
100 6
5000 1

Sample Output 2

6600

The total price of the products is 1000\times 1+100\times 6+5000\times 1=6600 yen.
Since the total amount for the products is not less than 2000 yen, the shipping fee will be 0 yen.
Therefore, the amount Takahashi will pay is 6600+0=6600 yen.


Sample Input 3

2 2000 500
1000 1
1000 1

Sample Output 3

2000

There may be multiple products with the same price per item.

C - Compression

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150 点

問題文

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

A に含まれる数を重複を除いて小さい順に出力してください。

制約

  • 1\le N\le 100
  • 1\le A_i\le 100
  • 入力される値は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

A に含まれる数を小さい順に C_1,C_2,\ldots , C_M として、以下の形式で出力せよ。

M
C_1 C_2 \ldots C_M

入力例 1

4
3 1 4 1

出力例 1

3
1 3 4

A=(3,1,4,1) に含まれる数は小さい順に 1,3,4 の 3 つです。したがって、上記のように出力してください。


入力例 2

3
7 7 7

出力例 2

1
7

入力例 3

8
19 5 5 19 5 19 4 19

出力例 3

3
4 5 19

Score : 150 points

Problem Statement

An integer sequence A=(A_1,A_2,\ldots,A_N) of length N is given.

Output the numbers contained in A in ascending order, removing duplicates.

Constraints

  • 1\le N\le 100
  • 1\le A_i\le 100
  • 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

Let C_1,C_2,\ldots , C_M be the numbers contained in A in ascending order. Output in the following format:

M
C_1 C_2 \ldots C_M

Sample Input 1

4
3 1 4 1

Sample Output 1

3
1 3 4

The numbers contained in A=(3,1,4,1) are 1,3,4 in ascending order, totalling 3 distinct numbers. Therefore, output as shown above.


Sample Input 2

3
7 7 7

Sample Output 2

1
7

Sample Input 3

8
19 5 5 19 5 19 4 19

Sample Output 3

3
4 5 19
D - Find snuke

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250 点

問題文

縦 H マス \times 横 W マスのマス目があり、各マスに 1 つずつ英小文字が書き込まれています。 上から i 行目かつ左から j 列目のマスを (i,j) で表します。

マス目に書き込まれている英小文字は H 個の長さ W の文字列 S_1,S_2,\ldots, S_H によって与えられ、 S_i の j 文字目が、(i, j) に書き込まれた英小文字を表します。

マス目の中に、s, n, u, k, e が この順に(縦・横・ななめのいずれかの方向に) 連続して並んでいる 場所がただ 1 つ存在します。
そのような場所を見つけ、そのマスの位置を出力の形式に従って出力してください。

ただし、s, n, u, k, e が この順に(縦・横・ななめのいずれかの方向に) 連続して並んでいる場所とは、 5 つのマスの組 (A_1,A_2,A_3,A_4,A_5) であって、次をすべてみたすものをさします。

  • A_1,A_2,A_3,A_4,A_5 に書き込まれた英小文字はそれぞれ s, n, u, k, e である。
  • 1\leq i\leq 4 について、A_i と A_{i+1} は頂点または辺を共有している。
  • A_1,A_2,A_3,A_4,A_5 の中心はこの順に一直線上に等間隔で並んでいる。

制約

  • 5\leq H\leq 100
  • 5\leq W\leq 100
  • H,W は整数
  • S_i は英小文字のみからなる長さ W の文字列
  • 与えられるマス目の中に条件をみたす場所がただ 1 つ存在する

入力

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

H W
S_1
S_2
\vdots
S_H

出力

次の形式にしたがって、5 行出力せよ。

条件をみたす場所のうち s, n, u, k, e が書かれたマスがそれぞれ (R_1,C_1), (R_2,C_2)\ldots,(R_5,C_5) であるとき、 i 行目には R_i と C_i をこの順に空白区切りで出力せよ。

すなわち、以下のように出力せよ。

R_1 C_1
R_2 C_2
\vdots
R_5 C_5

以下の入力例も参考にせよ。


入力例 1

6 6
vgxgpu
amkxks
zhkbpp
hykink
esnuke
zplvfj

出力例 1

5 2
5 3
5 4
5 5
5 6

この時、(A_1,A_2,A_3,A_4,A_5)=((5,2),(5,3),(5,4),(5,5),(5,6)) とすると、
それぞれのマスに書き込まれた英小文字は s, n, u, k, e であり、
1\leq i\leq 4 について、A_i と A_{i+1} は辺を共有しており、
各マスの中心は一直線上に存在するため、条件をみたしています。


入力例 2

5 5
ezzzz
zkzzz
ezuzs
zzznz
zzzzs

出力例 2

5 5
4 4
3 3
2 2
1 1

(A_1,A_2,A_3,A_4,A_5)=((5,5),(4,4),(3,3),(2,2),(1,1)) が条件をみたしています。
例えば、(A_1,A_2,A_3,A_4,A_5)=((3,5),(4,4),(3,3),(2,2),(3,1)) は、1,2 つめの条件をみたしていますが、
マスの中心が一直線上に存在しないため、3 つめの条件をみたしていません。


入力例 3

10 10
kseeusenuk
usesenesnn
kskekeeses
nesnusnkkn
snenuuenke
kukknkeuss
neunnennue
sknuessuku
nksneekknk
neeeuknenk

出力例 3

9 3
8 3
7 3
6 3
5 3

Score : 250 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns. Each cell has a lowercase English letter written on it. We denote by (i, j) the cell at the i-th row from the top and j-th column from the left.

The letters written on the grid are represented by H strings S_1,S_2,\ldots, S_H, each of length W. The j-th letter of S_i represents the letter written on (i, j).

There is a unique set of contiguous cells (going vertically, horizontally, or diagonally) in the grid with s, n, u, k, and e written on them in this order.
Find the positions of such cells and print them in the format specified in the Output section.

A tuple of five cells (A_1,A_2,A_3,A_4,A_5) is said to form a set of contiguous cells (going vertically, horizontally, or diagonally) with s, n, u, k, and e written on them in this order if and only if all of the following conditions are satisfied.

  • A_1,A_2,A_3,A_4 and A_5 have letters s, n, u, k, and e written on them, respectively.
  • For all 1\leq i\leq 4, cells A_i and A_{i+1} share a corner or a side.
  • The centers of A_1,A_2,A_3,A_4, and A_5 are on a common line at regular intervals.

Constraints

  • 5\leq H\leq 100
  • 5\leq W\leq 100
  • H and W are integers.
  • S_i is a string of length W consisting of lowercase English letters.
  • The given grid has a unique conforming set of cells.

Input

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

H W
S_1
S_2
\vdots
S_H

Output

Print five lines in the following format.

Let (R_1,C_1), (R_2,C_2)\ldots,(R_5,C_5) be the cells in the sought set with s, n, u, k, and e written on them, respectively. The i-th line should contain R_i and C_i in this order, separated by a space.

In other words, print them in the following format:

R_1 C_1
R_2 C_2
\vdots
R_5 C_5

See also Sample Inputs and Outputs below.


Sample Input 1

6 6
vgxgpu
amkxks
zhkbpp
hykink
esnuke
zplvfj

Sample Output 1

5 2
5 3
5 4
5 5
5 6

Tuple (A_1,A_2,A_3,A_4,A_5)=((5,2),(5,3),(5,4),(5,5),(5,6)) satisfies the conditions.
Indeed, the letters written on them are s, n, u, k, and e;
for all 1\leq i\leq 4, cells A_i and A_{i+1} share a side;
and the centers of the cells are on a common line.


Sample Input 2

5 5
ezzzz
zkzzz
ezuzs
zzznz
zzzzs

Sample Output 2

5 5
4 4
3 3
2 2
1 1

Tuple (A_1,A_2,A_3,A_4,A_5)=((5,5),(4,4),(3,3),(2,2),(1,1)) satisfies the conditions.
However, for example, (A_1,A_2,A_3,A_4,A_5)=((3,5),(4,4),(3,3),(2,2),(3,1)) violates the third condition because the centers of the cells are not on a common line, although it satisfies the first and second conditions.


Sample Input 3

10 10
kseeusenuk
usesenesnn
kskekeeses
nesnusnkkn
snenuuenke
kukknkeuss
neunnennue
sknuessuku
nksneekknk
neeeuknenk

Sample Output 3

9 3
8 3
7 3
6 3
5 3
E - Inc, Dec, Xor

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

長さ N の整数列 A=(A_1,A_2,\ldots,A_N) があります。はじめ、A の要素は全て 0 です。

Q 個のクエリが与えられるので、順に処理してください。クエリは 2 種類あり、以下のいずれかの形式で与えられます。

  • 1 x:A_x の値を 1 増やす。
  • 2:i=1,2,\ldots,N に対し、A_i \geq 1 ならば A_i の値を 1 減らす。

各クエリを処理した直後の A_1,A_2,\ldots,A_N のビット単位 \mathrm{XOR} を求めてください。

ビット単位 \mathrm{XOR} 演算とは

非負整数 A, B のビット単位 \mathrm{XOR} 、A \oplus B は、以下のように定義されます。

  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。
一般に k 個の非負整数 p_1, p_2, p_3, \dots, p_k のビット単位 \mathrm{XOR} は (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k) と定義され、これは p_1, p_2, p_3, \dots, p_k の順番によらないことが証明できます。

制約

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

入力

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

N Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

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

1 x
2

出力

Q 行出力せよ。

i 行目 (1\le i\le Q) には、i 番目のクエリを処理した直後の A に対する A_1,A_2,\ldots,A_N のビット単位 \mathrm{XOR} を出力せよ。


入力例 1

2 5
1 2
1 2
1 1
2
2

出力例 1

1
2
3
1
0

1 番目のクエリを処理した後 A=(0,1) となります。0,1 のビット単位 \mathrm{XOR} は 1 なので、1 行目には 1 を出力してください。

2 番目のクエリを処理した後 A=(0,2) となります。0,2 のビット単位 \mathrm{XOR} は 2 なので、2 行目には 2 を出力してください。

3 番目のクエリを処理した後 A=(1,2) となります。1,2 のビット単位 \mathrm{XOR} は 3 なので、3 行目には 3 を出力してください。

4 番目のクエリを処理した後 A=(0,1) となります。0,1 のビット単位 \mathrm{XOR} は 1 なので、4 行目には 1 を出力してください。

5 番目のクエリを処理した後 A=(0,0) となります。0,0 のビット単位 \mathrm{XOR} は 0 なので、5 行目には 0 を出力してください。


入力例 2

3 8
1 2
1 3
1 1
1 2
1 1
2
1 3
1 1

出力例 2

1
0
1
2
1
0
1
2

Score : 300 points

Problem Statement

There is a length-N integer sequence A=(A_1,A_2,\ldots,A_N). Initially, all elements of A are 0.

You will be given Q queries, which should be processed in order. There are two types of queries, each given in one of the following formats:

  • 1 x: Increase the value of A_x by 1.
  • 2: For each i=1,2,\ldots,N, if A_i \geq 1, decrease the value of A_i by 1.

Find the bitwise \mathrm{XOR} of A_1,A_2,\ldots,A_N immediately after processing each query.

What is bitwise \mathrm{XOR}?

The bitwise \mathrm{XOR} of non-negative integers A and B, denoted A \oplus B, is defined as follows:

  • In the binary representation of A \oplus B, the digit in the 2^k (k \geq 0) place is 1 if exactly one of the digits in the 2^k place of A and B in their binary representations is 1, and 0 otherwise.
For example, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110).
More generally, the bitwise \mathrm{XOR} of k non-negative integers p_1, p_2, p_3, \dots, p_k is defined as (\dots ((p_1 \oplus p_2) \oplus p_3) \oplus \dots \oplus p_k), and it can be proved that this value does not depend on the order of p_1, p_2, p_3, \dots, p_k.

Constraints

  • 1\le N\le 5\times 10^5
  • 1\le Q\le 5\times 10^5
  • 1\le x\le N
  • All input values are integers.

Input

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

N Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in one of the following 2 formats:

1 x
2

Output

Output Q lines.

The i-th line (1\le i\le Q) should contain the bitwise \mathrm{XOR} of A_1,A_2,\ldots,A_N for A immediately after processing the i-th query.


Sample Input 1

2 5
1 2
1 2
1 1
2
2

Sample Output 1

1
2
3
1
0

After processing the first query, A=(0,1). The bitwise \mathrm{XOR} of 0,1 is 1, so output 1 on the first line.

After processing the second query, A=(0,2). The bitwise \mathrm{XOR} of 0,2 is 2, so output 2 on the second line.

After processing the third query, A=(1,2). The bitwise \mathrm{XOR} of 1,2 is 3, so output 3 on the third line.

After processing the fourth query, A=(0,1). The bitwise \mathrm{XOR} of 0,1 is 1, so output 1 on the fourth line.

After processing the fifth query, A=(0,0). The bitwise \mathrm{XOR} of 0,0 is 0, so output 0 on the fifth line.


Sample Input 2

3 8
1 2
1 3
1 1
1 2
1 1
2
1 3
1 1

Sample Output 2

1
0
1
2
1
0
1
2
F - Many Balls

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

空の箱があります。
髙橋君は以下の 2 種類の魔法を好きな順番で好きな回数使えます。

  • 魔法 A :箱の中にボールを 1 つ増やす
  • 魔法 B :箱の中のボールの数を 2 倍にする

合計 \mathbf{120} 回以内の魔法で、箱の中のボールの数をちょうど N 個にする方法を 1 つ教えてください。
なお、与えられた制約のもとで条件を満たす方法が必ず存在することが示せます。

魔法以外の方法でボールの数を変化させることはできません。

制約

  • 1 \leq N \leq 10^{18}
  • 入力は全て整数

入力

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

N

出力

A , B のみからなる文字列 S を出力せよ。
S の i 文字目が A ならば、髙橋君が i 回目に使う魔法が魔法 A であることを表し、B ならば魔法 B であることを表す。

S の長さは \mathbf{120} 以下でなければならない。


入力例 1

5

出力例 1

AABA

ボールの数は、0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5 と変化します。
AAAAA などの答えも正解になります。


入力例 2

14

出力例 2

BBABBAAAB

ボールの数は、0 \xrightarrow{B} 0 \xrightarrow{B} 0 \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4 \xrightarrow{A}5 \xrightarrow{A}6 \xrightarrow{A} 7 \xrightarrow{B}14 と変化します。
S の長さを最小化する必要はありません。

Score : 300 points

Problem Statement

We have an empty box.
Takahashi can cast the following two spells any number of times in any order.

  • Spell A: puts one new ball into the box.
  • Spell B: doubles the number of balls in the box.

Tell us a way to have exactly N balls in the box with at most \mathbf{120} casts of spells.
It can be proved that there always exists such a way under the Constraints given.

There is no way other than spells to alter the number of balls in the box.

Constraints

  • 1 \leq N \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print a string S consisting of A and B. The i-th character of S should represent the spell for the i-th cast.

S must have at most \mathbf{120} characters.


Sample Input 1

5

Sample Output 1

AABA

This changes the number of balls as follows: 0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5.
There are also other acceptable outputs, such as AAAAA.


Sample Input 2

14

Sample Output 2

BBABBAAAB

This changes the number of balls as follows: 0 \xrightarrow{B} 0 \xrightarrow{B} 0 \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4 \xrightarrow{A}5 \xrightarrow{A}6 \xrightarrow{A} 7 \xrightarrow{B}14.
It is not required to minimize the length of S.

G - Tail of Snake

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

すぬけ君は蛇を観察しており、どこからどこまでが頭・胴・尾なのかが気になっています。 すぬけ君は蛇を N 個のブロックに区切り、それぞれのブロックについて頭らしさ・胴らしさ・尾らしさを評価しました。 そして、らしさの合計が最大になる分け方を考えることにしました。

長さ N の整数列 A = (A_1, A_2, \ldots, A_N), B = (B_1, B_2, \ldots, B_N), C = (C_1, C_2, \ldots, C_N) が与えられます。

整数の組 (x, y) が 1 \leq x < y < N を満たすとき、\displaystyle\sum_{i = 1}^{x} A_i + \sum_{i = x + 1}^{y} B_i + \sum_{i = y + 1}^{N} C_i の値として取り得る最大値を求めてください。

制約

  • 3 \leq N \leq 3 \times 10^5
  • 1 \leq A_i, B_i, C_i \leq 10^6
  • 入力される値はすべて整数

入力

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N
C_1 C_2 \ldots C_N

出力

答えを出力せよ。


入力例 1

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

出力例 1

16

(x, y) = (2, 3) とすると \displaystyle\sum_{i = 1}^{x} A_i + \sum_{i = x + 1}^{y} B_i + \sum_{i = y + 1}^{N} C_i = 1 + 4 + 4 + 4 + 3 = 16 となります。


入力例 2

3
1 1 1
1 1 1
1 1 1

出力例 2

3

入力例 3

6
2 10 7 7 7 11
5 7 9 10 9 12
6 6 7 10 12 7

出力例 3

50

Score : 400 points

Problem Statement

Snuke is observing a snake and is curious about which parts are the head, body, and tail. He divided the snake into N blocks and evaluated the head-likeness, body-likeness, and tail-likeness of each block. Then, he decided to find the division that maximizes the sum of the likeness values.

You are given length-N integer sequences A = (A_1, A_2, \ldots, A_N), B = (B_1, B_2, \ldots, B_N), and C = (C_1, C_2, \ldots, C_N).

Find the maximum possible value of \displaystyle\sum_{i = 1}^{x} A_i + \sum_{i = x + 1}^{y} B_i + \sum_{i = y + 1}^{N} C_i for a pair of integers (x, y) satisfying 1 \leq x < y < N.

Constraints

  • 3 \leq N \leq 3 \times 10^5
  • 1 \leq A_i, B_i, C_i \leq 10^6
  • 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
B_1 B_2 \ldots B_N
C_1 C_2 \ldots C_N

Output

Output the answer.


Sample Input 1

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

Sample Output 1

16

With (x, y) = (2, 3), we have \displaystyle\sum_{i = 1}^{x} A_i + \sum_{i = x + 1}^{y} B_i + \sum_{i = y + 1}^{N} C_i = 1 + 4 + 4 + 4 + 3 = 16.


Sample Input 2

3
1 1 1
1 1 1
1 1 1

Sample Output 2

3

Sample Input 3

6
2 10 7 7 7 11
5 7 9 10 9 12
6 6 7 10 12 7

Sample Output 3

50
H - Arithmetic Number

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

以下の条件を満たす正の整数 n を、 等差数 と呼びます。

  • (n を先頭に余計な 0 を付けずに 10 進法で表記した際、) n の上から i 桁目を d_i とする。このとき、 n が k 桁の整数であったとすると、 (d_2-d_1)=(d_3-d_2)=\dots=(d_k-d_{k-1}) が成立する。
    • この条件は、「 数列 (d_1,d_2,\dots,d_k) が等差数列である」と言い換えることができる。
    • 但し、 n が 1 桁の整数である時、 n は等差数であるものとする。

たとえば、 234,369,86420,17,95,8,11,777 は等差数ですが、 751,919,2022,246810,2356 は等差数ではありません。

等差数のうち、 X 以上で最小のものを求めてください。

制約

  • X は 1 以上 10^{17} 以下の整数である

入力

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

X

出力

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


入力例 1

152

出力例 1

159

152 以上で最小の等差数は 159 です。


入力例 2

88

出力例 2

88

X 自身が等差数である場合もあります。


入力例 3

8989898989

出力例 3

9876543210

Score : 500 points

Problem Statement

Let us call a positive integer n that satisfies the following condition an arithmetic number.

  • Let d_i be the i-th digit of n from the top (when n is written in base 10 without unnecessary leading zeros.) Then, (d_2-d_1)=(d_3-d_2)=\dots=(d_k-d_{k-1}) holds, where k is the number of digits in n.
    • This condition can be rephrased into the sequence (d_1,d_2,\dots,d_k) being arithmetic.
    • If n is a 1-digit integer, it is assumed to be an arithmetic number.

For example, 234,369,86420,17,95,8,11,777 are arithmetic numbers, while 751,919,2022,246810,2356 are not.

Find the smallest arithmetic number not less than X.

Constraints

  • X is an integer between 1 and 10^{17} (inclusive).

Input

Input is given from Standard Input in the following format:

X

Output

Print the answer as an integer.


Sample Input 1

152

Sample Output 1

159

The smallest arithmetic number not less than 152 is 159.


Sample Input 2

88

Sample Output 2

88

X itself may be an arithmetic number.


Sample Input 3

8989898989

Sample Output 3

9876543210
I - Lottery

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

高橋君はくじを引こうとしています。

くじを 1 回引くごとに、N 種類の賞品のいずれかが手に入ります。賞品 i が手に入る確率は \frac{W_i}{\sum_{j=1}^{N}W_j} であり、各くじの結果は独立です。

くじを K 回引いたとき、ちょうど M 種類の賞品が手に入る確率はいくらでしょうか? \bmod 998244353 で求めてください。

注記

有理数を出力する際は、まずその有理数を分数 \frac{y}{x} として表してください。 ここで、x,y は整数であり、x は 998244353 で割り切れてはなりません(この問題の制約下で、そのような表現は必ず可能です)。 そして、xz\equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の唯一の整数 z を出力してください。

制約

  • 1 \leq K \leq 50
  • 1 \leq M \leq N \leq 50
  • 0 < W_i
  • 0 < W_1 + \ldots + W_N < 998244353
  • 入力は全て整数である

入力

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

N M K
W_1
\vdots
W_N

出力

答えを出力せよ。


入力例 1

2 1 2
2
1

出力例 1

221832079

各くじの結果として、賞品 1 が手に入る確率が \frac{2}{3}、賞品 2 が手に入る確率が \frac{1}{3} です。

2 回のくじの結果として、ともに賞品 1 を手に入れる確率が \frac{4}{9}、ともに賞品 2 を手に入れる確率が \frac{1}{9} であるため、求める答えは \frac{5}{9} です。

これを注記にしたがって \bmod 998244353 で出力すると 221832079 になります。


入力例 2

3 3 2
1
1
1

出力例 2

0

くじを 2 回引いて 3 種類の賞品を手に入れることはできません。したがって求める確率は 0 です。


入力例 3

3 3 10
499122176
499122175
1

出力例 3

335346748

入力例 4

10 8 15
1
1
1
1
1
1
1
1
1
1

出力例 4

755239064

Score : 500 points

Problem Statement

Takahashi is participating in a lottery.

Each time he takes a draw, he gets one of the N prizes available. Prize i is awarded with probability \frac{W_i}{\sum_{j=1}^{N}W_j}. The results of the draws are independent of each other.

What is the probability that he gets exactly M different prizes from K draws? Find it modulo 998244353.

Note

To print a rational number, start by representing it as a fraction \frac{y}{x}. Here, x and y should be integers, and x should not be divisible by 998244353 (under the Constraints of this problem, such a representation is always possible). Then, print the only integer z between 0 and 998244352 (inclusive) such that xz\equiv y \pmod{998244353}.

Constraints

  • 1 \leq K \leq 50
  • 1 \leq M \leq N \leq 50
  • 0 < W_i
  • 0 < W_1 + \ldots + W_N < 998244353
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
W_1
\vdots
W_N

Output

Print the answer.


Sample Input 1

2 1 2
2
1

Sample Output 1

221832079

Each draw awards Prize 1 with probability \frac{2}{3} and Prize 2 with probability \frac{1}{3}.

He gets Prize 1 at both of the two draws with probability \frac{4}{9}, and Prize 2 at both draws with probability \frac{1}{9}, so the sought probability is \frac{5}{9}.

The modulo 998244353 representation of this value, according to Note, is 221832079.


Sample Input 2

3 3 2
1
1
1

Sample Output 2

0

It is impossible to get three different prizes from two draws, so the sought probability is 0.


Sample Input 3

3 3 10
499122176
499122175
1

Sample Output 3

335346748

Sample Input 4

10 8 15
1
1
1
1
1
1
1
1
1
1

Sample Output 4

755239064