To solve this problem, we need to find the number of integer pairs (x, y) such that their sum equals a given integer (a) and their greatest common divr (gcd) equals another given integer (b).
Approach
- Key Insight: If the gcd of (x) and (y) is (b), then (x = b \times m) and (y = b \times n) where (gcd(m, n) = 1) (coprime). Substituting into (x + y = a) gives (b(m + n) = a), so (a) must be divisible by (b) (otherwise, there are no valid pairs).
- Reduction: If (a) is divisible by (b), let (k = \frac{a}{b}). We need pairs ((m, n)) such that (m + n = k) and (gcd(m, n) = 1). Since (gcd(m, k - m) = gcd(m, k)), we need (gcd(m, k) = 1).
- Euler's Totient Function: The number of integers (m) (coprime with (k)) is given by Euler's Totient Function (\phi(k)). Each valid (m) gives two ordered pairs ((b \times m, b \times (k - m))) and ((b \times (k - m), b \times m)), so the total number of pairs is (2 \times \phi(k)).
Solution Code
def compute_phi(n):
result = n
i = 2
while i * i <= n:
if n % i == 0:
while n % i == 0:
n = n // i
result -= result // i
i += 1
if n > 1:
result -= result // n
return result
a, b = map(int, input().split())
if a % b != 0:
print(0)
else:
k = a // b
phi = compute_phi(k)
print(2 * phi)
Explanation
- Compute Euler's Totient Function: The function
compute_phi(n)calculates (\phi(n)) using the formula: (\phi(n) = n \times \prod_{p|n} (1 - \frac{1}{p})) where (p) are distinct prime factors of (n). - Check Divisibility: If (a) is not divisible by (b), output 0 (no valid pairs).
- Calculate Result: If (a) is divisible by (b), compute (k = \frac{a}{b}), then the number of valid pairs is (2 \times \phi(k)).
This approach efficiently computes the result using number theory concepts, ensuring optimal performance even for large values of (a) and (b). The example input (a = 12) and (b = 3) gives (k = 4), (\phi(4) = 2), so the result is (2 \times 2 = 4), which matches the expected output.
Answer: The code will output the correct number of pairs as described. For the given example, the output is 4. So the final answer is (\boxed{4}).


(免责声明:本文为本网站出于传播商业信息之目的进行转载发布,不代表本网站的观点及立场。本文所涉文、图、音视频等资料的一切权利和法律责任归材料提供方所有和承担。本网站对此资讯文字、图片等所有信息的真实性不作任何保证或承诺,亦不构成任何购买、投资等建议,据此操作者风险自担。) 本文为转载内容,授权事宜请联系原著作权人,如有侵权,请联系本网进行删除。