Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
英小文字および .
からなる文字列 S が与えられます。
S から .
を全て削除した文字列を求めてください。
制約
- S は英小文字および
.
からなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S から .
を全て削除した文字列を出力せよ。
入力例 1
.v.
出力例 1
v
.v.
から .
を全て削除すると v
になります。したがって、 v
を出力してください。
入力例 2
chokudai
出力例 2
chokudai
S に .
が含まれない場合もあります。
入力例 3
...
出力例 3
S の文字が全て .
である場合もあります。
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters and .
.
Find the string obtained by removing all .
from S.
Constraints
- S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters and
.
.
Input
The input is given from Standard Input in the following format:
S
Output
Print the string obtained by removing all .
from S.
Sample Input 1
.v.
Sample Output 1
v
Removing all .
from .v.
yields v
, so print v
.
Sample Input 2
chokudai
Sample Output 2
chokudai
There are cases where S does not contain .
.
Sample Input 3
...
Sample Output 3
There are also cases where all characters in S are .
.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
正整数 M が与えられます。 以下の条件を全て満たす正整数 N と非負整数列 A=(A_1,A_2,\ldots,A_N) を一つ求めてください。
- 1\le N\le 20
- 0\le A_i\le 10 (1\le i\le N)
- \displaystyle \sum_{i=1}^N 3^{A_i}=M
ただし、制約下では条件を満たす N と A の組が必ず存在することが証明できます。
制約
- 1\le M\le 10^5
入力
入力は以下の形式で標準入力から与えられる。
M
出力
以下の形式で条件を満たす N と A を出力せよ。
N A_1 A_2 \ldots A_N
なお、条件を満たす N と A の組が複数存在する場合は、どれを出力しても正答となる。
入力例 1
6
出力例 1
2 1 1
N=2 、A=(1,1) とすると \displaystyle \sum_{i=1}^N 3^{A_i}=3+3=6 より全ての条件を満たします。
他に N=4 、 A=(0,0,1,0) なども条件を満たします。
入力例 2
100
出力例 2
4 2 0 2 4
入力例 3
59048
出力例 3
20 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
1\le N\le 20 という制約に注意してください。
Score : 200 points
Problem Statement
You are given a positive integer M. Find a positive integer N and a sequence of non-negative integers A = (A_1, A_2, \ldots, A_N) that satisfy all of the following conditions:
- 1 \le N \le 20
- 0 \le A_i \le 10 (1 \le i \le N)
- \displaystyle \sum_{i=1}^N 3^{A_i} = M
It can be proved that under the constraints, there always exists at least one such pair of N and A satisfying the conditions.
Constraints
- 1 \le M \le 10^5
Input
The input is given from Standard Input in the following format:
M
Output
Print N and A satisfying the conditions in the following format:
N A_1 A_2 \ldots A_N
If there are multiple valid pairs of N and A, any of them is acceptable.
Sample Input 1
6
Sample Output 1
2 1 1
For example, with N=2 and A=(1,1), we have \displaystyle \sum_{i=1}^N 3^{A_i} = 3+3=6, satisfying all conditions.
Another example is N=4 and A=(0,0,1,0), which also satisfies the conditions.
Sample Input 2
100
Sample Output 2
4 2 0 2 4
Sample Input 3
59048
Sample Output 3
20 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
Note the condition 1 \le N \le 20.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
長さ N の文字列 S が与えられます。クエリが Q 個与えられるので、順番に処理してください。
i 番目のクエリは以下の通りです。
- 整数 X_i と文字 C_i が与えられるので、 S の X_i 番目の文字を C_i に置き換える。その後、 S に文字列
ABC
が部分文字列として何箇所含まれるかを出力する。
ここで、 S の 部分文字列 とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。
例えば、ab
は abc
の部分文字列ですが、ac
は abc
の部分文字列ではありません。
制約
- 3\le N\le 2\times 10^5
- 1\le Q\le 2\times 10^5
- S は英大文字からなる長さ N の文字列
- 1\le X_i\le N
- C_i は英大文字
入力
入力は以下の形式で標準入力から与えられる。
N Q S X_1 C_1 X_2 C_2 \vdots X_Q C_Q
出力
Q 行出力せよ。 i 行目 (1\le i\le Q) には i 番目のクエリに対する答えを出力せよ。
入力例 1
7 4 ABCDABC 4 B 3 A 5 C 4 G
出力例 1
2 1 1 0
各クエリを処理した後の S は次のようになります。
- 1 つ目のクエリを処理後: S=
ABCBABC
となる。この中にABC
は部分文字列として 2 回登場する。 - 2 つ目のクエリを処理後: S=
ABABABC
となる。この中にABC
は部分文字列として 1 回登場する。 - 3 つ目のクエリを処理後: S=
ABABCBC
となる。この中にABC
は部分文字列として 1 回登場する。 - 4 つ目のクエリを処理後: S=
ABAGCBC
となる。この中にABC
は部分文字列として 0 回登場する。
入力例 2
3 3 ABC 1 A 2 B 3 C
出力例 2
1 1 1
クエリの処理前と処理後で S が変わらない場合もあります。
入力例 3
15 10 BBCCBCACCBACACA 9 C 11 B 5 B 11 B 4 A 8 C 8 B 5 B 7 B 14 B
出力例 3
0 0 0 0 1 1 2 2 1 1
Score : 350 points
Problem Statement
You are given a string S of length N. You are also given Q queries, which you should process in order.
The i-th query is as follows:
- Given an integer X_i and a character C_i, replace the X_i-th character of S with C_i. Then, print the number of times the string
ABC
appears as a substring in S.
Here, a substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S.
For example, ab
is a substring of abc
, but ac
is not a substring of abc
.
Constraints
- 3 \le N \le 2 \times 10^5
- 1 \le Q \le 2 \times 10^5
- S is a string of length N consisting of uppercase English letters.
- 1 \le X_i \le N
- C_i is an uppercase English letter.
Input
The input is given from Standard Input in the following format:
N Q S X_1 C_1 X_2 C_2 \vdots X_Q C_Q
Output
Print Q lines. The i-th line (1 \le i \le Q) should contain the answer to the i-th query.
Sample Input 1
7 4 ABCDABC 4 B 3 A 5 C 4 G
Sample Output 1
2 1 1 0
After processing each query, S becomes as follows.
- After the first query: S=
ABCBABC
. In this string,ABC
appears twice as a substring. - After the second query: S=
ABABABC
. In this string,ABC
appears once as a substring. - After the third query: S=
ABABCBC
. In this string,ABC
appears once as a substring. - After the fourth query: S=
ABAGCBC
. In this string,ABC
appears zero times as a substring.
Sample Input 2
3 3 ABC 1 A 2 B 3 C
Sample Output 2
1 1 1
There are cases where S does not change through processing a query.
Sample Input 3
15 10 BBCCBCACCBACACA 9 C 11 B 5 B 11 B 4 A 8 C 8 B 5 B 7 B 14 B
Sample Output 3
0 0 0 0 1 1 2 2 1 1
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
ビル 1, ビル 2, \ldots, ビル N の N 棟のビルがこの順で一列に並んでいます。ビル i\ (1\leq i\leq N) の高さは H_i です。
各 i=1,2,\ldots,N について、次を満たす整数 j\ (i\lt j\leq N) の個数を求めてください。
- ビル i とビル j の間にビル j より高いビルが存在しない。
制約
- 1\leq N\leq 2\times 10^5
- 1\leq H_i\leq N
- H_i\neq H_j\ (i\neq j)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N H_1 H_2 \ldots H_N
出力
各 i=1,2,\ldots,N に対して条件を満たす j の個数を c_i としたとき、c_1,c_2,\ldots,c_N をこの順で空白区切りで出力せよ。
入力例 1
5 2 1 4 3 5
出力例 1
3 2 2 1 0
i=1 について、条件を満たす j は 2,3,5 の 3 つです。(ビル 1 とビル 4 の間にはビル 4 より高いビル 3 が存在するため、j=4 は条件を満たしません。)よって、出力の 1 つ目は 3 になります。
入力例 2
4 1 2 3 4
出力例 2
3 2 1 0
入力例 3
10 1 9 6 5 2 7 10 4 8 3
出力例 3
2 3 3 3 2 1 2 1 1 0
Score : 400 points
Problem Statement
There are N buildings, Building 1, Building 2, \ldots, Building N, arranged in a line in this order. The height of Building i (1 \leq i \leq N) is H_i.
For each i = 1, 2, \ldots, N, find the number of integers j (i < j \leq N) satisfying the following condition:
- There is no building taller than Building j between Buildings i and j.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq H_i \leq N
- H_i\neq H_j\ (i\neq j)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H_1 H_2 \ldots H_N
Output
For each i = 1, 2, \ldots, N, let c_i be the number of j satisfying the condition. Print c_1, c_2, \ldots, c_N in order, separated by spaces.
Sample Input 1
5 2 1 4 3 5
Sample Output 1
3 2 2 1 0
For i=1, the integers j satisfying the condition are 2, 3, and 5: there are three. (Between Buildings 1 and 4, there is a building taller than Building 4, which is Building 3, so j=4 does not satisfy the condition.) Therefore, the first number in the output is 3.
Sample Input 2
4 1 2 3 4
Sample Output 2
3 2 1 0
Sample Input 3
10 1 9 6 5 2 7 10 4 8 3
Sample Output 3
2 3 3 3 2 1 2 1 1 0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
N 頂点 0 辺の無向グラフがあります。頂点には 1 から N の番号がつけられています。
Q 個のクエリが与えられるので、与えられた順に処理してください。各クエリは以下の 2 種類のいずれかです。
- タイプ 1 :
1 u v
の形式で与えられる。頂点 u と頂点 v の間に辺を追加する。 - タイプ 2 :
2 v k
の形式で与えられる。頂点 v と連結な頂点の中で、k 番目に頂点番号が大きいものを出力する。ただし、頂点 v と連結な頂点が k 個未満のときは-1
を出力する。
制約
- 1\leq N,Q\leq 2\times 10^5
- タイプ 1 のクエリにおいて、1\leq u\lt v\leq N
- タイプ 2 のクエリにおいて、1\leq v\leq N, 1\leq k\leq 10
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
ただし、\mathrm{query}_i は i 個目のクエリであり、以下のいずれかの形式で与えられる。
1 u v
2 v k
出力
タイプ 2 のクエリの個数を q として、q 行出力せよ。i 行目には i 個目のタイプ 2 に対するクエリの答えを出力せよ。
入力例 1
4 10 1 1 2 2 1 1 2 1 2 2 1 3 1 1 3 1 2 3 1 3 4 2 1 1 2 1 3 2 1 5
出力例 1
2 1 -1 4 2 -1
- 1 個目のクエリについて、頂点 1 と頂点 2 の間に辺が追加されます。
- 2 個目のクエリについて、頂点 1 と連結な頂点は 1,2 の 2 つです。この中で 1 番目に大きい 2 を出力します。
- 3 個目のクエリについて、頂点 1 と連結な頂点は 1,2 の 2 つです。この中で 2 番目に大きい 1 を出力します。
- 4 個目のクエリについて、頂点 1 と連結な頂点は 1,2 の 2 つです。3 個未満なので
-1
を出力します。 - 5 個目のクエリについて、頂点 1 と頂点 3 の間に辺が追加されます。
- 6 個目のクエリについて、頂点 2 と頂点 3 の間に辺が追加されます。
- 7 個目のクエリについて、頂点 3 と頂点 4 の間に辺が追加されます。
- 8 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,4 の 4 つです。この中で 1 番目に大きい 4 を出力します。
- 9 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,4 の 4 つです。この中で 3 番目に大きい 2 を出力します。
- 10 個目のクエリについて、頂点 1 と連結な頂点は 1,2,3,4 の 4 つです。5 個未満なので
-1
を出力します。
入力例 2
6 20 1 3 4 1 3 5 2 1 1 2 3 1 1 1 5 2 6 9 2 1 3 2 6 1 1 4 6 2 2 1 2 6 2 2 4 7 1 1 4 2 6 2 2 3 4 1 2 5 2 4 1 1 1 6 2 3 3 2 1 3
出力例 2
1 5 -1 3 6 2 5 -1 5 3 6 4 4
Score : 475 points
Problem Statement
There is an undirected graph with N vertices and 0 edges. The vertices are numbered 1 to N.
You are given Q queries to process in order. Each query is of one of the following two types:
- Type 1: Given in the format
1 u v
. Add an edge between vertices u and v. - Type 2: Given in the format
2 v k
. Print the k-th largest vertex number among the vertices connected to vertex v. If there are fewer than k vertices connected to v, print-1
.
Constraints
- 1 \leq N, Q \leq 2 \times 10^5
- In a Type 1 query, 1 \leq u < v \leq N.
- In a Type 2 query, 1 \leq v \leq N, 1 \leq k \leq 10.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q \mathrm{query}_1 \mathrm{query}_2 \vdots \mathrm{query}_Q
Here, \mathrm{query}_i is the i-th query and is given in one of the following formats:
1 u v
2 v k
Output
Let q be the number of Type 2 queries. Print q lines. The i-th line should contain the answer to the i-th Type 2 query.
Sample Input 1
4 10 1 1 2 2 1 1 2 1 2 2 1 3 1 1 3 1 2 3 1 3 4 2 1 1 2 1 3 2 1 5
Sample Output 1
2 1 -1 4 2 -1
- In the first query, an edge is added between vertices 1 and 2.
- In the second query, two vertices are connected to vertex 1: 1 and 2. Among them, the 1-st largest vertex number is 2, which should be printed.
- In the third query, two vertices are connected to vertex 1: 1 and 2. Among them, the 2-nd largest vertex number is 1, which should be printed.
- In the fourth query, two vertices are connected to vertex 1: 1 and 2, which is fewer than 3, so print
-1
. - In the fifth query, an edge is added between vertices 1 and 3.
- In the sixth query, an edge is added between vertices 2 and 3.
- In the seventh query, an edge is added between vertices 3 and 4.
- In the eighth query, four vertices are connected to vertex 1: 1,2,3,4. Among them, the 1-st largest vertex number is 4, which should be printed.
- In the ninth query, four vertices are connected to vertex 1: 1,2,3,4. Among them, the 3-rd largest vertex number is 2, which should be printed.
- In the tenth query, four vertices are connected to vertex 1: 1,2,3,4, which is fewer than 5, so print
-1
.
Sample Input 2
6 20 1 3 4 1 3 5 2 1 1 2 3 1 1 1 5 2 6 9 2 1 3 2 6 1 1 4 6 2 2 1 2 6 2 2 4 7 1 1 4 2 6 2 2 3 4 1 2 5 2 4 1 1 1 6 2 3 3 2 1 3
Sample Output 2
1 5 -1 3 6 2 5 -1 5 3 6 4 4
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
N 頂点 N+M 辺の単純有向グラフ G があります。頂点には 1 から N の番号が、辺には 1 から N+M までの番号がつけられています。
辺 i (1 \leq i \leq N) は頂点 i から頂点 i+1 へ張られています。(ただし、頂点 N+1 は頂点 1 とみなします。)
辺 N+i\ (1\leq i\leq M) は頂点 X_i から頂点 Y_i へ張られています。
高橋君は頂点 1 にいます。各頂点において、高橋君はその頂点から有向辺が張られている頂点に移動することができます。
高橋君がちょうど K 回移動する方法が何通りあるかを求めてください。
すなわち、長さ K+1 の 整数列 (v_0,v_1,\dots,v_K) であって、下記の 3 つの条件をすべて満たすものの個数を求めてください。
- i=0,1,\dots,K について、1\leq v_i\leq N
- v_0=1
- i=1,2,\ldots,K について、頂点 v_{i-1} から頂点 v_i へ有向辺が張られている。
ただし、答えは非常に大きくなることがあるので、答えを 998244353 で割った余りを出力してください。
制約
- 2\leq N\leq 2\times 10^5
- 0\leq M\leq 50
- 1\leq K\leq 2\times 10^5
- 1\leq X_i,Y_i\leq N,X_i\neq Y_i
- N+M 本の有向辺はすべて異なる
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M K X_1 Y_1 X_2 Y_2 \vdots X_M Y_M
出力
答えを 998244353 で割った余りを出力せよ。
入力例 1
6 2 5 1 4 2 5
出力例 1
5
上図はグラフ G を表しています。以下の 5 通りの移動方法が存在します。
- 頂点 1\to 頂点 2\to 頂点 3\to 頂点 4\to 頂点 5\to 頂点 6
- 頂点 1\to 頂点 2\to 頂点 5\to 頂点 6\to 頂点 1\to 頂点 2
- 頂点 1\to 頂点 2\to 頂点 5\to 頂点 6\to 頂点 1\to 頂点 4
- 頂点 1\to 頂点 4\to 頂点 5\to 頂点 6\to 頂点 1\to 頂点 2
- 頂点 1\to 頂点 4\to 頂点 5\to 頂点 6\to 頂点 1\to 頂点 4
入力例 2
10 0 200000
出力例 2
1
入力例 3
199 10 1326 122 39 142 49 164 119 197 127 188 145 69 80 6 120 24 160 18 154 185 27
出力例 3
451022766
Score : 525 points
Problem Statement
There is a simple directed graph G with N vertices and N+M edges. The vertices are numbered 1 to N, and the edges are numbered 1 to N+M.
Edge i (1 \leq i \leq N) goes from vertex i to vertex i+1. (Here, vertex N+1 is considered as vertex 1.)
Edge N+i (1 \leq i \leq M) goes from vertex X_i to vertex Y_i.
Takahashi is at vertex 1. At each vertex, he can move to any vertex to which there is an outgoing edge from the current vertex.
Compute the number of ways he can move exactly K times.
That is, find the number of integer sequences (v_0, v_1, \dots, v_K) of length K+1 satisfying all of the following three conditions:
- 1 \leq v_i \leq N for i = 0, 1, \dots, K.
- v_0 = 1.
- There is a directed edge from vertex v_{i-1} to vertex v_i for i = 1, 2, \ldots, K.
Since this number can be very large, print it modulo 998244353.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 50
- 1 \leq K \leq 2 \times 10^5
- 1 \leq X_i, Y_i \leq N, X_i \neq Y_i
- All of the N+M directed edges are distinct.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M K X_1 Y_1 X_2 Y_2 \vdots X_M Y_M
Output
Print the count modulo 998244353.
Sample Input 1
6 2 5 1 4 2 5
Sample Output 1
5
The above figure represents the graph G. There are five ways for Takahashi to move:
- Vertex 1 \to Vertex 2 \to Vertex 3 \to Vertex 4 \to Vertex 5 \to Vertex 6
- Vertex 1 \to Vertex 2 \to Vertex 5 \to Vertex 6 \to Vertex 1 \to Vertex 2
- Vertex 1 \to Vertex 2 \to Vertex 5 \to Vertex 6 \to Vertex 1 \to Vertex 4
- Vertex 1 \to Vertex 4 \to Vertex 5 \to Vertex 6 \to Vertex 1 \to Vertex 2
- Vertex 1 \to Vertex 4 \to Vertex 5 \to Vertex 6 \to Vertex 1 \to Vertex 4
Sample Input 2
10 0 200000
Sample Output 2
1
Sample Input 3
199 10 1326 122 39 142 49 164 119 197 127 188 145 69 80 6 120 24 160 18 154 185 27
Sample Output 3
451022766
Time Limit: 3 sec / Memory Limit: 1024 MiB
配点 : 625 点
問題文
長さ 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\le i\le N に対して A_i\times x+B_i\times y < C_i が成立する。
なお、条件を満たす正整数の組の個数は有限個であることが証明できます。
T 個のテストケースが与えられるので、それぞれに対して答えを求めてください。
制約
- 1\le T\le 2\times 10^5
- 1\le N\le 2\times 10^5
- 1\le A_i,B_i,C_i\le 10^9
- 全てのテストケースにおける N の総和は 2\times 10^5 以下である
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで、\mathrm{case}_i は i 番目のテストケースを意味する。
T \mathrm{case}_1 \mathrm{case}_2 \vdots \mathrm{case}_T
各テストケースは以下の形式で与えられる。
N A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_N B_N C_N
出力
T 行出力せよ。 i 行目 (1\le i\le T) には \mathrm{case}_i に対する答えを出力せよ。
入力例 1
2 2 1 1 4 1 2 5 1 1 1 2
出力例 1
2 0
1 つ目のテストケースでは、条件を満たす正整数の組は (x,y)=(1,1),(2,1) の 2 つです。したがって、 1 行目には 2 を出力してください。
2 つ目のテストケースでは、条件を満たす正整数の組は存在しません。したがって、 2 行目には 0 を出力してください。
入力例 2
3 7 138 16011 918976 5478 7748 499926 5234 17727 748589 1157 10511 643136 31200 3005 721285 28839 14469 798851 1933 5378 864127 9 17775 1665 386430 37001 863 922418 9756 4182 746671 12379 9106 807578 3984 4049 640539 25333 9869 780810 20372 7000 688738 16107 11974 827227 10779 10531 770510 5 4916 14132 460944 11856 45422 610561 56014 18216 825793 10363 6220 945356 37418 33866 851593
出力例 2
660 995 140
Score : 625 points
Problem Statement
You are given three length-N sequences of positive integers: 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 number of pairs of positive integers (x, y) that satisfy the following condition:
- A_i \times x + B_i \times y < C_i for all 1 \leq i \leq N.
It can be proved that the number of such pairs of positive integers satisfying the condition is finite.
You are given T test cases, each of which should be solved.
Constraints
- 1 \leq T \leq 2 \times 10^5
- 1 \leq N \leq 2 \times 10^5
- 1 \leq A_i, B_i, C_i \leq 10^9
- The sum of N over all test cases is at most 2 \times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format. Here, \mathrm{case}_i refers to the i-th test case.
T \mathrm{case}_1 \mathrm{case}_2 \vdots \mathrm{case}_T
Each test case is given in the following format:
N A_1 B_1 C_1 A_2 B_2 C_2 \vdots A_N B_N C_N
Output
Print T lines. The i-th line (1 \leq i \leq T) should contain the answer for \mathrm{case}_i.
Sample Input 1
2 2 1 1 4 1 2 5 1 1 1 2
Sample Output 1
2 0
In the first test case, there are two valid pairs of integers: (x, y) = (1, 1), (2,1). Thus, the first line should contain 2.
In the second test case, there are no valid pairs of integers. Thus, the second line should contain 0.
Sample Input 2
3 7 138 16011 918976 5478 7748 499926 5234 17727 748589 1157 10511 643136 31200 3005 721285 28839 14469 798851 1933 5378 864127 9 17775 1665 386430 37001 863 922418 9756 4182 746671 12379 9106 807578 3984 4049 640539 25333 9869 780810 20372 7000 688738 16107 11974 827227 10779 10531 770510 5 4916 14132 460944 11856 45422 610561 56014 18216 825793 10363 6220 945356 37418 33866 851593
Sample Output 2
660 995 140