Longge`s problem#1128

Longge`s problem (Editorial)

Theorem. If the function is multiplicative, then the summatory function

S_f(n) = \sum_{d \mid n} f(d)

is also multiplicative.

Proof. Let x, y \in N, where and are coprime.

Let x_1, x_2, ..., x_k be all divisors of , and let y_1, y_2, ..., y_m be all divisors of .

Since and are coprime, any divisor x_i is coprime to any divisor y_j. Therefore, GCD(x_i, y_j) = 1. This means that the product x_i \cdot y_j is a divisor of the number x \cdot y.

Moreover, each product x_i \cdot y_j gives a distinct divisor of x \cdot y, and in this way we obtain all divisors of x \cdot y. Therefore, the sum over all divisors of x \cdot y can be represented as a double sum over all pairs of divisors x_i and y_j. Then

S_f(x) \cdot S_f(y) = \sum_{i=1}^{k} f(x_i) \cdot \sum_{j=1}^{m} f(y_j) = \sum_{i,j} f(x_i)f(y_j) = \sum_{i,j} f(x_i y_j) = S_f(xy)

Corollary. Consider the function f(n) = GCD(n, c), where is a constant. Let us show that this function is multiplicative.

If and are coprime, then

f(x \cdot y) = GCD(x \cdot y, c) = GCD(x, c) \cdot GCD(y, c) = f(x) \cdot f(y)

Therefore, the function f(n) = GCD(n, c) is multiplicative.

Example. Let us compute GCD(36, 90) using the multiplicativity property.

GCD(36, 90) = GCD(4 \cdot 9, 90) = GCD(4, 90) \cdot GCD(9, 90) = 2 \cdot 9 = 18

Now let us consider its summatory function:

g(n) = \sum_{i=1}^{n} \gcd(i,n)

From the previous theorem, we know that the summatory function of a multiplicative function is also multiplicative. Therefore, the function is also multiplicative.

If we factor into powers of prime numbers n = p_1^{a_1} p_2^{a_2}...p_k^{a_k}, then

g(n) = g(p_1^{a_1} p_2^{a_2}...p_k^{a_k}) = g(p_1^{a_1}) \cdot g(p_2^{a_2}) \cdot ... \cdot g(p_k^{a_k})

Lemma. Let be a divisor of . Then there are exactly integers such that 1 \le i \le n and GCD(i, n) = d.

► If GCD(i, n) = d, then divides . Let us represent in the form i = d \cdot j. Since divides , we can write as n = d \cdot (n / d). Then:

GCD(i, n) = GCD(d \cdot j, n) = GCD(d \cdot j, d \cdot (n / d)) = d \cdot GCD(j, n / d)

We are interested in those values of for which GCD(i, n) = d. Therefore, we must have d \cdot GCD(j, n / d) = d. Hence

GCD(j, n / d) = 1

The number of integers that are coprime to is .

Example. Consider the number n = 24 and find all integers for which GCD(i, 24) = 3. According to the lemma, the number of such integers is φ(24 / 3) = φ(8) = 4.

To find these integers, let i = 3 \cdot j. Then, from the proof of the lemma, we know that GCD(j, 8) = 1. This equality holds for j \in \{1, 3, 5, 7\}. Therefore, GCD(i, 24) = 3 for i \in \{3, 9, 15, 21\}.

Theorem (formula for the sum of GCDs in terms of Euler’s totient function). Let be a positive integer and

g(n) = \sum_{i=1}^{n} \gcd(i,n)

Then

g(n) = \sum_{d \mid n} d \varphi \left( \frac{n}{d} \right) = n \sum_{d \mid n} \frac{\varphi(d)}{d},

where the sum is taken over all positive divisors of , and is Euler's totient function

Proof. Consider all integers from to and group them according to the value of GCD(i, n).

Let GCD(i, n) = e. According to the lemma above, the number of such integers is exactly . Each such integer contributes the same value to the sum . Therefore, all integers for which GCD(i, n) = e contribute a total of e \cdot φ(n / e).

The value is always a divisor of . Therefore, to obtain the entire sum , we need to sum these contributions over all divisors of :

g(n) = \sum_{e \mid n} e \varphi\left(\frac{n}{e}\right)

Let us make a change of variable: d = n / e. Then e = n / d. Since is a divisor of is also a divisor of . Substituting e = n / d into the obtained formula, we get:

