Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
3 種類の文字 . | * からなる、長さ N の文字列 S が与えられます。
S には | がちょうど 2 つ、* がちょうど 1 つ含まれます。
2 つの | で囲まれた部分の中に * が含まれるか判定してください。
含まれている場合 in と、含まれていない場合 out と出力してください。
より厳密には、* より前にある文字のいずれかが | であり、かつ、* より後ろにある文字のいずれかが | であるか判定してください。
制約
- 3\leq N\leq 100
- N は整数
- S は
.|*からなる長さ N の文字列 - S に
|はちょうど 2 個含まれる - S に
*はちょうど 1 個含まれる
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
2 つの | に囲まれた部分の中に * が含まれている場合 in と、含まれていない場合 out と 1 行に出力せよ。
入力例 1
10 .|..*...|.
出力例 1
in
2 つの | に囲まれた部分は |..*...| です。
この中に * が含まれているため、in と出力してください。
入力例 2
10 .|..|.*...
出力例 2
out
2 つの | に囲まれた部分は |..| です。
この中に * は含まれていないため、out と出力してください。
入力例 3
3 |*|
出力例 3
in
Score : 100 points
Problem Statement
You are given a string S of length N consisting of three kinds of characters: ., |, and *.
S contains exactly two | and exactly one *.
Determine whether the * is between the two |, and if so, print in; otherwise, print out.
More formally, determine whether one of the characters before the * is | and one of the characters after the * is |.
Constraints
- 3\leq N\leq 100
- N is an integer.
- S is a string of length N consisting of
.,|, and*. - S contains exactly two
|. - S contains exactly one
*.
Input
The input is given from Standard Input in the following format:
N S
Output
Print a single line containing in if the * is between the two |, and out otherwise.
Sample Input 1
10 .|..*...|.
Sample Output 1
in
Between the two |, we have |..*...|, which contains *, so you should print in.
Sample Input 2
10 .|..|.*...
Sample Output 2
out
Between the two |, we have |..|, which does not contain *, so you should print out.
Sample Input 3
3 |*|
Sample Output 3
in
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
A を N 個、B を N 個、…、Z を N 個この順に繋げて得られる文字列の先頭から X 番目の文字を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq X \leq N\times 26
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N X
出力
答えを出力せよ。
入力例 1
1 3
出力例 1
C
得られる文字列は ABCDEFGHIJKLMNOPQRSTUVWXYZ です。先頭から 3 番目の文字は C です。
入力例 2
2 12
出力例 2
F
得られる文字列は AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ です。先頭から 12 番目の文字は F です。
Score : 100 points
Problem Statement
Find the X-th character from the beginning of the string that is obtained by concatenating these characters: N copies of A's, N copies of B's, …, and N copies of Z's, in this order.
Constraints
- 1 \leq N \leq 100
- 1 \leq X \leq N\times 26
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X
Output
Print the answer.
Sample Input 1
1 3
Sample Output 1
C
We obtain the string ABCDEFGHIJKLMNOPQRSTUVWXYZ, whose 3-rd character from the beginning is C.
Sample Input 2
2 12
Sample Output 2
F
We obtain the string AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ, whose 12-th character from the beginning is F.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) 及び整数 L,R が与えられます。ここで L,R は L\leq R を満たします。
i=1,2,\ldots,N について以下の 2 つの条件を共に満たす整数 X_i を求めてください。なお、求める整数は常に一意に定まります。
- L\leq X_i \leq R
- L 以上 R 以下であるようなどの整数 Y についても |X_i - A_i| \leq |Y-A_i| を満たす
制約
- 1\leq N\leq 2\times 10^5
- 1\leq L\leq R \leq 10^9
- 1\leq A_i\leq 10^9
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N L R A_1 \ldots A_N
出力
i=1,2,\ldots,N について X_i を空白区切りで出力せよ。
入力例 1
5 4 7 3 1 4 9 7
出力例 1
4 4 4 7 7
i=1 では、
- |4-3|=1
- |5-3|=2
- |6-3|=3
- |7-3|=4
より X_i = 4 です。
入力例 2
3 10 10 11 10 9
出力例 2
10 10 10
Score : 200 points
Problem Statement
You are given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N and integers L and R such that L\leq R.
For each i=1,2,\ldots,N, find the integer X_i that satisfies both of the following conditions. Note that the integer to be found is always uniquely determined.
- L\leq X_i \leq R.
- For every integer Y such that L \leq Y \leq R, it holds that |X_i - A_i| \leq |Y - A_i|.
Constraints
- 1\leq N\leq 2\times 10^5
- 1\leq L\leq R \leq 10^9
- 1\leq A_i\leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N L R A_1 \ldots A_N
Output
Print X_i for i=1,2,\ldots,N, separated by spaces.
Sample Input 1
5 4 7 3 1 4 9 7
Sample Output 1
4 4 4 7 7
For i=1:
- |4-3|=1
- |5-3|=2
- |6-3|=3
- |7-3|=4
Thus, X_i = 4.
Sample Input 2
3 10 10 11 10 9
Sample Output 2
10 10 10
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君はゲームの中で洞窟を探索しています。
洞窟は N 個の部屋が一列に並んだ構造であり、入り口から順に部屋 1,2,\ldots,N と番号がついています。
最初、高橋君は部屋 1 におり、持ち時間 は T です。
各 1 \leq i \leq N-1 について、持ち時間を A_i 消費することで、部屋 i から部屋 i+1 へ移動することができます。これ以外に部屋を移動する方法はありません。
また、持ち時間が 0 以下になるような移動は行うことができません。
洞窟内には M 個のボーナス部屋があります。i 番目のボーナス部屋は部屋 X_i であり、この部屋に到達すると持ち時間が Y_i 増加します。
高橋君は部屋 N にたどりつくことができますか?
制約
- 2 \leq N \leq 10^5
- 0 \leq M \leq N-2
- 1 \leq T \leq 10^9
- 1 \leq A_i \leq 10^9
- 1 < X_1 < \ldots < X_M < N
- 1 \leq Y_i \leq 10^9
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M T
A_1 A_2 \ldots A_{N-1}
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M
出力
高橋君が部屋 N にたどりつくことができるなら Yes を、できないなら No を出力せよ。
入力例 1
4 1 10 5 7 5 2 10
出力例 1
Yes
- 高橋君は最初、部屋 1 にいて持ち時間は 10 です。
- 持ち時間を 5 消費して部屋 2 に移動します。持ち時間は 5 になります。その後、持ち時間が 10 増え 15 になります。
- 持ち時間を 7 消費して部屋 3 に移動します。持ち時間は 8 になります。
- 持ち時間を 5 消費して部屋 4 に移動します。持ち時間は 3 になります。
入力例 2
4 1 10 10 7 5 2 10
出力例 2
No
部屋 1 から部屋 2 へ移動することができません。
Score : 200 points
Problem Statement
Takahashi is exploring a cave in a video game.
The cave consists of N rooms arranged in a row. The rooms are numbered Room 1,2,\ldots,N from the entrance.
Takahashi is initially in Room 1, and the time limit is T.
For each 1 \leq i \leq N-1, he may consume a time of A_i to move from Room i to Room (i+1). There is no other way to move between rooms.
He cannot make a move that makes the time limit 0 or less.
There are M bonus rooms in the cave. The i-th bonus room is Room X_i; when he arrives at the room, the time limit increases by Y_i.
Can Takahashi reach Room N?
Constraints
- 2 \leq N \leq 10^5
- 0 \leq M \leq N-2
- 1 \leq T \leq 10^9
- 1 \leq A_i \leq 10^9
- 1 < X_1 < \ldots < X_M < N
- 1 \leq Y_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M T
A_1 A_2 \ldots A_{N-1}
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M
Output
If Takahashi can reach Room N, print Yes; otherwise, print No.
Sample Input 1
4 1 10 5 7 5 2 10
Sample Output 1
Yes
- Takahashi is initially in Room 1, and the time limit is 10.
- He consumes a time of 5 to move to Room 2. Now the time limit is 5. Then, the time limit increases by 10; it is now 15.
- He consumes a time of 7 to move to Room 3. Now the time limit is 8.
- He consumes a time of 5 to move to Room 4. Now the time limit is 3.
Sample Input 2
4 1 10 10 7 5 2 10
Sample Output 2
No
He cannot move from Room 1 to Room 2.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
整数 N と o と x からなる長さ N の文字列 S が与えられます。
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) があります。はじめ A=(1,2,\ldots,N) です。
A に対して k=1,2,\ldots,N の順に以下の操作を行います:
- S_k=
oである場合、A の先頭 k 項を反転する。具体的には、A を (A_k,A_{k-1},\ldots,A_1,A_{k+1},A_{k+2},\ldots,A_N) に置き換える。 - S_k=
xである場合は何もしない。
全ての操作を終えた後の A を求めてください。
制約
- 2\le N\le 5\times 10^5
- N は整数
- S は
oとxからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
全ての操作を終えた後の A の各要素を空白区切りで出力せよ。
入力例 1
5 ooxoo
出力例 1
5 2 1 3 4
A は各操作で以下のように変化します:
- k=1 のとき:A の先頭 1 項を反転する。A=(1,2,3,4,5) になる。
- k=2 のとき:A の先頭 2 項を反転する。A=(2,1,3,4,5) になる。
- k=3 のとき:何もしない。
- k=4 のとき:A の先頭 4 項を反転する。A=(4,3,1,2,5) になる。
- k=5 のとき:A の先頭 5 項を反転する。A=(5,2,1,3,4) になる。
全ての操作を終えた後の A は A=(5,2,1,3,4) です。
入力例 2
7 ooooooo
出力例 2
7 5 3 1 2 4 6
入力例 3
15 xooxoxoxoxoxxoo
出力例 3
15 11 10 7 6 3 1 2 4 5 8 9 12 13 14
Score : 300 points
Problem Statement
You are given an integer N and a string S of length N consisting of o and x.
There is an integer sequence A=(A_1,A_2,\ldots,A_N) of length N. Initially, A=(1,2,\ldots,N).
Perform the following operation on A for k=1,2,\ldots,N in this order.
- If S_k=
o, reverse the first k terms of A. Specifically, replace A with (A_k,A_{k-1},\ldots,A_1,A_{k+1},A_{k+2},\ldots,A_N). - If S_k=
x, do nothing.
Find A after all the operations are completed.
Constraints
- 2\le N\le 5\times 10^5
- N is an integer.
- S is a string of length N consisting of
oandx.
Input
The input is given from Standard Input in the following format:
N S
Output
Output the elements of A after all the operations are completed, separated by spaces.
Sample Input 1
5 ooxoo
Sample Output 1
5 2 1 3 4
A changes as follows with each operation:
- For k=1: reverse the first 1 term of A. A becomes (1,2,3,4,5).
- For k=2: reverse the first 2 terms of A. A becomes (2,1,3,4,5).
- For k=3: do nothing.
- For k=4: reverse the first 4 terms of A. A becomes (4,3,1,2,5).
- For k=5: reverse the first 5 terms of A. A becomes (5,2,1,3,4).
After all the operations are completed, A is A=(5,2,1,3,4).
Sample Input 2
7 ooooooo
Sample Output 2
7 5 3 1 2 4 6
Sample Input 3
15 xooxoxoxoxoxxoo
Sample Output 3
15 11 10 7 6 3 1 2 4 5 8 9 12 13 14
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
AtCoder 社の入口には特殊なパスコード入力装置があり、 この装置は文字列を 1 つ表示する画面と 2 つのボタンからなります。
画面に表示される文字列を t とします。 t ははじめ空文字列であり、ボタンを押すことで t に以下の変化が起きます。
- ボタン A を押すと、t の末尾に
0が追加される。 - ボタン B を押すと、t に含まれるすべての数字が、それぞれ次の数字に置き換わる。ここで、
0から8までの数字については次の数字は 1 大きな数を表す数字とし、9の次の数字は0とする。
たとえば、t が 1984 のときにボタン A を押すと t は 19840 に変化し、さらにボタン B を押すと t は 20951 に変化します。
文字列 S が与えられます。t が空文字列である状態からボタンを 0 回以上押して t を S に一致させるとき、ボタンを最少で何回押す必要があるかを求めてください。
制約
- S は
0,1,2,3,4,5,6,7,8,9からなる文字列 - 1 \leq |S| \leq 5 \times 10^5 (|S| は S の長さをあらわす)
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
21
出力例 1
4
以下の順にボタンを押せば、t が 21 になります。
- ボタン A を押す。t は
0になる。 - ボタン B を押す。t は
1になる。 - ボタン A を押す。t は
10になる。 - ボタン B を押す。t は
21になる。
4 回より少ない回数ボタンを押して t を 21 にすることはできないので、4 を出力します。
入力例 2
407
出力例 2
17
入力例 3
2025524202552420255242025524
出力例 3
150
Score : 300 points
Problem Statement
At the entrance of AtCoder Inc., there is a special pass-code input device. The device consists of a screen displaying one string, and two buttons.
Let t be the string shown on the screen. Initially, t is the empty string. Pressing a button changes t as follows:
- Pressing button A appends
0to the end of t. - Pressing button B replaces every digit in t with the next digit: for digits
0through8the next digit is the one whose value is larger by 1; the next digit after9is0.
For example, if t is 1984 and button A is pressed, t becomes 19840; if button B is then pressed, t becomes 20951.
You are given a string S. Starting from the empty string, press the buttons zero or more times until t coincides with S. Find the minimum number of button presses required.
Constraints
- S is a string consisting of
0,1,2,3,4,5,6,7,8, and9. - 1 \le |S| \le 5 \times 10^{5}, where |S| is the length of S.
Input
The input is given from Standard Input in the following format:
S
Output
Output the answer.
Sample Input 1
21
Sample Output 1
4
The following sequence of presses makes t equal to 21.
- Press button A. t becomes
0. - Press button B. t becomes
1. - Press button A. t becomes
10. - Press button B. t becomes
21.
It is impossible to obtain 21 with fewer than four presses, so output 4.
Sample Input 2
407
Sample Output 2
17
Sample Input 3
2025524202552420255242025524
Sample Output 3
150
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
無限に広い 2 次元グリッドがあり、このグリッドの座標 (0,0) に焚き火があります。
時刻 t=0 では、マス (0,0) にのみ煙が存在します。
N, W, S, E からなる長さ N の文字列 S が与えられ、時刻 t=1,2,\dots,N では次の現象が順番に発生します。
- 風が吹き、現時点で存在する全ての煙が以下の通りに移動する。
- S の t 文字目が
Nであるとき、マス (r,c) にある煙がマス (r-1,c) に移動する。 - S の t 文字目が
Wであるとき、マス (r,c) にある煙がマス (r,c-1) に移動する。 - S の t 文字目が
Sであるとき、マス (r,c) にある煙がマス (r+1,c) に移動する。 - S の t 文字目が
Eであるとき、マス (r,c) にある煙がマス (r,c+1) に移動する。
- S の t 文字目が
- マス (0,0) に煙が存在しない場合、新たな煙がマス (0,0) に生成される。
高橋君はマス (R,C) に立っています。
整数 1 \le t \le N について、時刻 t+0.5 においてマス (R,C) に煙が存在するか判定し、出力欄の形式に従って出力してください。
制約
- N は 1 以上 200000 以下の整数
- S は
N,W,S,Eからなる長さ N の文字列 - R,C は -N 以上 N 以下の整数
- (R,C) \neq (0,0)
入力
入力は以下の形式で標準入力から与えられる。
N R C S
出力
答えを N 文字の 0, 1 からなる文字列として出力せよ。
出力する文字列のうち t 文字目 (1 \le t \le N) は次の通りにせよ。
- 時刻 t+0.5 においてマス (R,C) に煙が存在するなら
1 - 時刻 t+0.5 においてマス (R,C) に煙が存在しないなら
0
入力例 1
6 -2 1 NNEEWS
出力例 1
001010
時刻 1.5,2.5,4.5,6.5 ではマス (-2,1) には煙が存在せず、時刻 3.5,5.5 ではマス (-2,1) に煙が存在します。
よって、 001010 と出力します。
図では焚き火のあるマス (0,0) を基準として、マス (r,c) を
- r < 0 なら -r マス上に
- r \ge 0 なら r マス下に
- c < 0 なら -c マス左に
- c \ge 0 なら c マス右に
描画します。
時刻 0.5 でのグリッドの状態は次の通りです。

