「LAOI-1」GCD

Luogu
IDLGP9484
Time1000ms
Memory64MB
DifficultyP2
搜索数学贪心数论洛谷原创O2优化最短路最大公约数 gcd
一张图有 $n$ 个节点,编号为 $1,2,3,\dots,n$。其中 $i$ 号节点会向 $j$ 号节点连一条边权为 $|i-j|$ 的无向边,当且仅当 $\gcd(i,j)=i,\operatorname{lcm}(i,j)=j$ 时连边。现询问 $q$ 次,每次询问求 $x$ 到 $y$ 的最短路径。 ## Input 第一行一个 $T$,表示数据组数。 每组数据的第一行两个正整数 $n,q$,表示节点数和询问次数。 接下来 $q$ 行,每行两个正整数 $x,y$,表示起点和终点。 ## Output 对于每组询问,输出一个正整数。相邻两个输出以换行符隔开。 [samples] ## Note 注意时空限制,本题不捆绑。 对于 $40\%$ 的数据,$T,n,q\le100$; 对于 $100\%$ 的数据,$1\le T\le10^6$,$1\le n,q\le10^6$,$1\le x,y\le n$,$1\le \sum n,\sum q\le10^6$。 **请使用更快的 IO 方式**。 updata on 2024/8/8: 时限上调到 1000ms。/yun
Samples
Input #1
1
6 4
1 4
3 5
2 5
2 4
Output #1
3
6
5
2
API Response (JSON)
{
  "problem": {
    "name": "「LAOI-1」GCD",
    "description": {
      "content": "一张图有 $n$ 个节点,编号为 $1,2,3,\\dots,n$。其中 $i$ 号节点会向 $j$ 号节点连一条边权为 $|i-j|$ 的无向边,当且仅当 $\\gcd(i,j)=i,\\operatorname{lcm}(i,j)=j$ 时连边。现询问 $q$ 次,每次询问求 $x$ 到 $y$ 的最短路径。",
      "description_type": "Markdown"
    },
    "platform": "Luogu",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 65536
    },
    "difficulty": {
      "LuoguStyle": "P2"
    },
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "LGP9484"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "一张图有 $n$ 个节点,编号为 $1,2,3,\\dots,n$。其中 $i$ 号节点会向 $j$ 号节点连一条边权为 $|i-j|$ 的无向边,当且仅当 $\\gcd(i,j)=i,\\operatorname{lcm}(i,j)=j$ 时连边。现询问 $q$ 次,每次询问求 $x$ 到 $y$ 的最短路径。\n\n## Input\n\n第一行一个 $T$,表示数据组数。\n\n每组数据的第一行两个正整数 $...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments