#P2257. Counting Pairs with Prime GCD

    ID: 15534 Type: Default 1000ms 256MiB

Counting Pairs with Prime GCD

Counting Pairs with Prime GCD

You are given two positive integers N and M. Your task is to count the number of pairs (x, y) such that 1 ≤ x ≤ N, 1 ≤ y ≤ M and the greatest common divisor \(\gcd(x, y)\) is a prime number.

Hint:

  • If \(d = \gcd(x, y)\) is prime, then we can write \(x = d \cdot a\) and \(y = d \cdot b\) with \(\gcd(a, b) = 1\).
  • Thus for each prime \(p\), the number of valid pairs contributed by \(p\) is \(f(\lfloor N/p \rfloor, \lfloor M/p \rfloor)\), where \(f(A, B) = \sum_{d=1}^{\min(A,B)}\mu(d) \left\lfloor \frac{A}{d} \right\rfloor \left\lfloor \frac{B}{d} \right\rfloor\) and \(\mu(\cdot)\) is the Möbius function.

Compute and output the total count over all prime numbers.

inputFormat

The input consists of a single line containing two space-separated integers N and M.

outputFormat

Output a single integer representing the number of pairs \((x, y)\) such that \(1 \le x \le N\), \(1 \le y \le M\) and \(\gcd(x, y)\) is a prime.

sample

5 5
5