時刻 1.5 でのグリッドの状態は次の通りです。

時刻 2.5 でのグリッドの状態は次の通りです。

時刻 3.5 でのグリッドの状態は次の通りです。

時刻 4.5 でのグリッドの状態は次の通りです。

時刻 5.5 でのグリッドの状態は次の通りです。

時刻 6.5 でのグリッドの状態は次の通りです。

入力例 2
10 1 2 NEESESWEES
出力例 2
0001101011
入力例 3
20 -1 -2 WWNNWSWEWNSWWENSNWWN
出力例 3
00100111111000101111
Score : 425 points
Problem Statement
There is an infinitely large two-dimensional grid, with a campfire at coordinate (0,0).
At time t=0, smoke exists only at cell (0,0).
You are given a length-N string S consisting of N, W, S, E. At times t=1,2,\dots,N, the following happen in order:
- Wind blows, and all the smoke present at that time moves as follows:
- If the t-th character of S is
N, smoke in cell (r,c) moves to cell (r-1,c). - If it is
W, smoke in cell (r,c) moves to cell (r,c-1). - If it is
S, smoke in cell (r,c) moves to cell (r+1,c). - If it is
E, smoke in cell (r,c) moves to cell (r,c+1).
- If the t-th character of S is
- If there is no smoke in cell (0,0), new smoke is generated at cell (0,0).
Takahashi is standing at cell (R,C).
For each integer 1 \le t \le N, determine if smoke exists at cell (R,C) at time t+0.5, and print the response according to the required format.
Constraints
- N is an integer between 1 and 200000, inclusive.
- S is a length N string consisting of
N,W,S,E. - R and C are integers between -N and N, inclusive.
- (R,C) \neq (0,0)
Input
The input is given from Standard Input in the following format:
N R C S
Output
Print an N-character string consisting of 0 and 1.
The t-th character (1 \le t \le N) should be:
1if smoke exists at cell (R,C) at time t+0.5, and0otherwise.
Sample Input 1
6 -2 1 NNEEWS
Sample Output 1
001010
At times 1.5,2.5,4.5,6.5, there is no smoke at cell (-2,1). At times 3.5,5.5, there is smoke at cell (-2,1).
Hence, output 001010.
In the figures below, taking cell (0,0) with the campfire as a reference, cell (r,c) is drawn:
- -r cells up if r < 0,
- r cells down if r \ge 0,
- -c cells left if c < 0,
- c cells right if c \ge 0.
The grid at time 0.5 looks like:

