#P9484. 「LAOI-1」GCD

    ID: 10124 远端评测题 1000ms 64MiB 尝试: 0 已通过: 0 难度: 3 上传者: 标签>搜索数学贪心数论洛谷原创O2优化最短路最大公约数 gcd

「LAOI-1」GCD

Problem Description

There is a graph with nn nodes, numbered 1,2,3,,n1,2,3,\dots,n. Node ii is connected to node jj by an undirected edge with weight ij|i-j| if and only if gcd(i,j)=i\gcd(i,j)=i and lcm(i,j)=j\operatorname{lcm}(i,j)=j. Now there are qq queries. For each query, find the shortest path from xx to yy.

Input Format

The first line contains an integer TT, meaning the number of test cases.

For each test case, the first line contains two positive integers n,qn,q, meaning the number of nodes and the number of queries.

The next qq lines each contain two positive integers x,yx,y, meaning the start node and the end node.

Output Format

For each query, output one positive integer. Separate adjacent outputs with a newline.

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

Hint

Pay attention to the time and memory limits. This problem is not bundled.

For 40%40\% of the testdata, T,n,q100T,n,q\le100.

For 100%100\% of the testdata, 1T1061\le T\le10^6, 1n,q1061\le n,q\le10^6, 1x,yn1\le x,y\le n, and 1n,q1061\le \sum n,\sum q\le10^6.

Please use faster I/O methods.

updata on 2024/8/8:

The time limit has been increased to 1000 ms. /yun.

Translated by ChatGPT 5