g(n) = \sum_{e \mid n} e \varphi\left(\frac{n}{e}\right) = \sum_{d \mid n} \frac{n}{d}\varphi(d) = n \sum_{d \mid n} \frac{\varphi(d)}{d}

Theorem. Let

g(n) = \sum_{i=1}^{n} \gcd(i,n)

Then, for any prime and positive integer , the following relation holds:

g(p^a) = (a + 1)p^a - ap^{a–1}

► For a = 1, we have:

g(p) = GCD(1, p) + GCD(2, p) + ... + GCD(p, p) = (p - 1) + p = 2p - 1

For a = 2, we have:

= (1 + 1 + … + 1 + p) + \\ (1 + 1 + … + 1 + p) + \\ ... \\ (1 + 1 + … + 1 + p^2) =
= (p - 1 + p) \cdot (p - 1) + (p - 1 + p^2) = \\ (2p - 1) \cdot (p - 1) + (p^2 + p - 1) = \\ 2p^2 - 2p - p + 1 + (p^2 + p - 1) = \\ = 3p^2 - 2p

Let us compute g(p^a). The value of GCD(i, p^a) can only be a divisor of p^a. Since is a prime number, all divisors of p^a have the form p^k, where 0 \le k \le a.

Let us find the number of integers from to p^a for which GCD(i, p^a) = p^k. According to the lemma given above, this number is equal to

φ(p^{a - k}) = p^{a - k} - p^{a - k - 1} (the~formula~is~valid~for~a > k)

In addition, for i = p^a we have: GCD(i, p^a) = GCD(p^a, p^a) = p^a.

Therefore

g(p^a) = \sum_{k=0}^{a-1} p^k \varphi\left(p^{a-k}\right) + p^a = \sum_{k=0}^{a-1} p^k \left(p^{a-k} - p^{a-k-1}\right) + p^a \\ = \sum_{k=0}^{a-1} \left(p^a - p^{a-1}\right) + p^a = a\left(p^a - p^{a-1}\right) + p^a = (a+1)p^a - ap^{a-1}

Example. Let n = 6.

Then

g(6) = \sum_{i=1}^{6} \gcd(i,6) = \\ = GCD(1, 6) + GCD(2, 6) + GCD(3, 6) + GCD(4, 6) + GCD(5, 6) + GCD(6, 6) = \\ = 1 + 2 + 3 + 2 + 1 + 6 = 15

At the same time

g(6) = g(2) \cdot g(3) = \\ (GCD(1, 2) + GCD(2, 2)) \cdot (GCD(1, 3) + GCD(2, 3) + GCD(3, 3)) = \\ (1 + 2) \cdot (1 + 1 + 3) = 3 \cdot 5 = 15

Let us compute using the formula g(n) = n \sum_{d \mid n} \frac{\varphi(d)}{d}

g(6) = 6 \sum_{d \mid 6} \frac{\varphi(d)}{d} = 6 \cdot \left(\frac{\varphi(1)}{1}+ \frac{\varphi(2)}{2} + \frac{\varphi(3)}{3} + \frac{\varphi(6)}{6} \right) \\ = 6\varphi(1) + 3\varphi(2) + 2\varphi(3) + \varphi(6) = 6 + 3 + 4 + 2 = 15

Now let us compute using the multiplicativity of the function f(x) = GCD(x, n):

g(6) = g(2) \cdot g(3) = (2 \cdot 2 - 1) \cdot (2 \cdot 3 - 1) = 3 \cdot 5 = 15

Example. Let n = 12.

Then

g(12) = \sum_{i=1}^{12} \gcd(i,12) = 1 + 2 + 3 + 4 + 1 + 6 + 1 + 4 + 3 + 2 + 1 + 12 = 40

At the same time

g(12) = g(4) \cdot g(3) = \\ (GCD(1, 4) + GCD(2, 4) + GCD(3, 4) + GCD(4, 4)) \cdot \\ (GCD(1, 3) + GCD(2, 3) + GCD(3, 3)) = \\ (1 + 2 + 1 + 4) \cdot (1 + 1 + 3) = 8 \cdot 5 = 40

Let us compute using the formula g(n) = n \sum_{d \mid n} \frac{\varphi(d)}{d}:

g(12) = 12 \sum_{d \mid 12} \frac{\varphi(d)}{d} = 12 \cdot \left( \frac{\varphi(1)}{1} + \frac{\varphi(2)}{2} + \frac{\varphi(3)}{3} + \frac{\varphi(4)}{4} + \frac{\varphi(6)}{6} + \frac{\varphi(12)}{12} \right) \\ = 12\varphi(1) + 6\varphi(2) + 4\varphi(3) + 3\varphi(4) + 2\varphi(6) + \varphi(12) \\ = 12 + 6 + 8 + 6 + 4 + 4 = 40

