33OJ
登录 注册

#P14079. [GESP202509 八级] 最短距离

[GESP202509 八级] 最短距离

Background

Corresponding multiple-choice and true/false questions: https://ti.luogu.com.cn/problemset/1196

Problem Description

Given positive integers p,qp, q and the constant N=1018N=10^{18}, build a weighted undirected graph with NN vertices numbered 1,2,…,N1, 2, \ldots, N. For every pair u,vu, v with 1≤u<v≤N1 \le u < v \le N, add an undirected edge connecting vertex uu and vertex vv. The weight of the edge depends on whether uu and vv are coprime:

  • If uu and vv are coprime (that is, the greatest common divisor of uu and vv is 11), the undirected edge connecting vertex uu and vertex vv has length pp;
  • Otherwise, the undirected edge connecting vertex uu and vertex vv has length qq.

Now nn queries are given. The ii-th query (1≤i≤n1 \le i \le n) gives two positive integers ai,bia_i, b_i. You need to answer the shortest distance between vertex aia_i and vertex bib_i.

Input Format

The first line contains three positive integers n,p,qn, p, q, the number of queries, the edge weight when the vertex numbers are coprime, and the edge weight when the vertex numbers are not coprime.

Then nn lines follow, each containing two positive integers ai,bia_i, b_i, a query.

Output Format

Output nn lines in total, each containing one integer, the shortest distance between vertex aia_i and vertex bib_i.

4 4 3
1 2
2 3
4 2
3 5
4
4
3
4
5 2 6
1 2
2 3
4 2
3 5
6 6
2
2
4
2
0

Hint

For 30%30\% of the test files, 1≤n≤101 \le n \le 10 and 1≤ai,bi≤501 \le a_i, b_i \le 50.

For another 30%30\% of the test files, 1≤ai,bi≤2501 \le a_i, b_i \le 250.

For all test files, 1≤n≤1041 \le n \le 10^4, 1≤ai,bi≤1091 \le a_i, b_i \le 10^9, and 1≤p,q≤1091 \le p, q \le 10^9.

Translated by QwenWork