A - Water Station

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

全長 100\;\mathrm{km} のマラソンコースがあります。 マラソンコースにはスタートから 5\;\mathrm{km} おきに給水所が設置されており、スタート地点・ゴール地点とあわせて 21 箇所の給水所があります。

高橋くんは、このマラソンコースの N\;\mathrm{km} 地点にいます。 高橋くんに最も近い給水所は何 \mathrm{km} 地点の給水所か求めてください。

この問題の制約のもとで、最も近い給水所が 1 つに定まることが証明できます。

制約

  • 0\leq N\leq100
  • N は整数

入力

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

N

出力

高橋くんがいる地点に最も近い給水所が何 \mathrm{km} 地点にあるかを 1 行で出力せよ。


入力例 1

53

出力例 1

55

高橋くんは、マラソンコースの 53\;\mathrm{km} 地点にいます。 55\;\mathrm{km} 地点の給水所までの距離は 2\;\mathrm{km} で、これより近い給水所はありません。 よって、55 を出力してください。


入力例 2

21

出力例 2

20

高橋くんはマラソンコースを戻ることもできます。


入力例 3

100

出力例 3

100

給水所はスタートやゴールにもあります。 また、高橋くんはすでに給水所にいることもあります。

Score : 100 points

Problem Statement

There is an ultramarathon course totaling 100\;\mathrm{km}. Water stations are set up every 5\;\mathrm{km} along the course, including the start and goal, for a total of 21.

Takahashi is at the N\;\mathrm{km} point of this course. Find the position of the nearest water station to him.

Under the constraints of this problem, it can be proven that the nearest water station is uniquely determined.

Constraints

  • 0\leq N\leq100
  • N is an integer.

Input

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

N

Output

Print the distance between the start and the water station nearest to Takahashi, in kilometers, in a single line.


Sample Input 1

53

Sample Output 1

55

Takahashi is at the 53\;\mathrm{km} point of the course. The water station at the 55\;\mathrm{km} point is 2\;\mathrm{km} away, and there is no closer water station. Therefore, you should print 55.


Sample Input 2

21

Sample Output 2

20

Takahashi could also go back the way.


Sample Input 3

100

Sample Output 3

100

There are also water stations at the start and goal. Additionally, Takahashi may already be at a water station.

B - Adjacent Product

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

N 個の整数 A_1,A_2,\dots,A_N が与えられます。 また、B_i=A_i\times A_{i+1}\ (1\leq i\leq N-1) と定めます。

B_1,B_2,\dots,B_{N-1} をこの順に空白区切りで出力してください。

制約

  • 2\leq N \leq 100
  • 1\leq A_i \leq 100
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

B_1,B_2,\dots,B_{N-1} をこの順に空白区切りで出力せよ。


入力例 1

3
3 4 6

出力例 1

12 24

B_1=A_1\times A_2 = 12, B_2=A_2\times A_3 = 24 です。


入力例 2

5
22 75 26 45 72

出力例 2

1650 1950 1170 3240

Score: 100 points

Problem Statement

You are given N integers A_1, A_2, \dots, A_N. Also, define B_i = A_i \times A_{i+1}\ (1 \leq i \leq N-1).

Print B_1, B_2, \dots, B_{N-1} in this order, separated by spaces.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq A_i \leq 100
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_N

Output

Print B_1, B_2, \dots, B_{N-1} in this order, separated by spaces.


Sample Input 1

3
3 4 6

Sample Output 1

12 24

We have B_1 = A_1 \times A_2 = 12, B_2 = A_2 \times A_3 = 24.


Sample Input 2

5
22 75 26 45 72

Sample Output 2

1650 1950 1170 3240
C - 3-smooth Numbers

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

正の整数 N が与えられます。 N=2^x3^y を満たす整数 x,y が存在するなら Yes 、そうでなければ No と出力してください。

制約

  • 1\leq N\leq10^{18}
  • N は整数

入力

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

N

出力

条件を満たす整数 x,y が存在するなら Yes 、そうでなければ No1 行に出力せよ。


入力例 1

324

出力例 1

Yes

x=2,y=4 とすると、2^x3^y=2^23^4=4\times81=324 となるため条件を満たします。 よって、Yes と出力してください。


入力例 2

5

出力例 2

No

どのような整数 x,y をとっても 2^x3^y=5 とすることはできません。 よって、No と出力してください。


入力例 3

32

出力例 3

Yes

x=5,y=0 とすると、2^x3^y=32\times1=32 となるため、Yes と出力してください。


入力例 4

37748736

出力例 4

Yes

Score : 200 points

Problem Statement

You are given a positive integer N. If there are integers x and y such that N=2^x3^y, print Yes; otherwise, print No.

Constraints

  • 1\leq N\leq10^{18}
  • N is an integer.

Input

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

N

Output

Print a single line containing Yes if there are integers x and y that satisfy the condition, and No otherwise.


Sample Input 1

324

Sample Output 1

Yes

For x=2,y=4, we have 2^x3^y=2^23^4=4\times81=324, so the condition is satisfied. Thus, you should print Yes.


Sample Input 2

5

Sample Output 2

No

There are no integers x and y such that 2^x3^y=5. Thus, you should print No.


Sample Input 3

32

Sample Output 3

Yes

For x=5,y=0, we have 2^x3^y=32\times1=32, so you should print Yes.


Sample Input 4

37748736

Sample Output 4

Yes
D - Maintain Multiple Sequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

整数からなる数列が N 個あります。
i \, (1 \leq i \leq N) 番目の数列は L_i 項からなり、i 番目の数列の第 j \, (1 \leq j \leq L_i) 項 は a_{i, j} です。

Q 個のクエリが与えられます。k \, (1 \leq k \leq Q) 番目のクエリでは、整数 s_k, t_k が与えられるので、s_k 番目の数列の第 t_k 項を求めてください。

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • L_i \geq 1 \, (1 \leq i \leq N)
  • \sum_{i=1}^N L_i \leq 2 \times 10^5
  • 1 \leq a_{i, j} \leq 10^9 \, (1 \leq i \leq N, 1 \leq j \leq L_i)
  • 1 \leq s_k \leq N, 1 \leq t_k \leq L_{s_k} \, (1 \leq k \leq Q)
  • 入力は全て整数

入力

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

N Q
L_1 a_{1, 1} \ldots a_{1, L_1}
\vdots
L_N a_{N, 1} \ldots a_{N, L_N}
s_1 t_1
\vdots 
s_Q t_Q

出力

Q 行出力せよ。k \, (1 \leq k \leq Q) 行目には、k 番目のクエリに対する答えを出力せよ。


入力例 1

2 2
3 1 4 7
2 5 9
1 3
2 1

出力例 1

7
5

1 番目の数列は (1, 4, 7)2 番目の数列は (5, 9) です。
それぞれのクエリに対する答えは次のようになります。

  • 1 番目の数列の第 3 項は 7 です。
  • 2 番目の数列の第 1 項は 5 です。

入力例 2

3 4
4 128 741 239 901
2 1 1
3 314 159 26535
1 1
2 2
3 3
1 4

出力例 2

128
1
26535
901

Score : 200 points

Problem Statement

There are N sequences of integers.
The i-th (1 \leq i \leq N) sequence has L_i terms; the j-th (1 \leq j \leq L_i) term of the i-th sequence is a_{i, j}.

You are given Q queries. For the k-th (1 \leq k \leq Q) query, given integers s_k and t_k, find the t_k-th term of the s_k-th sequence.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • L_i \geq 1 \, (1 \leq i \leq N)
  • \sum_{i=1}^N L_i \leq 2 \times 10^5
  • 1 \leq a_{i, j} \leq 10^9 \, (1 \leq i \leq N, 1 \leq j \leq L_i)
  • 1 \leq s_k \leq N, 1 \leq t_k \leq L_{s_k} \, (1 \leq k \leq Q)
  • All values in the input are integers.

Input

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

N Q
L_1 a_{1, 1} \ldots a_{1, L_1}
\vdots
L_N a_{N, 1} \ldots a_{N, L_N}
s_1 t_1
\vdots 
s_Q t_Q

Output

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


Sample Input 1

2 2
3 1 4 7
2 5 9
1 3
2 1

Sample Output 1

7
5

The 1-st sequence is (1, 4, 7) and the 2-nd is (5, 9).
The answer to each query is as follows:

  • The 3-rd term of the 1-st sequence is 7.
  • The 1-st term of the 2-nd sequence is 5.

Sample Input 2

3 4
4 128 741 239 901
2 1 1
3 314 159 26535
1 1
2 2
3 3
1 4

Sample Output 2

128
1
26535
901
E - Loong Tracking

Time Limit: 2 sec / Memory Limit: 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 - Avoid K Palindrome 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

英小文字のみからなる長さ N の文字列 S が与えられます。

S の文字を並び替えて得られる文字列(S 自身を含む)であって、長さ K の回文を部分文字列として 含まない ものの個数を求めてください。

ただし、長さ N の文字列 T が「長さ K の回文を部分文字列として含む」とは、
ある (N-K) 以下の非負整数 i が存在して、1 以上 K 以下の任意の整数 j について T_{i+j}=T_{i+K+1-j} が成り立つことをいいます。
ここで、T_k は文字列 Tk 文字目を表すものとします。

制約

  • 2\leq K \leq N \leq 10
  • N,K は整数
  • S は英小文字のみからなる長さ N の文字列

入力

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

N K
S

出力

S の文字を並び替えて得られる文字列であって、長さ K の回文を部分文字列として含まないものの個数を出力せよ。


入力例 1

3 2
aab

出力例 1

1

aab を並び替えて得られる文字列は aab, aba, baa3 つであり、このうち aab および baa は長さ 2 の回文 aa を部分文字列として含んでいます。
よって、条件をみたす文字列は aba のみであり、1 を出力します。


入力例 2

5 3
zzyyx

出力例 2

16

zzyyx を並べて得られる文字列は 30 個ありますが、そのうち長さ 3 の回文を含まないようなものは 16 個です。よって、16 を出力します。


入力例 3

10 5
abcwxyzyxw

出力例 3

440640

Score : 300 points

Problem Statement

You are given a string S of length N consisting only of lowercase English letters.

Find the number of strings obtained by permuting the characters of S (including the string S itself) that do not contain a palindrome of length K as a substring.

Here, a string T of length N is said to "contain a palindrome of length K as a substring" if and only if there exists a non-negative integer i not greater than (N-K) such that T_{i+j} = T_{i+K+1-j} for every integer j with 1 \leq j \leq K.
Here, T_k denotes the k-th character of the string T.

Constraints

  • 2 \leq K \leq N \leq 10
  • N and K are integers.
  • S is a string of length N consisting only of lowercase English letters.

Input

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

N K
S

Output

Print the number of strings obtained by permuting S that do not contain a palindrome of length K as a substring.


Sample Input 1

3 2
aab

Sample Output 1

1

The strings obtained by permuting aab are aab, aba, and baa. Among these, aab and baa contain the palindrome aa of length 2 as a substring.
Thus, the only string that satisfies the condition is aba, so print 1.


Sample Input 2

5 3
zzyyx

Sample Output 2

16

There are 30 strings obtained by permuting zzyyx, 16 of which do not contain a palindrome of length 3. Thus, print 16.


Sample Input 3

10 5
abcwxyzyxw

Sample Output 3

440640
G - Doubles

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

N 個のサイコロがあります。 i 番目のサイコロは K_i 個の面をもち、各面にはそれぞれ A_{i,1},A_{i,2},\ldots,A_{i,K_i} が書かれています。このサイコロを振ると、各面がそれぞれ \frac{1}{K_i} の確率で出ます。

あなたは N 個のサイコロから 2 個を選んで振ります。サイコロを適切に選んだときの、2 つのサイコロの出目が等しくなる確率の最大値を求めてください。

制約

  • 2 \leq N \leq 100
  • 1 \leq K_i
  • K_1+K_2+\dots+K_N \leq 10^5
  • 1 \leq A_{i,j} \leq 10^5
  • 入力は全て整数である

入力

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

N
K_1 A_{1,1} A_{1,2} \dots A_{1,K_1}
\vdots
K_N A_{N,1} A_{N,2} \dots A_{N,K_N}

出力

答えを出力せよ。真の解との相対誤差または絶対誤差が 10^{-8} 以下のとき正解とみなされる。


入力例 1

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

出力例 1

