CF BUDDY
← Problems·

1334E · Divisor Paths

2200 · combinatorics, graphs, greedy

Problem: You are given a positive integer DD. Let's build the following graph from it:

  • each vertex is a divisor of DD (not necessarily prime, 11 and DD itself are also included);
  • two vertices xx and yy (x>yx > y) have an undirected edge between them if xx is divisible by yy and xy\frac x y is a prime;
  • the weight of an edge is the number of divisors of xx that are not divisors of yy.

For example, here is the graph for D=12D=12:

Edge (4,12)(4,12) has weight 33 because 1212 has divisors [1,2,3,4,6,12][1,2,3,4,6,12] and 44 has divisors [1,2,4][1,2,4]. Thus, there are 33 divisors of 1212 that are not divisors of 44[3,6,12][3,6,12].

There is no edge between 33 and 22 because 33 is not divisible by 22. There is no edge between 1212 and 33 because 123=4\frac{12}{3}=4 is not a prime.

Let the length of the path between some vertices vv and uu in the graph be the total weight of edges on it. For example, path [(1,2),(2,6),(6,12),(12,4),(4,2),(2,6)][(1, 2), (2, 6), (6, 12), (12, 4), (4, 2), (2, 6)] has length 1+2+2+3+1+2=111+2+2+3+1+2=11. The empty path has length 00.

So the shortest path between two vertices vv and uu is the path that has the minimal possible length.

Two paths aa and bb are different if there is either a different number of edges in them or there is a position ii such that aia_i and bib_i are different edges.

You are given qq queries of the following form:

  • vv uu — calculate the number of the shortest paths between vertices vv and uu.

The answer for each query might be large so print it modulo 998244353998244353.

Input Format: The first line contains a single integer DD (1D10151 \le D \le 10^{15}) — the number the graph is built from.

The second line contains a single integer qq (1q31051 \le q \le 3 \cdot 10^5) — the number of queries.

Each of the next qq lines contains two integers vv and uu (1v,uD1 \le v, u \le D). It is guaranteed that DD is divisible by both vv and uu (both vv and uu are divisors of DD).

Output Format: Print qq integers — for each query output the number of the shortest paths between the two given vertices modulo 998244353998244353.

Note: In the first example:

  • The first query is only the empty path — length 00;
  • The second query are paths [(12,4),(4,2),(2,1)][(12, 4), (4, 2), (2, 1)] (length 3+1+1=53+1+1=5), [(12,6),(6,2),(2,1)][(12, 6), (6, 2), (2, 1)] (length 2+2+1=52+2+1=5) and [(12,6),(6,3),(3,1)][(12, 6), (6, 3), (3, 1)] (length 2+2+1=52+2+1=5).
  • The third query is only the path [(3,1),(1,2),(2,4)][(3, 1), (1, 2), (2, 4)] (length 1+1+1=31+1+1=3).

Sample Cases

Case 1

Input

12
3
4 4
12 1
3 4

Output

1
3
1

Case 2

Input

1
1
1 1

Output

1

Case 3

Input

288807105787200
4
46 482955026400
12556830686400 897
414 12556830686400
4443186242880 325

Output

547558588
277147129
457421435
702277623

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback