PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000369. Đường đi ngắn nhất trên đồ thị GCD ẩn (Shortest GCD Paths)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đường đi ngắn nhất trên đồ thị GCD ẩn (Shortest GCD Paths)

Shortest GCD Paths

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given integers n,a,bn,a,b, consider a complete undirected graph on vertices 1,2,…,n1,2,\ldots,n: every pair of distinct vertices u,vu,v is joined by an edge with weight

w(u,v)=max⁡(u,v)gcd⁡(u,v).w(u,v)=\frac{\max(u,v)}{\gcd(u,v)}.

This is a positive integer. Find the minimum total edge weight of a path from vertex aa to vertex bb. Output the minimum cost only, not the path itself.

Important: nn can be as large as 10910^9, so explicitly building this graph is impossible.

Input

A single line containing n,a,bn,a,b.

Output

Print one integer: the shortest-path distance from aa to bb.

Subtasks

  • Subtask 1 (20 points): 2≤n≤702\le n\le70; 1≤a,b≤n1\le a,b\le n; a≠ba\ne b.
  • Subtask 2 (30 points): 2≤n≤1062\le n\le10^6; 1≤a,b≤n1\le a,b\le n; a≠ba\ne b.
  • Subtask 3 (50 points): 2≤n≤1092\le n\le10^9; 1≤a,b≤n1\le a,b\le n; a≠ba\ne b.

Examples

Example 1

Input

10 9 8

Output

7

Explanation

The direct edge 9→89\to8 costs 99. The path 9→6→89\to6\to8 costs 9/3+8/2=3+4=79/3+8/2=3+4=7, which is optimal.

Example 2

Input

100 16 27

Output

10

Explanation

Path 16→12→18→2716\to12\to18\to27 has edge costs 16/4=416/4=4, 18/6=318/6=3, and 27/9=327/9=3. The total is 1010, the minimum possible.

Example 3

Input

100 55 11

Output

5

Explanation

The direct edge 55→1155\to11 costs 55/gcd⁡(55,11)=555/\gcd(55,11)=5, which is optimal.