実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 150 点
問題文
英小文字からなる文字列 S が与えられます。ここで S の長さは 3 以上 100 以下です。
S はある 1 文字を除いて全て同じ文字で構成されています。
他のどの文字とも異なる文字は前から何文字目でしょうか。
制約
- S は 2 種類の英小文字からなる長さ 3 以上 100 以下の文字列
- S はある 1 文字を除いて全て同じ文字
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
yay
出力例 1
2
yay
の 2 文字目は、1 文字目とも 3 文字目とも異なります。
入力例 2
egg
出力例 2
1
入力例 3
zzzzzwz
出力例 3
6
Score: 150 points
Problem Statement
You are given a string S consisting of lowercase English letters. The length of S is between 3 and 100, inclusive.
All characters but one of S are the same.
Find x such that the x-th character of S differs from all other characters.
Constraints
- S is a string of length between 3 and 100, inclusive, consisting of two different lowercase English letters.
- All characters but one of S are the same.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
yay
Sample Output 1
2
The second character of yay
differs from the first and third characters.
Sample Input 2
egg
Sample Output 2
1
Sample Input 3
zzzzzwz
Sample Output 3
6
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
鉄道の AtCoder 線には N 個の駅があり、それぞれ 1, 2, \ldots, N の番号が付けられています。
AtCoder 線では、駅 1 を始発駅として駅 2, 3, \ldots, N の順に各駅に停車する上り列車および、駅 N を始発駅として駅 N - 1, N - 2, \ldots, 1 の順に各駅に停車する下り列車が運行されています。
高橋君は AtCoder 線の上り列車あるいは下り列車の一方のみを使うことで駅 X から駅 Y まで移動しようとしています。
この移動の間に高橋君が乗っている電車が駅 Z に停車することがあるか判定してください。
制約
- 3 \leq N \leq 100
- 1 \leq X, Y, Z \leq N
- X, Y, Z は相異なる
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N X Y Z
出力
駅 X から駅 Y への移動の間に高橋君が乗っている電車が駅 Z に停車することがあるならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
7 6 1 3
出力例 1
Yes
駅 6 から駅 1 に移動するためには下り列車に乗車します。
駅 6 を出発し、駅 5, 4, 3, 2, 1 の順に停車するため移動の間に電車が駅 3 に停車することがあり、Yes
を出力します。
入力例 2
10 3 2 9
出力例 2
No
入力例 3
100 23 67 45
出力例 3
Yes
Score: 100 points
Problem Statement
The AtCoder railway line has N stations, numbered 1, 2, \ldots, N.
On this line, there are inbound trains that start at station 1 and stop at the stations 2, 3, \ldots, N in order, and outbound trains that start at station N and stop at the stations N - 1, N - 2, \ldots, 1 in order.
Takahashi is about to travel from station X to station Y using only one of the inbound and outbound trains.
Determine whether the train stops at station Z during this travel.
Constraints
- 3 \leq N \leq 100
- 1 \leq X, Y, Z \leq N
- X, Y, and Z are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N X Y Z
Output
If the train stops at station Z during the travel from station X to station Y, print Yes
; otherwise, print No
.
Sample Input 1
7 6 1 3
Sample Output 1
Yes
To travel from station 6 to station 1, Takahashi will take an outbound train.
After departing from station 6, the train stops at stations 5, 4, 3, 2, 1 in order, which include station 3, so you should print Yes
.
Sample Input 2
10 3 2 9
Sample Output 2
No
Sample Input 3
100 23 67 45
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
AtCoder 商店には N 個の商品があります。 i\ (1\leq i\leq N) 番目の商品の価格は P _ i です。 i\ (1\leq i\leq N) 番目の商品は C _ i 個の機能をもち、i\ (1\leq i\leq N) 番目の商品の j\ (1\leq j\leq C _ i) 番目の機能は 1 以上 M 以下の整数 F _ {i,j} として表されます。
高橋くんは、AtCoder 商店の商品で一方が一方の上位互換であるものがないか気になりました。
i 番目の商品と j 番目の商品 (1\leq i,j\leq N) であって、次の条件をすべて満たすものがあるとき Yes
と、ないとき No
と出力してください。
- P _ i\geq P _ j である。
- j 番目の製品は i 番目の製品がもつ機能をすべてもつ。
- P _ i\gt P _ j であるか、j 番目の製品は i 番目の製品にない機能を 1 つ以上もつ。
制約
- 2\leq N\leq100
- 1\leq M\leq100
- 1\leq P _ i\leq10^5\ (1\leq i\leq N)
- 1\leq C _ i\leq M\ (1\leq i\leq N)
- 1\leq F _ {i,1}\lt F _ {i,2}\lt\cdots\lt F _ {i,C _ i}\leq M\ (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M P _ 1 C _ 1 F _ {1,1} F _ {1,2} \ldots F _ {1,C _ 1} P _ 2 C _ 2 F _ {2,1} F _ {2,2} \ldots F _ {2,C _ 2} \vdots P _ N C _ N F _ {N,1} F _ {N,2} \ldots F _ {N,C _ N}
出力
答えを 1 行で出力せよ。
入力例 1
5 6 10000 2 1 3 15000 3 1 2 4 30000 3 1 3 5 35000 2 1 5 100000 6 1 2 3 4 5 6
出力例 1
Yes
(i,j)=(4,3) とすると、条件を全て満たします。
他の組は条件を満たしません。例えば (i,j)=(4,5) とすると j 番目の商品は i 番目の商品の機能をすべてもっていますが、P _ i\lt P _ j なので上位互換ではありません。
入力例 2
4 4 3 1 1 3 1 2 3 1 2 4 2 2 3
出力例 2
No
まったく同じ価格と機能をもった商品がある場合もあります。
入力例 3
20 10 72036 3 3 4 9 7716 4 1 2 3 6 54093 5 1 6 7 8 10 25517 7 3 4 5 6 7 9 10 96930 8 2 3 4 6 7 8 9 10 47774 6 2 4 5 6 7 9 36959 5 1 3 4 5 8 46622 7 1 2 3 5 6 8 10 34315 9 1 3 4 5 6 7 8 9 10 54129 7 1 3 4 6 7 8 9 4274 5 2 4 7 9 10 16578 5 2 3 6 7 9 61809 4 1 2 4 5 1659 5 3 5 6 9 10 59183 5 1 2 3 4 9 22186 4 3 5 6 8 98282 4 1 4 7 10 72865 8 1 2 3 4 6 8 9 10 33796 6 1 3 5 7 9 10 74670 4 1 2 6 8
出力例 3
Yes
Score : 200 points
Problem Statement
AtCoder Shop has N products. The price of the i-th product (1\leq i\leq N) is P _ i. The i-th product (1\leq i\leq N) has C_i functions. The j-th function (1\leq j\leq C _ i) of the i-th product (1\leq i\leq N) is represented as an integer F _ {i,j} between 1 and M, inclusive.
Takahashi wonders whether there is a product that is strictly superior to another.
If there are i and j (1\leq i,j\leq N) such that the i-th and j-th products satisfy all of the following conditions, print Yes
; otherwise, print No
.
- P _ i\geq P _ j.
- The j-th product has all functions of the i-th product.
- P _ i\gt P _ j, or the j-th product has one or more functions that the i-th product lacks.
Constraints
- 2\leq N\leq100
- 1\leq M\leq100
- 1\leq P _ i\leq10^5\ (1\leq i\leq N)
- 1\leq C _ i\leq M\ (1\leq i\leq N)
- 1\leq F _ {i,1}\lt F _ {i,2}\lt\cdots\lt F _ {i,C _ i}\leq M\ (1\leq i\leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M P _ 1 C _ 1 F _ {1,1} F _ {1,2} \ldots F _ {1,C _ 1} P _ 2 C _ 2 F _ {2,1} F _ {2,2} \ldots F _ {2,C _ 2} \vdots P _ N C _ N F _ {N,1} F _ {N,2} \ldots F _ {N,C _ N}
Output
Print the answer in a single line.
Sample Input 1
5 6 10000 2 1 3 15000 3 1 2 4 30000 3 1 3 5 35000 2 1 5 100000 6 1 2 3 4 5 6
Sample Output 1
Yes
(i,j)=(4,3) satisfies all of the conditions.
No other pair satisfies them. For instance, for (i,j)=(4,5), the j-th product has all functions of the i-th one, but P _ i\lt P _ j, so it is not strictly superior.
Sample Input 2
4 4 3 1 1 3 1 2 3 1 2 4 2 2 3
Sample Output 2
No
Multiple products may have the same price and functions.
Sample Input 3
20 10 72036 3 3 4 9 7716 4 1 2 3 6 54093 5 1 6 7 8 10 25517 7 3 4 5 6 7 9 10 96930 8 2 3 4 6 7 8 9 10 47774 6 2 4 5 6 7 9 36959 5 1 3 4 5 8 46622 7 1 2 3 5 6 8 10 34315 9 1 3 4 5 6 7 8 9 10 54129 7 1 3 4 6 7 8 9 4274 5 2 4 7 9 10 16578 5 2 3 6 7 9 61809 4 1 2 4 5 1659 5 3 5 6 9 10 59183 5 1 2 3 4 9 22186 4 3 5 6 8 98282 4 1 4 7 10 72865 8 1 2 3 4 6 8 9 10 33796 6 1 3 5 7 9 10 74670 4 1 2 6 8
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
英小文字のみからなる N 個の文字列 S_1,S_2,\ldots,S_N が与えられます。
1 以上 N 以下の 相異なる 整数 i,j であって、S_i と S_j をこの順に連結した文字列が回文となるようなものが存在するか判定してください。
ただし、長さ M の文字列 T が回文であるとは、任意の 1\leq i\leq M について、T の i 文字目と (M+1-i) 文字目が一致していることをいいます。
制約
- 2\leq N\leq 100
- 1\leq \lvert S_i\rvert \leq 50
- N は整数
- S_i は英小文字のみからなる文字列
- S_i はすべて異なる。
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
問題文の条件をみたす i,j が存在するならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
5 ab ccef da a fe
出力例 1
Yes
(i,j)=(1,4) とすると、S_1=ab
と S_4=a
をこの順に連結した文字列は aba
となり、
これは回文であるため条件をみたしています。
よって、Yes
を出力します。
また、(i,j)=(5,2) としても、S_5=fe
と S_2=ccef
をこの順に連結した文字列は feccef
となり、やはり条件をみたしています。
入力例 2
3 a b aba
出力例 2
No
S_1, S_2, S_3 のうち、どの相異なる 2 つの文字列を繋げても回文となりません。
よって、No
を出力します。
問題文における i,j は相異なる必要があることに注意してください。
入力例 3
2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
出力例 3
Yes
Score : 200 points
Problem Statement
You are given N strings S_1,S_2,\ldots,S_N consisting of lowercase English letters.
Determine if there are distinct integers i and j between 1 and N, inclusive, such that the concatenation of S_i and S_j in this order is a palindrome.
A string T of length M is a palindrome if and only if the i-th character and the (M+1-i)-th character of T are the same for every 1\leq i\leq M.
Constraints
- 2\leq N\leq 100
- 1\leq \lvert S_i\rvert \leq 50
- N is an integer.
- S_i is a string consisting of lowercase English letters.
- All S_i are distinct.
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
If there are i and j that satisfy the condition in the problem statement, print Yes
; otherwise, print No
.
Sample Input 1
5 ab ccef da a fe
Sample Output 1
Yes
If we take (i,j)=(1,4), the concatenation of S_1=ab
and S_4=a
in this order is aba
, which is a palindrome, satisfying the condition.
Thus, print Yes
.
Here, we can also take (i,j)=(5,2), for which the concatenation of S_5=fe
and S_2=ccef
in this order is feccef
, satisfying the condition.
Sample Input 2
3 a b aba
Sample Output 2
No
No two distinct strings among S_1, S_2, and S_3 form a palindrome when concatenated.
Thus, print No
.
Note that the i and j in the statement must be distinct.
Sample Input 3
2 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
英小文字からなる長さ M の文字列 N 個 S_1,S_2,\dots,S_N が与えられます。ここで、S_i は互いに異なります。
これらを並び替えた文字列の列 T_1,T_2,\dots,T_N であって、以下の条件を満たすものが存在するか判定してください。
- 1 \le i \le N-1 を満たす全ての整数 i に対して、T_i を 1 文字だけ別の英小文字に変えて T_{i+1} にすることが出来る。
制約
- 2 \le N \le 8
- 1 \le M \le 5
- S_i は英小文字からなる長さ M の文字列である。(1 \le i \le N)
- S_i は互いに異なる。
入力
入力は以下の形式で標準入力から与えられる。
N M S_1 S_2 \vdots S_N
出力
問題文の条件を満たす列が存在するならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
4 4 bbed abcd abed fbed
出力例 1
Yes
abcd
, abed
, bbed
, fbed
の順に並び替えると条件を満たします。
入力例 2
2 5 abcde abced
出力例 2
No
どのように並び替えても条件を満たすことは出来ません。
入力例 3
8 4 fast face cast race fact rice nice case
出力例 3
Yes
Score : 250 points
Problem Statement
You are given N strings S_1,S_2,\dots,S_N, each of length M, consisting of lowercase English letter. Here, S_i are pairwise distinct.
Determine if one can rearrange these strings to obtain a new sequence of strings T_1,T_2,\dots,T_N such that:
- for all integers i such that 1 \le i \le N-1, one can alter exactly one character of T_i to another lowercase English letter to make it equal to T_{i+1}.
Constraints
- 2 \le N \le 8
- 1 \le M \le 5
- S_i is a string of length M consisting of lowercase English letters. (1 \le i \le N)
- S_i are pairwise distinct.
Input
The input is given from Standard Input in the following format:
N M S_1 S_2 \vdots S_N
Output
Print Yes
if one can obtain a conforming sequence; print No
otherwise.
Sample Input 1
4 4 bbed abcd abed fbed
Sample Output 1
Yes
One can rearrange them in this order: abcd
, abed
, bbed
, fbed
. This sequence satisfies the condition.
Sample Input 2
2 5 abcde abced
Sample Output 2
No
No matter how the strings are rearranged, the condition is never satisfied.
Sample Input 3
8 4 fast face cast race fact rice nice case
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 250 点
問題文
1,2,\dots,N がちょうど 3 回ずつ現れる長さ 3N の数列 A=(A_1,A_2,\dots,A_{3N}) が与えられます。
i=1,2,\dots,N について、A の中にある i のうち真ん中にあるものの添字を f(i) と定めます。 1,2,\dots,N を f(i) の昇順に並べ替えてください。
f(i) の定義は厳密には以下の通りです。
- A_j = i を満たす j が j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma) であるとする。このとき、f(i) = \beta である。
制約
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i=1,2,\dots,N それぞれについて、A の中に i はちょうど 3 回現れる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_{3N}
出力
1,2,\dots,N を f(i) の昇順に並べ替えてできる長さ N の数列を空白区切りで出力せよ。
入力例 1
3 1 1 3 2 3 2 2 3 1
出力例 1
1 3 2
- A の中にある 1 は A_1,A_2,A_9 なので、f(1) = 2 です。
- A の中にある 2 は A_4,A_6,A_7 なので、f(2) = 6 です。
- A の中にある 3 は A_3,A_5,A_8 なので、f(3) = 5 です。
よって、f(1) < f(3) < f(2) であるため 1,3,2 の順に出力します。
入力例 2
1 1 1 1
出力例 2
1
入力例 3
4 2 3 4 3 4 1 3 1 1 4 2 2
出力例 3
3 4 1 2
Score : 250 points
Problem Statement
You are given a sequence A=(A_1,A_2,\dots,A_{3N}) of length 3N where each of 1,2,\dots, and N occurs exactly three times.
For i=1,2,\dots,N, let f(i) be the index of the middle occurrence of i in A. Sort 1,2,\dots,N in ascending order of f(i).
Formally, f(i) is defined as follows.
- Suppose that those j such that A_j = i are j=\alpha,\beta,\gamma\ (\alpha < \beta < \gamma). Then, f(i) = \beta.
Constraints
- 1\leq N \leq 10^5
- 1 \leq A_j \leq N
- i occurs in A exactly three times, for each i=1,2,\dots,N.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_{3N}
Output
Print the sequence of length N obtained by sorting 1,2,\dots,N in ascending order of f(i), separated by spaces.
Sample Input 1
3 1 1 3 2 3 2 2 3 1
Sample Output 1
1 3 2
- 1 occurs in A at A_1,A_2,A_9, so f(1) = 2.
- 2 occurs in A at A_4,A_6,A_7, so f(2) = 6.
- 3 occurs in A at A_3,A_5,A_8, so f(3) = 5.
Thus, f(1) < f(3) < f(2), so 1,3, and 2 should be printed in this order.
Sample Input 2
1 1 1 1
Sample Output 2
1
Sample Input 3
4 2 3 4 3 4 1 3 1 1 4 2 2
Sample Output 3
3 4 1 2
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
文字列 S があり、初め S= 1
です。
以下の形式のクエリが Q 個与えられるので順に処理してください。
1 x
: S の末尾に数字 x を追加する2
: S の先頭の数字を削除する3
: S を十進数表記の数とみなした値を 998244353 で割った余りを出力する
制約
- 1 \leq Q \leq 6 \times 10^5
- 1 番目の形式のクエリについて、x \in \{1,2,3,4,5,6,7,8,9\}
- 2 番目の形式のクエリは S が 2 文字以上の時にのみ与えられる
- 3 番目の形式のクエリが 1 個以上存在する
入力
入力は以下の形式で標準入力から与えられる。
Q \mathrm{query}_1 \vdots \mathrm{query}_Q
ただし \mathrm{query}_i は i 番目のクエリを表し、以下のいずれかの形式である。
1 x
2
3
出力
3 番目の形式のクエリの個数を q として、q 行出力せよ。i (1 \leq i \leq q) 行目には i 番目の 3 番目の形式のクエリに対する出力をせよ。
入力例 1
3 3 1 2 3
出力例 1
1 12
1 番目のクエリにおいて、S は 1
なので ( 1 を 998244353 で割った余りに等しい) 1 を出力します。
2 番目のクエリにおいて、S は 12
になります。
3 番目のクエリにおいて、S は 12
なので ( 12 を 998244353 で割った余りに等しい) 12 を出力します。
入力例 2
3 1 5 2 3
出力例 2
5
入力例 3
11 1 9 1 9 1 8 1 2 1 4 1 4 1 3 1 5 1 3 2 3
出力例 3
0
出力されるべき値は 998244353 で割った余りであることに注意してください。
Score : 400 points
Problem Statement
We have a string S. Initially, S= 1
.
Process Q queries in the following formats in order.
1 x
: Append a digit x at the end of S.2
: Delete the digit at the beginning of S.3
: Print the number represented by S in decimal, modulo 998244353.
Constraints
- 1 \leq Q \leq 6 \times 10^5
- For each query in the first format, x \in \{1,2,3,4,5,6,7,8,9\}.
- A query in the second format is given only if S has a length of 2 or greater.
- There is at least one query in the third format.
Input
The input is given from Standard Input in the following format:
Q \mathrm{query}_1 \vdots \mathrm{query}_Q
Here, \mathrm{query}_i denotes the i-th query, which is in one of the following formats:
1 x
2
3
Output
Print q lines, where q is the number of queries in the third format. The i-th line (1 \leq i \leq q) should correspond to the i-th query in the third format.
Sample Input 1
3 3 1 2 3
Sample Output 1
1 12
In the first query, S is 1
, so you should print 1 modulo 998244353, that is, 1.
In the second query, S becomes 12
.
In the third query, S is 12
, so you should print 12 modulo 998244353, that is, 12.
Sample Input 2
3 1 5 2 3
Sample Output 2
5
Sample Input 3
11 1 9 1 9 1 8 1 2 1 4 1 4 1 3 1 5 1 3 2 3
Sample Output 3
0
Be sure to print numbers modulo 998244353.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の単純有向グラフが与えられます。辺 i は頂点 u_i から頂点 v_i への有向辺です。
また、あなたは次の操作を 0 回以上何度でも行えます。
- 相異なる頂点 x,y であって頂点 x から頂点 y への有向辺が存在しないようなものを選ぶ。そして、頂点 x から頂点 y への有向辺を追加する。
このグラフが次の条件を満たす状態にするために最小で何回操作を行う必要があるかを求めてください。
- 相異なる頂点 a,b,c すべてについて、頂点 a から頂点 b への有向辺と頂点 b から頂点 c への有向辺がともに存在するならば頂点 a から頂点 c への有向辺も存在する。
制約
- 3 \leq N \leq 2000
- 0 \leq M \leq 2000
- 1 \leq u_i ,v_i \leq N
- u_i \neq v_i
- i \neq j ならば (u_i,v_i) \neq (u_j,v_j)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 \vdots u_M v_M
出力
答えを出力せよ。
入力例 1
4 3 2 4 3 1 4 3
出力例 1
3
初め、一例として頂点 2,4,3 について、頂点 2 から頂点 4 への有向辺と頂点 4 から頂点 3 への有向辺がともに存在するにもかかわらず、頂点 2 から頂点 3 への有向辺は存在せず、条件を満たさない状態です。
そこで、以下の 3 本の有向辺を追加すると条件を満たす状態になります。
- 頂点 2 から頂点 3 への有向辺
- 頂点 2 から頂点 1 への有向辺
- 頂点 4 から頂点 1 への有向辺
一方、3 本未満の追加で条件を満たす状態には出来ないため、答えは 3 です。
入力例 2
292 0
出力例 2
0
入力例 3
5 8 1 2 2 1 1 3 3 1 1 4 4 1 1 5 5 1
出力例 3
12
Score : 500 points
Problem Statement
You are given a simple directed graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i is a directed edge from vertex u_i to vertex v_i.
You may perform the following operation zero or more times.
- Choose distinct vertices x and y such that there is no directed edge from vertex x to vertex y, and add a directed edge from vertex x to vertex y.
Find the minimum number of times you need to perform the operation to make the graph satisfy the following condition.
- For every triple of distinct vertices a, b, and c, if there are directed edges from vertex a to vertex b and from vertex b to vertex c, there is also a directed edge from vertex a to vertex c.
Constraints
- 3 \leq N \leq 2000
- 0 \leq M \leq 2000
- 1 \leq u_i ,v_i \leq N
- u_i \neq v_i
- (u_i,v_i) \neq (u_j,v_j) if i \neq j.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 \vdots u_M v_M
Output
Print the answer.
Sample Input 1
4 3 2 4 3 1 4 3
Sample Output 1
3
Initially, the condition is not satisfied because, for instance, for vertices 2, 4, and 3, there are directed edges from vertex 2 to vertex 4 and from vertex 4 to vertex 3, but not from vertex 2 to vertex 3.
You can make the graph satisfy the condition by adding the following three directed edges:
- one from vertex 2 to vertex 3,
- one from vertex 2 to vertex 1, and
- one from vertex 4 to vertex 1.
On the other hand, the condition cannot be satisfied by adding two or fewer edges, so the answer is 3.
Sample Input 2
292 0
Sample Output 2
0
Sample Input 3
5 8 1 2 2 1 1 3 3 1 1 4 4 1 1 5 5 1
Sample Output 3
12
実行時間制限: 2 sec / メモリ制限: 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