#P10091. [ROIR 2022] 分数排序 (Day 2)

    ID: 10747 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>数学二分2022Special Judge排序其它技巧ROIR(俄罗斯)

[ROIR 2022] 分数排序 (Day 2)

Background

Translated from ROIR 2022 D2T2.

Problem Description

There are two sequences A=[a1,a2,,an]A = [a_1, a_2, \dots , a_n] and B=[b1,b2,,bn]B = [b_1, b_2, \dots , b_n], each consisting of nn distinct integers. Combine them into n2n^2 fractions of the form aibj\frac{a_i}{b_j}, reduce each fraction to simplest form, and then sort them in increasing order.

You are given a number qq and qq integers c1,c2,,cqc_1, c_2, \dots , c_q. For each cic_i, output the cic_i-th smallest fraction among the n2n^2 fractions described above.

Input Format

The first line contains two integers nn and qq.

The second line contains nn distinct integers a1,a2,,ana_1, a_2, \dots, a_n.

The third line contains nn distinct integers b1,b2,,bnb_1, b_2, \dots, b_n.

The fourth line contains qq distinct integers c1,c2,,cqc_1, c_2, \dots, c_q.

Output Format

Output qq lines. The ii-th line should output the cic_i-th fraction in the increasing order. A fraction pq\frac{p}{q} should be printed in the format p q, and it must be in simplest form.

4 8
3 4 1 2
2 3 4 5
1 16 2 4 5 6 10 15
1 5
2 1
1 4
2 5
1 2
1 2
4 5
3 2

Hint

In the sample, the initial list of fractions is:

$$\left[ \frac{3}{2}, \frac{3}{3}, \frac{3}{4}, \frac{3}{5}, \frac{4}{2}, \frac{4}{3}, \frac{4}{4}, \frac{4}{5}, \frac{1}{2}, \frac{1}{3}, \frac{1}{4}, \frac{1}{5}, \frac{2}{2}, \frac{2}{3}, \frac{2}{4}, \frac{2}{5} \right],$$

After reducing, we get:

$$\left[ \frac{3}{2}, \frac{1}{1}, \frac{3}{4}, \frac{3}{5}, \frac{2}{1}, \frac{4}{3}, \frac{1}{1}, \frac{4}{5}, \frac{1}{2}, \frac{1}{3}, \frac{1}{4}, \frac{1}{5}, \frac{1}{1}, \frac{2}{3}, \frac{1}{2}, \frac{2}{5} \right],$$

Finally, after sorting in increasing order, we get:

$$\left[ \frac{1}{5}, \frac{1}{4}, \frac{1}{3}, \frac{2}{5}, \frac{1}{2}, \frac{1}{2}, \frac{3}{5}, \frac{2}{3}, \frac{3}{4}, \frac{4}{5}, \frac{1}{1}, \frac{1}{1}, \frac{1}{1}, \frac{4}{3}, \frac{3}{2}, \frac{2}{1} \right].$$

This problem uses bundled testdata.

Subtask Score Special Properties
11 1414 n50n\le50
22 1313 n500n\le500
33 1515 q,ci100q,c_i\le100
44 2121 ci105c_i\le10^5
55 3737

For 100%100\% of the testdata, 1n1051 \leq n \leq 10^5, 1q1051 \leq q \leq 10^5, and qn2,n×q105q \leq n^2, n \times q \leq 10^5 (so in fact q10001032154q \le 1000\sqrt[3]{10}\approx2154), 1ai,bi1061 \leq a_i, b_i \leq 10^6, 1cin21 \leq c_i \leq n^2.

Translated by ChatGPT 5