0.333333333333333
  • 1 番目のサイコロと 2 番目のサイコロを選んで振ったとき、出目が等しくなる確率は \frac{1}{3} です。
  • 1 番目のサイコロと 3 番目のサイコロを選んで振ったとき、出目が等しくなる確率は \frac{1}{6} です。
  • 2 番目のサイコロと 3 番目のサイコロを選んで振ったとき、出目が等しくなる確率は \frac{1}{6} です。

よって最大値は \frac{1}{3}=0.3333333333\ldots となります。


入力例 2

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

出力例 2

0.666666666666667

Score : 400 points

Problem Statement

There are N dice. The i-th die has K_i faces, with the numbers A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} written on them. When you roll this die, each face appears with probability \frac{1}{K_i}.

You choose two dice from the N dice and roll them. Determine the maximum probability that the two dice show the same number, when the dice are chosen optimally.

Constraints

  • 2 \leq N \leq 100
  • 1 \leq K_i
  • K_1 + K_2 + \dots + K_N \leq 10^5
  • 1 \leq A_{i,j} \leq 10^5
  • All input values are integers.

Input

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

N
K_1 A_{1,1} A_{1,2} \dots A_{1,K_1}
\vdots
K_N A_{N,1} A_{N,2} \dots A_{N,K_N}

Output

Print the answer. Your answer is considered correct if the absolute or relative error from the true solution does not exceed 10^{-8}.


Sample Input 1

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

Sample Output 1

0.333333333333333
  • When choosing the 1st and 2nd dice, the probability that the outcomes are the same is \frac{1}{3}.
  • When choosing the 1st and 3rd dice, the probability is \frac{1}{6}.
  • When choosing the 2nd and 3rd dice, the probability is \frac{1}{6}.

Therefore, the maximum probability is \frac{1}{3} = 0.3333333333\ldots.


Sample Input 2

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

Sample Output 2

0.666666666666667
H - Add and Mex

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

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

以下の操作を M 回行ってください。

  • i\ (1\leq i \leq N) について、 A_ii を加算する。その後 A に含まれない最小の非負整数を求める。

制約

  • 1\leq N,M \leq 2\times 10^5
  • -10^9\leq A_i\leq 10^9
  • 入力は全て整数

入力

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

N M 
A_1 A_2 \ldots A_N

出力

M 行出力せよ。

i (1\leq i \leq M) 行目には i 回目の操作後に A に含まれない最小の非負整数を出力せよ。


入力例 1

3 3
-1 -1 -6

出力例 1

2
2
0

1 回目の操作では、数列 A

(-1 + 1, -1 +2 ,-6+3) = (0,1,-3)

になります。 A に含まれない最小の非負整数は 2 です。

2 回目の操作では、数列 A

(0 + 1, 1 +2 ,-3+3) = (1,3,0)

になります。 A に含まれない最小の非負整数は 2 です。

3 回目の操作では、数列 A

(1 + 1, 3 +2 ,0+3) = (2,5,3)

になります。 A に含まれない最小の非負整数は 0 です。


入力例 2

5 6
-2 -2 -5 -7 -15

出力例 2

1
3
2
0
0
0

Score : 500 points

Problem Statement

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

Perform the following operation M times:

  • For each i\ (1\leq i \leq N), add i to A_i. Then, find the minimum non-negative integer not contained in A.

Constraints

  • 1\leq N,M \leq 2\times 10^5
  • -10^9\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 M 
A_1 A_2 \ldots A_N

Output

Print M lines.

The i-th (1\leq i \leq M) line should contain the minimum non-negative integer not contained in A after the i-th operation.


Sample Input 1

3 3
-1 -1 -6

Sample Output 1

2
2
0

The 1-st operation makes the sequence A

(-1 + 1, -1 +2 ,-6+3) = (0,1,-3).

The minimum non-negative integer not contained in A is 2.

The 2-nd operation makes the sequence A

(0 + 1, 1 +2 ,-3+3) = (1,3,0).

The minimum non-negative integer not contained in A is 2.

The 3-rd operation makes the sequence A

(1 + 1, 3 +2 ,0+3) = (2,5,3).

The minimum non-negative integer not contained in A is 0.


Sample Input 2

5 6
-2 -2 -5 -7 -15

Sample Output 2

1
3
2
0
0
0
I - Numbered Checker

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