The grid at time 1.5 looks like:

The grid at time 2.5 looks like:

The grid at time 3.5 looks like:

The grid at time 4.5 looks like:

The grid at time 5.5 looks like:

The grid at time 6.5 looks like:

Sample Input 2
10 1 2 NEESESWEES
Sample Output 2
0001101011
Sample Input 3
20 -1 -2 WWNNWSWEWNSWWENSNWWN
Sample Output 3
00100111111000101111
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
長さ N の数列 A = (A_1, A_2, \dots, A_N) および整数 K があります。
A をいくつかの連続部分列に分割する方法は 2^{N-1} 通りありますが、そのうち分割後に要素の和が K である列が存在しない分割の方法は何通りありますか。答えを 998244353 で割った余りを求めてください。
ここで、「A をいくつかの連続部分列に分割する」とは次の手順のことを言います。
- 分割後の数列の個数 k (1 \leq k \leq N) および 1 = i_1 \lt i_2 \lt \dots \lt i_k \lt i_{k+1} = N+1 を満たす整数列 (i_1, i_2, \dots, i_k, i_{k+1}) を自由に選ぶ。
- 1 \leq n \leq k について、n 番目の数列を、A の i_n 番目から i_{n+1} - 1 番目までの要素を順番を保ったまま取り出して出来る数列とする。
A = (1, 2, 3, 4, 5) のときの分割の例を以下に挙げます。
- (1, 2, 3), (4), (5)
- (1, 2), (3, 4, 5)
- (1, 2, 3, 4, 5)
制約
- 1 \leq N \leq 2 \times 10^5
- -10^{15} \leq K \leq 10^{15}
- -10^9 \leq A_i \leq 10^9
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを 998244353 で割った余りを出力せよ。
入力例 1
3 3 1 2 3
出力例 1
2
問題文の条件を満たす分割は次の 2 通りです。
- (1), (2, 3)
- (1, 2, 3)
入力例 2
5 0 0 0 0 0 0
出力例 2
0
入力例 3
10 5 -5 -1 -7 6 -6 -2 -5 10 2 -10
出力例 3
428
Score : 475 points
Problem Statement
You are given a sequence A = (A_1, A_2, \dots, A_N) of length N and an integer K.
There are 2^{N-1} ways to divide A into several contiguous subsequences. How many of these divisions have no subsequence whose elements sum to K? Find the count modulo 998244353.
Here, "to divide A into several contiguous subsequences" means the following procedure.
- Freely choose the number k (1 \leq k \leq N) of subsequences and an integer sequence (i_1, i_2, \dots, i_k, i_{k+1}) satisfying 1 = i_1 \lt i_2 \lt \dots \lt i_k \lt i_{k+1} = N+1.
- For each 1 \leq n \leq k, the n-th subsequence is formed by taking the i_n-th through (i_{n+1} - 1)-th elements of A, maintaining their order.
Here are some examples of divisions for A = (1, 2, 3, 4, 5):
- (1, 2, 3), (4), (5)
- (1, 2), (3, 4, 5)
- (1, 2, 3, 4, 5)
Constraints
- 1 \leq N \leq 2 \times 10^5
- -10^{15} \leq K \leq 10^{15}
- -10^9 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the count modulo 998244353.
Sample Input 1
3 3 1 2 3
Sample Output 1
2
There are two divisions that satisfy the condition in the problem statement:
- (1), (2, 3)
- (1, 2, 3)
Sample Input 2
5 0 0 0 0 0 0
Sample Output 2
0
Sample Input 3
10 5 -5 -1 -7 6 -6 -2 -5 10 2 -10
Sample Output 3
428
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
正整数 N と長さ N の正整数列 A=(A_1,A_2,\dots,A_N) と B=(B_1,B_2,\dots,B_N) が与えられます。
N \times N のマス目があります。上から i 行目、左から j 列目のマスをマス (i,j) と呼びます。1 \le i,j \le N を満たす整数の組 (i,j) に対し、マス (i,j) に A_i + B_j が書かれています。以下のクエリを Q 個処理してください。
- 1 \le h_1 \le h_2 \le N,1 \le w_1 \le w_2 \le N を満たす整数の組 h_1,h_2,w_1,w_2 が与えられる。左上隅が (h_1,w_1)、右下隅が (h_2,w_2) である矩形領域に含まれる整数の最大公約数を求めよ。
制約
- 1 \le N,Q \le 2 \times 10^5
- 1 \le A_i,B_i \le 10^9
- 1 \le h_1 \le h_2 \le N
- 1 \le w_1 \le w_2 \le N
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N Q
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
各クエリは以下の形式で与えられる。
h_1 h_2 w_1 w_2
出力
Q 行出力せよ。i 行目には \mathrm{query}_i の答えを出力せよ。
入力例 1
3 5 3 5 2 8 1 3 1 2 2 3 1 3 1 3 1 1 1 1 2 2 2 2 3 3 1 1
出力例 1
2 1 11 6 10
マス (i,j) に書かれている整数を C_{i,j} とします。
1 個目のクエリについて、C_{1,2}=4,C_{1,3}=6,C_{2,2}=6,C_{2,3}=8 なのでこれらの最大公約数の 2 が答えとなります。
入力例 2
1 1 9 100 1 1 1 1
出力例 2
109
Score : 500 points
Problem Statement
You are given a positive integer N and sequences of N positive integers each: A=(A_1,A_2,\dots,A_N) and B=(B_1,B_2,\dots,B_N).
We have an N \times N grid. The square at the i-th row from the top and the j-th column from the left is called the square (i,j). For each pair of integers (i,j) such that 1 \le i,j \le N, the square (i,j) has the integer A_i + B_j written on it. Process Q queries of the following form.
- You are given a quadruple of integers h_1,h_2,w_1,w_2 such that 1 \le h_1 \le h_2 \le N,1 \le w_1 \le w_2 \le N. Find the greatest common divisor of the integers contained in the rectangle region whose top-left and bottom-right corners are (h_1,w_1) and (h_2,w_2), respectively.
Constraints
- 1 \le N,Q \le 2 \times 10^5
- 1 \le A_i,B_i \le 10^9
- 1 \le h_1 \le h_2 \le N
- 1 \le w_1 \le w_2 \le N
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N Q
A_1 A_2 \dots A_N
B_1 B_2 \dots B_N
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q
Each query is in the following format:
h_1 h_2 w_1 w_2
Output
Print Q lines. The i-th line should contain the answer to \mathrm{query}_i.
Sample Input 1
3 5 3 5 2 8 1 3 1 2 2 3 1 3 1 3 1 1 1 1 2 2 2 2 3 3 1 1
Sample Output 1
2 1 11 6 10
Let C_{i,j} denote the integer on the square (i,j).
For the 1-st query, we have C_{1,2}=4,C_{1,3}=6,C_{2,2}=6,C_{2,3}=8, so the answer is their greatest common divisor, which is 2.
Sample Input 2
1 1 9 100 1 1 1 1
Sample Output 2
109