#P14079. [GESP202509 八级] 最短距离
[GESP202509 八级] 最短距离
Background
Corresponding multiple-choice and true/false questions: https://ti.luogu.com.cn/problemset/1196
Problem Description
Given positive integers and the constant , build a weighted undirected graph with vertices numbered . For every pair with , add an undirected edge connecting vertex and vertex . The weight of the edge depends on whether and are coprime:
- If and are coprime (that is, the greatest common divisor of and is ), the undirected edge connecting vertex and vertex has length ;
- Otherwise, the undirected edge connecting vertex and vertex has length .
Now queries are given. The -th query () gives two positive integers . You need to answer the shortest distance between vertex and vertex .
Input Format
The first line contains three positive integers , 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 lines follow, each containing two positive integers , a query.
Output Format
Output lines in total, each containing one integer, the shortest distance between vertex and vertex .
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 of the test files, and .
For another of the test files, .
For all test files, , , and .
Translated by QwenWork