Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
高橋くんは満月が好きです。
今日を 1 日目とすると、今日以降で満月を見られる最初の日は M 日目です。以後は P 日ごと、つまり M+P 日目、M+2P 日目、\ldots に満月を見られます。
1 日目から N 日目まで(両端を含む)の中で、高橋くんが満月を見られる日の数を求めてください。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq M \leq P \leq 2\times 10^5
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M P
出力
答えを整数として出力せよ。
入力例 1
13 3 5
出力例 1
3
満月を見られる日は、3 日目、8 日目、13 日目、18 日目、\ldots です。
1 日目から 13 日目までの中で高橋くんが満月を見られる日は、3 日目、8 日目、13 日目の 3 個です。
入力例 2
5 6 6
出力例 2
0
高橋くんが満月を見られる日が存在しない場合もあります。
入力例 3
200000 314 318
出力例 3
628
Score : 100 points
Problem Statement
Takahashi likes full moons.
Let today be day 1. The first day on or after today on which he can see a full moon is day M. After that, he can see a full moon every P days, that is, on day M+P, day M+2P, and so on.
Find the number of days between day 1 and day N, inclusive, on which he can see a full moon.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq M \leq P \leq 2\times 10^5
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M P
Output
Print the answer as an integer.
Sample Input 1
13 3 5
Sample Output 1
3
He can see a full moon on day 3, 8, 13, 18, and so on.
From day 1 to 13, he can see a full moon on three days: day 3, 8, and 13.
Sample Input 2
5 6 6
Sample Output 2
0
There may be no days he can see a full moon.
Sample Input 3
200000 314 318
Sample Output 3
628
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字からなる文字列 S が与えられます。
S から a, e, i, o, u をすべて取り除いて得られる文字列を出力してください。
なお、S は a, e, i, o, u 以外の文字を一つ以上含みます。
制約
- S は英小文字からなる長さ 1 以上 100 以下の文字列
- S は
a,e,i,o,u以外の文字を一つ以上含む
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
atcoder
出力例 1
tcdr
S = atcoder のとき、1, 4, 6 文字目を取り除いて tcdr を得ます。
入力例 2
xyz
出力例 2
xyz
入力例 3
aaaabbbbcccc
出力例 3
bbbbcccc
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters.
Remove all occurrences of a, e, i, o, u from S and print the resulting string.
S contains at least one character other than a, e, i, o, u.
Constraints
- S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters.
- S contains at least one character other than
a,e,i,o,u.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
atcoder
Sample Output 1
tcdr
For S = atcoder, remove the 1-st, 4-th, and 6-th characters to get tcdr.
Sample Input 2
xyz
Sample Output 2
xyz
Sample Input 3
aaaabbbbcccc
Sample Output 3
bbbbcccc
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
人 1 、人 2 、\ldots 、人 N と番号をつけられた N 人の人がいます。
N 人は、人 1 、人 2 、\ldots 、人 N の順番に下記の行動をちょうど 1 回ずつ行います。
- 人 i 自身がまだ一度も番号を呼ばれていないなら、人 A_i の番号を呼ぶ。
最後まで番号を一度も呼ばれない人全員の番号を昇順に列挙してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq N
- A_i \neq i
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
下記の形式にしたがって、最後まで番号を一度も呼ばれない人全員の番号を昇順に列挙せよ。
K X_1 X_2 \ldots X_K
すなわち、まず 1 行目に、最後まで番号を一度も呼ばれない人の人数 K を出力し、 2 行目に、最後まで番号を一度も呼ばれない人全員の番号を昇順に並べた列 (X_1, X_2, \ldots, X_K) を空白区切りで出力せよ。
入力例 1
5 3 1 4 5 4
出力例 1
2 2 4
5 人の行動は下記の通りです。
- 人 1 はまだ番号を一度も呼ばれていないので、人 1 は人 3 の番号を呼びます。
- 人 2 はまだ番号を一度も呼ばれていないので、人 2 は人 1 の番号を呼びます。
- 人 3 はすでに人 1 によって番号を呼ばれているので、何もしません。
- 人 4 はまだ番号を一度も呼ばれていないので、人 4 は人 5 の番号を呼びます。
- 人 5 はすでに人 4 によって番号を呼ばれているので、何もしません。
よって、最後まで番号を一度も呼ばれないのは人 2 と人 4 です。
入力例 2
20 9 7 19 7 10 4 13 9 4 8 10 15 16 3 18 19 12 13 2 12
出力例 2
10 1 2 5 6 8 11 14 17 18 20
Score : 200 points
Problem Statement
There are N people whose IDs are 1, 2, \ldots, and N.
Each of person 1, person 2, \ldots, and person N performs the following action once in this order:
- If person i's ID has not been called out yet, call out person A_i's ID.
Enumerate the IDs of all the people whose IDs are never called out until the end in ascending order.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq N
- A_i \neq i
- 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
Enumerate the IDs of all the people whose IDs are not called out until the end in ascending order in the following format:
K X_1 X_2 \ldots X_K
In other words, the first line should contain the number of people, K, whose IDs are never called out until the end; the second line should contain the sequence (X_1, X_2, \ldots, X_K) of IDs of such people in ascending order, with spaces in between.
Sample Input 1
5 3 1 4 5 4
Sample Output 1
2 2 4
The five people's actions are as follows.
- Person 1's ID has not been called out yet, so person 1 calls out person 3's ID.
- Person 2's ID has not been called out yet, so person 2 calls out person 1's ID.
- Person 3's ID has already been called out by person 1, so nothing happens.
- Person 4's ID has not been called out yet, so person 4 calls out person 5's ID.
- Person 5's ID has already been called out by person 4, so nothing happens.
Therefore, person 2 and 4's IDs are not called out until the end.
Sample Input 2
20 9 7 19 7 10 4 13 9 4 8 10 15 16 3 18 19 12 13 2 12
Sample Output 2
10 1 2 5 6 8 11 14 17 18 20
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
文字列 S, T が与えられます。以下の操作を高々 1 回行うことで、S を T と一致させることができるかを判定してください。
- S の隣り合う 2 文字を選び、入れ替える。
なお、上記の操作を一度も行わないことも可能です。
制約
- S, T はそれぞれ英小文字のみからなる、長さ 2 以上 100 以下の文字列
- S の長さと T の長さは等しい
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
問題文中の操作を高々 1 回行うことで S を T と一致させることができるなら Yes を、できないなら No を出力せよ。
入力例 1
abc acb
出力例 1
Yes
S の 2 文字目と 3 文字目を入れ替えることで、S を T と一致させることができます。
入力例 2
aabb bbaa
出力例 2
No
どのように操作を行っても、S を T と一致させることはできません。
入力例 3
abcde abcde
出力例 3
Yes
S と T は既に一致しています。
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.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
あなたはアメーバの観察記録をつけました。
最初 1 匹のアメーバがおり、番号は 1 です。
観察記録は時系列順に N 個あり、i 番目の観察記録は「番号 A_i のアメーバが分裂して消滅し、新たに 2 匹のアメーバが生まれ、それらにそれぞれ 2i,2i+1 と番号をつけた」というものです。
このとき、アメーバ A_i を アメーバ 2i,2i+1 の親と呼びます。
各 k=1,\ldots,2N+1 について、アメーバ k から何代親を遡るとアメーバ 1 になるか求めてください。
制約
- 1 \leq N \leq 2\times 10^5
- 観察記録は矛盾していない。すなわち
- 1\leq A_i \leq 2i-1
- A_i は相異なる整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
2N+1 行出力せよ。k 行目にはアメーバ k から何代親を遡るとアメーバ 1 になるかを出力せよ。
入力例 1
2 1 2
出力例 1
0 1 1 2 2
アメーバ 1 からアメーバ 2,3 が生まれ、アメーバ 2 からアメーバ 4,5 が生まれました。
- アメーバ 1 は 0 代遡るとアメーバ 1 になります。
- アメーバ 2 は 1 代遡るとアメーバ 1 になります。
- アメーバ 3 は 1 代遡るとアメーバ 1 になります。
- アメーバ 4 は 1 代遡るとアメーバ 2 になり、2 代遡るとアメーバ 1 になります。
- アメーバ 5 は 1 代遡るとアメーバ 2 になり、2 代遡るとアメーバ 1 になります。
入力例 2
4 1 3 5 2
出力例 2
0 1 1 2 2 3 3 2 2
Score : 300 points
Problem Statement
You observed amoebae and kept some records.
Initially, there was one amoeba, numbered 1.
You made N records. According to the i-th record, the amoeba numbered A_i disappeared by dividing itself into two new amoebae, which were then numbered 2i and 2i+1.
Here, amoeba A_i is said to be the parent of amoebae 2i and 2i+1.
For each k=1,\ldots,2N+1, how many generations away is amoeba k from amoeba 1?
Constraints
- 1 \leq N \leq 2\times 10^5
- The records are consistent. That is:
- 1\leq A_i \leq 2i-1.
- A_i are distinct integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print 2N+1 lines. The k-th line should contain the generation distance between amoeba 1 and amoeba k.
Sample Input 1
2 1 2
Sample Output 1
0 1 1 2 2
From amoeba 1, amoebae 2 and 3 were born. From amoeba 2, amoebae 4 and 5 were born.
- Amoeba 1 is zero generations away from amoeba 1.
- Amoeba 2 is one generation away from amoeba 1.
- Amoeba 3 is one generation away from amoeba 1.
- Amoeba 4 is one generation away from amoeba 2, and two generations away from amoeba 1.
- Amoeba 5 is one generation away from amoeba 2, and two generations away from amoeba 1.
Sample Input 2
4 1 3 5 2
Sample Output 2
0 1 1 2 2 3 3 2 2
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
二次元平面上に高橋君がいます。高橋君は原点から移動を N 回行いました。
N 回の移動は長さ N の文字列で表され、意味は次の通りです。
- i 回目の高橋君の移動後の座標は、移動前の座標を (x,y) として、
- S の i 文字目が
Rであるとき (x+1,y) - S の i 文字目が
Lであるとき (x-1,y) - S の i 文字目が
Uであるとき (x,y+1) - S の i 文字目が
Dであるとき (x,y-1)
- S の i 文字目が
N 回の移動 (始点と終点を含む) で、高橋君が同じ座標にいたことがあるかどうかを判定してください。
制約
- 1 \leq N \leq 2\times 10^5
- N は整数
- S は
R,L,U,Dのみからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
N 回の移動 (始点と終点を含む) で、高橋君が同じ座標にいたことがあれば Yes、なければ No と出力せよ。
入力例 1
5 RLURU
出力例 1
Yes
高橋君のいる座標は (0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2) と変化します。
入力例 2
20 URDDLLUUURRRDDDDLLLL
出力例 2
No
Score : 300 points
Problem Statement
Takahashi is on a two-dimensional plane. Starting from the origin, he made N moves.
The N moves are represented by a string of length N as described below:
-
Takahashi's coordinates after the i-th move are:
- (x+1,y) if the i-th character of S is
R; - (x-1,y) if the i-th character of S is
L; - (x,y+1) if the i-th character of S is
U; and - (x,y-1) if the i-th character of S is
D,
where (x,y) is his coordinates before the move.
- (x+1,y) if the i-th character of S is
Determine if Takahashi visited the same coordinates multiple times in the course of the N moves (including the starting and ending points).
Constraints
- 1 \leq N \leq 2\times 10^5
- N is an integer.
- S is a string of length N consisting of
R,L,U, andD.
Input
The input is given from Standard Input in the following format:
N S
Output
Print Yes if Takahashi visited the same coordinates multiple times in the course of the N moves; print No otherwise.
Sample Input 1
5 RLURU
Sample Output 1
Yes
Takahashi's coordinates change as follows: (0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2).
Sample Input 2
20 URDDLLUUURRRDDDDLLLL
Sample Output 2
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
一方の端が赤に塗られており、もう一方の端が青に塗られているロープが N 本あります。ロープには 1 から N までの番号がつけられています。
これからロープの端を結ぶ操作を M 回行います。i 回目の操作ではロープ A_i の色 B_i の端とロープ C_i の色 D_i の端を結びます。ただし、色 R は赤を意味し、色 B は青を意味します。各ロープについて、同じ色の端が複数回結ばれることはありません。
すべての操作を終えた後に、ひとつながりになっているロープの組について環状になっているものとそうでないものの個数を出力してください。
ただし、ひとつながりになっているロープの組 \lbrace v_0, v_1, \ldots, v_{x-1} \rbrace が環状になっているとは、v の要素の順序を適切に入れ替えることで各 0 \leq i < x についてロープ v_i とロープ v_{(i+1) \bmod x} が結ばれているようにできることをいいます。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq A_i, C_i \leq N
- (A_i, B_i) \neq (A_j, B_j), (C_i, D_i) \neq (C_j, D_j) (i \neq j)
- (A_i, B_i) \neq (C_j, D_j)
- N, M, A_i, C_i は整数
- B_i, D_i は
RかBのいずれか
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_M B_M C_M D_M
出力
ひとつながりになっているロープの組について、環状になっているものの個数を X、そうでないものの個数を Y として X と Y をこの順に空白区切りで出力せよ。
入力例 1
5 3 3 R 5 B 5 R 3 B 4 R 2 B
出力例 1
1 2
ひとつながりになっているロープの組は \lbrace 1 \rbrace、\lbrace 2,4 \rbrace、\lbrace 3,5 \rbrace の 3 つです。
ロープ \lbrace 3,5 \rbrace の組は環状になっており、ロープ \lbrace 1 \rbrace と \lbrace 2,4 \rbrace の組は環状になっていません。したがって、X = 1, Y = 2 です。
入力例 2
7 0
出力例 2
0 7
入力例 3
7 6 5 R 3 R 7 R 4 R 4 B 1 R 2 R 3 B 2 B 5 B 1 B 7 B
出力例 3
2 1
Score : 400 points
Problem Statement
There are N ropes numbered 1 through N. One end of each rope is painted red, and the other is painted blue.
You are going to perform M operations of tying ropes. In the i-th operation, you tie the end of rope A_i painted B_i with the end of rope C_i painted D_i, where R means red and B means blue. For each rope, an end with the same color is not tied multiple times.
Find the number of groups of connected ropes that form cycles, and the number of those that do not, after all the operations.
Here, a group of connected ropes \lbrace v_0, v_1, \ldots, v_{x-1} \rbrace is said to form a cycle if one can rearrange the elements of v so that, for each 0 \leq i < x, rope v_i is tied to rope v_{(i+1) \bmod x}.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 1 \leq A_i, C_i \leq N
- (A_i, B_i) \neq (A_j, B_j), (C_i, D_i) \neq (C_j, D_j) (i \neq j)
- (A_i, B_i) \neq (C_j, D_j)
- N, M, A_i, and C_i are integers.
- B_i is
RorB, and so is D_i.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 C_1 D_1 A_2 B_2 C_2 D_2 \vdots A_M B_M C_M D_M
Output
Print X and Y in this order, separated by a space, where X is the number of groups of connected ropes that form cycles, and Y is the number of those that do not.
Sample Input 1
5 3 3 R 5 B 5 R 3 B 4 R 2 B
Sample Output 1
1 2
There are three groups of connected ropes: \lbrace 1 \rbrace, \lbrace 2,4 \rbrace, and \lbrace 3,5 \rbrace.
The group of ropes \lbrace 3,5 \rbrace forms a cycle, while the groups of rope \lbrace 1 \rbrace and ropes \lbrace 2,4 \rbrace do not. Thus, X = 1 and Y = 2.
Sample Input 2
7 0
Sample Output 2
0 7
Sample Input 3
7 6 5 R 3 R 7 R 4 R 4 B 1 R 2 R 3 B 2 B 5 B 1 B 7 B
Sample Output 3
2 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
AtCoder 王国には都市 1,2,\ldots,N の N 個の都市と、道路 1,2,\ldots,M の M 本の道路があります。
道路 i は都市 A_i と B_i を双方向に結び、距離は C_i です。
どの都市間もいくつかの道路を通って行き来することができます。
財政難である王国は、どの都市間もいくつかの道路を通って行き来できるという条件を満たすように N-1 本の道路を保守し、それ以外の道路を廃道にすることにしました。
保守する道路のみを通って都市 1 から都市 i へ移動するときの距離を d_i とするとき、保守する道路の選び方であって、d_2+d_3+\ldots+d_N を最小化するようなものを 1 つ出力してください。
制約
- 2 \leq N \leq 2\times 10^5
- N-1 \leq M \leq 2\times 10^5
- 1 \leq A_i < B_i \leq N
- i\neq j のとき、(A_i,B_i)\neq(A_j,B_j)
- 1\leq C_i \leq 10^9
- どの都市間もいくつかの道路を通って行き来することができる
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
出力
保守するような道路の番号を空白区切りで出力せよ。出力の順序は問わない。
答えが複数存在する場合、どれを出力しても正解とみなされる。
入力例 1
3 3 1 2 1 2 3 2 1 3 10
出力例 1
1 2
保守する道路の選び方と d_i の値は次のようになります。
- 道路 1,2 を保守するとき、d_2=1, d_3=3
- 道路 1,3 を保守するとき、d_2=1, d_3=10
- 道路 2,3 を保守するとき、d_2=12, d_3=10
よって、道路 1,2 を保守するときに d_2+d_3 が最小になります。
入力例 2
4 6 1 2 1 1 3 1 1 4 1 2 3 1 2 4 1 3 4 1
出力例 2
3 1 2
Score : 500 points
Problem Statement
The Kingdom of AtCoder has N cities called City 1,2,\ldots,N and M roads called Road 1,2,\ldots,M.
Road i connects Cities A_i and B_i bidirectionally and has a length of C_i.
One can travel between any two cities using some roads.
Under financial difficulties, the kingdom has decided to maintain only N-1 roads so that one can still travel between any two cities using those roads and abandon the rest.
Let d_i be the total length of the roads one must use when going from City 1 to City i using only maintained roads. Print a choice of roads to maintain that minimizes d_2+d_3+\ldots+d_N.
Constraints
- 2 \leq N \leq 2\times 10^5
- N-1 \leq M \leq 2\times 10^5
- 1 \leq A_i < B_i \leq N
- (A_i,B_i)\neq(A_j,B_j) if i\neq j.
- 1\leq C_i \leq 10^9
- One can travel between any two cities using some roads.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_M B_M C_M
Output
Print the indices of roads to maintain, in arbitrary order, with spaces in between.
If multiple solutions exist, you may print any of them.
Sample Input 1
3 3 1 2 1 2 3 2 1 3 10
Sample Output 1
1 2
Here are the possible choices of roads to maintain and the corresponding values of d_i.
- Maintain Road 1 and 2: d_2=1, d_3=3.
- Maintain Road 1 and 3: d_2=1, d_3=10.
- Maintain Road 2 and 3: d_2=12, d_3=10.
Thus, maintaining Road 1 and 2 minimizes d_2+d_3.
Sample Input 2
4 6 1 2 1 1 3 1 1 4 1 2 3 1 2 4 1 3 4 1
Sample Output 2
3 1 2
Time Limit: 6 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N 次元空間上の 2 点 x=(x_1, x_2, \dots, x_N), y = (y_1, y_2, \dots, y_N) のマンハッタン距離 d(x,y) は次の式で定義されます。
また、座標成分 x_1, x_2, \dots, x_N がすべて整数であるような点 x=(x_1, x_2, \dots, x_N) を格子点と呼びます。
N 次元空間上の格子点 p=(p_1, p_2, \dots, p_N), q = (q_1, q_2, \dots, q_N) が与えられます。
d(p,r) \leq D かつ d(q,r) \leq D であるような格子点 r としてあり得るものは全部で何個ありますか?答えを 998244353 で割ったあまりを求めてください。
制約
- 1 \leq N \leq 100
- 0 \leq D \leq 1000
- -1000 \leq p_i, q_i \leq 1000
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D p_1 p_2 \dots p_N q_1 q_2 \dots q_N
出力
答えを出力せよ。
入力例 1
1 5 0 3
出力例 1
8
N=1 の場合は 1 次元空間、すなわち数直線上の点に関する問題になります。
条件を満たす点は -2,-1,0,1,2,3,4,5 の 8 個です。
入力例 2
3 10 2 6 5 2 1 2
出力例 2
632
入力例 3
10 100 3 1 4 1 5 9 2 6 5 3 2 7 1 8 2 8 1 8 2 8
出力例 3
145428186
Score : 500 points
Problem Statement
In an N-dimensional space, the Manhattan distance d(x,y) between two points x=(x_1, x_2, \dots, x_N) and y = (y_1, y_2, \dots, y_N) is defined by:
A point x=(x_1, x_2, \dots, x_N) is said to be a lattice point if the components x_1, x_2, \dots, x_N are all integers.
You are given lattice points p=(p_1, p_2, \dots, p_N) and q = (q_1, q_2, \dots, q_N) in an N-dimensional space.
How many lattice points r satisfy d(p,r) \leq D and d(q,r) \leq D? Find the count modulo 998244353.
Constraints
- 1 \leq N \leq 100
- 0 \leq D \leq 1000
- -1000 \leq p_i, q_i \leq 1000
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N D p_1 p_2 \dots p_N q_1 q_2 \dots q_N
Output
Print the answer.
Sample Input 1
1 5 0 3
Sample Output 1
8
When N=1, we consider points in a one-dimensional space, that is, on a number line.
8 lattice points satisfy the conditions: -2,-1,0,1,2,3,4,5.
Sample Input 2
3 10 2 6 5 2 1 2
Sample Output 2
632
Sample Input 3
10 100 3 1 4 1 5 9 2 6 5 3 2 7 1 8 2 8 1 8 2 8
Sample Output 3
145428186