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

#GL0000252. Tổng ước chung lớn nhất của các cặp (GCD - Extreme (II))

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng ước chung lớn nhất của các cặp (GCD - Extreme (II))

GCD - Extrem

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a positive integer NN. Consider every pair of integers (i,j)(i,j) satisfying 1≤i<j≤N1\le i<j\le N. For each pair, compute gcd⁡(i,j)\gcd(i,j), the greatest positive integer dividing both values.

Find the sum of these greatest common divisors over all pairs. Each pair of distinct values is counted exactly once, with the smaller value written first:

G(N)=∑i=1N−1∑j=i+1Ngcd⁡(i,j).G(N)=\sum_{i=1}^{N-1}\sum_{j=i+1}^N\gcd(i,j).

For example, when N=4N=4, the six pairs are (1,2)(1,2), (1,3)(1,3), (1,4)(1,4), (2,3)(2,3), (2,4)(2,4), and (3,4)(3,4). Their contributions sum to 1+1+1+1+2+1=71+1+1+1+2+1=7.

Input

  • A single line containing the integer NN, defining the set of integers from 11 through NN to consider.

Output

  • Print one integer: G(N)G(N). The answer is guaranteed to fit in a signed 64-bit integer.

Subtasks

  • Subtask 1 (20 points): 2≤N≤5002\le N\le 500.
  • Subtask 2 (30 points): 2≤N≤100 0002\le N\le 100\,000.
  • Subtask 3 (50 points): 2≤N≤4 000 0002\le N\le 4\,000\,000.

Examples

Example 1

Input

10

Output

67

Explanation

Fix the larger number jj and sum gcd⁡(i,j)\gcd(i,j) over all 1≤i<j1\le i<j. For j=2,3,…,10j=2,3,\ldots,10, the individual sums are 1,2,4,4,9,6,12,12,171,2,4,4,9,6,12,12,17.

Thus, G(10)=1+2+4+4+9+6+12+12+17=67G(10)=1+2+4+4+9+6+12+12+17=67. No pair is counted twice because i<ji<j.

Example 2

Input

4

Output

7

Explanation

The valid pairs are (1,2)(1,2), (1,3)(1,3), (1,4)(1,4), (2,3)(2,3), (2,4)(2,4), and (3,4)(3,4). Their greatest common divisors are 1,1,1,1,2,11,1,1,1,2,1, respectively. Their sum is 77.