NM 列のグリッドがあり、上から i 行目、左から j 列目のマス (i,j) には整数 (i-1) \times M + j が書かれています。
このグリッドに、以下の操作を施します。

  • 全てのマス (i,j) について、 i+j が奇数ならそのマスに書かれている数字を 0 に書き換える。

操作後のグリッドについて、Q 個の質問に答えてください。
i 個目の質問は以下の通りです:

  • 以下の条件を全て満たすマス (p,q) 全てについて、そこに書かれている整数を全て足し合わせた値を 998244353 で割った余りを求めよ。
    • A_i \le p \le B_i
    • C_i \le q \le D_i

制約

  • 入力は全て整数
  • 1 \le N,M \le 10^9
  • 1 \le Q \le 2 \times 10^5
  • 1 \le A_i \le B_i \le N
  • 1 \le C_i \le D_i \le M

入力

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

N M
Q
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
\vdots
A_Q B_Q C_Q D_Q

出力

Q 行出力せよ。
そのうち i 行目には、 i 個目の質問に対する答えを整数として出力せよ。


入力例 1

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

出力例 1

28
27
36
14
0
104

この入力では、グリッドは以下の通りです。

この入力には 6 つの質問が含まれます。

  • 1 個目の質問の答えは 0+3+0+6+0+8+0+11+0=28 です。
  • 2 個目の質問の答えは 1+0+9+0+17=27 です。
  • 3 個目の質問の答えは 17+0+19+0=36 です。
  • 4 個目の質問の答えは 14 です。
  • 5 個目の質問の答えは 0 です。
  • 6 個目の質問の答えは 104 です。

入力例 2

1000000000 1000000000
3
1000000000 1000000000 1000000000 1000000000
165997482 306594988 719483261 992306147
1 1000000000 1 1000000000

出力例 2

716070898
240994972
536839100

1 個目の質問について、マス (10^9,10^9) に書かれている整数は 10^{18} ですが、それを 998244353 で割った余りを求めることに注意してください。


入力例 3

999999999 999999999
3
999999999 999999999 999999999 999999999
216499784 840031647 84657913 415448790
1 999999999 1 999999999

出力例 3

712559605
648737448
540261130

Score : 500 points

Problem Statement

We have a grid with N rows and M columns. The square (i,j) at the i-th row from the top and j-th column from the left has an integer (i-1) \times M + j written on it.
Let us perform the following operation on this grid.

  • For every square (i,j) such that i+j is odd, replace the integer on that square with 0.

Answer Q questions on the grid after the operation.
The i-th question is as follows:

  • Find the sum of the integers written on all squares (p,q) that satisfy all of the following conditions, modulo 998244353.
    • A_i \le p \le B_i.
    • C_i \le q \le D_i.

Constraints

  • All values in the input are integers.
  • 1 \le N,M \le 10^9
  • 1 \le Q \le 2 \times 10^5
  • 1 \le A_i \le B_i \le N
  • 1 \le C_i \le D_i \le M

Input

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

N M
Q
A_1 B_1 C_1 D_1
A_2 B_2 C_2 D_2
\vdots
A_Q B_Q C_Q D_Q

Output

Print Q lines.
The i-th line should contain the answer to the i-th question as an integer.


Sample Input 1

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

Sample Output 1

28
27
36
14
0
104

The grid in this input is shown below.

This input contains six questions.

  • The answer to the first question is 0+3+0+6+0+8+0+11+0=28.
  • The answer to the second question is 1+0+9+0+17=27.
  • The answer to the third question is 17+0+19+0=36.
  • The answer to the fourth question is 14.
  • The answer to the fifth question is 0.
  • The answer to the sixth question is 104.

Sample Input 2

1000000000 1000000000
3
1000000000 1000000000 1000000000 1000000000
165997482 306594988 719483261 992306147
1 1000000000 1 1000000000

Sample Output 2

716070898
240994972
536839100

For the first question, note that although the integer written on the square (10^9,10^9) is 10^{18}, you are to find it modulo 998244353.


Sample Input 3

999999999 999999999
3
999999999 999999999 999999999 999999999
216499784 840031647 84657913 415448790
1 999999999 1 999999999

Sample Output 3

712559605
648737448
540261130