The divisors of are: 1, 2, 3, 4, 6, 12. The number of integers such that GCD(i, 12) = d is . For example, GCD(i, 12) = 3 holds for ϕ(12/3) = ϕ(4) = 2 different values of , namely i = 3, 9.

Now let us compute using the multiplicativity of the function f(x) = GCD(x, n):

g(12) = g(2^2) \cdot g(3) = (3 \cdot 2^2 - 2 \cdot 2) \cdot (2 \cdot 3 - 1) = 8 \cdot 5 = 40

Algorithm implementation

The euler function computes Euler’s totient function.

long long euler(long long n)
{
  long long i, result = n;
  for (i = 2; i * i <= n;i++)
  {
    if (n % i == 0) result -= result / i;
    while (n % i == 0) n /= i;
  }
  if (n > 1) result -= result / n;
  return result;
}

The main part of the program. Read the value of . Compute the value of using the formula \sum_{e \mid n} e \varphi\left(\frac{n}{e}\right). We look for all divisors of among the numbers from to \lfloor \sqrt{n} \rfloor.

If is a divisor of , then is also a divisor of . Therefore, when we find each divisor i \le \lfloor \sqrt{n} \rfloor, we should add i \phi(\frac{n}{i}) + \frac{n}{i} \phi(i) to the result , taking into account both divisors and .

If is a perfect square and i = sq = \left\lfloor \sqrt{n} \right\rfloor, then the divisors and are the same. In this case, iϕ(\frac{n}{i}) = \frac{n}{i} ϕ(i) will be added twice to the sum as two identical terms. Therefore, subtract one of them from during the initialization of this variable.

while(scanf("%lld",&n) == 1)
{
  sq = (long long)sqrt(1.0*n);
  res = (sq * sq == n) ? -sq * euler(sq) : 0;

  for (i = 1; i <= sq; i++)
    if(n % i == 0) res = res + i * euler(n/i) + (n / i) * euler(i);
  printf("%lld\n",res);
}

Algorithm implementation – using multiplicativity

Let us factor the number into prime factors:

n = p_1^{a_1} p_2^{a_2} ... p_k^{a_k}

We have proved that the function g(n) = \sum_{i=1}^{n} \gcd(i,n) is multiplicative. Therefore

g(n) = g(p_1^{a_1}) \cdot g(p_2^{a_2}) \cdot \ldots \cdot g(p_k^{a_k})

For a power of a prime number, we have proved the following formula:

g(p^a) = (a + 1)p^a - ap^{a-1}

These are the two formulas used directly in the program.

Process several test cases. Read the value of .

while(scanf("%lld",&n) == 1)
{

In the variable , we'll compute the value of .

  res = 1;

Factor the number into prime factors. Iterate over possible divisors.

  for (i = 2; i * i <= n; i++)
  {

If the number is a prime factor, extract the factor p = i^a.

    if (n % i == 0)
    {
      a = 0; p = 1;
      while(n % i == 0)
      {
        a++;
        p *= i;
        n /= i;
      }

Multiply the result by

g(p) = g(i^a) = (a + 1) \cdot i^a - a \cdot i^{a - 1} = (a + 1) \cdot p - a \cdot p / i
      res *= (a + 1) * p - a * p / i;
    }
  }

If n > 1, then is the last prime factor.

  if (n > 1) res *= (2*n - 1);

Print the answer.

  printf("%lld\n",res);
}

Java implementation

import java.util.Scanner;

public class Main
{
  static long euler(long n)
  {
    long result = n;
    for(int i = 2; i <= Math.sqrt(n);i++)
    {
      if (n % i == 0) result -= result / i;
      while (n % i == 0) n /= i;
    }
    if (n > 1) result -= result / n;
    return result;
  }
    
  public static void main(String[] args)
  {
    Scanner con = new Scanner(System.in);
    while(con.hasNext())
    {
      long n = con.nextLong();
      long sq = (long)Math.sqrt(n);
      long res = (sq * sq == n) ? -sq * euler(sq) : 0;
      for(long i = 1; i <= sq; i++)
        if (n % i == 0)
          res = res + i * euler(n/i) + (n / i) * euler(i);
      System.out.println(res);
    }
    con.close();
  }
}