Data Structures & AlgorithmsVery hardICPC regional · number theory
Problem: How many ordered pairs (a, b) with 1 <= a <= b <= 20 have gcd(a, b) = 1? Run the code.
Problem: How many ordered pairs (a, b) with 1 <= a <= b <= 20 have gcd(a, b) = 1? Run the code.
from math import gcd print(sum(1 for a in range(1, 21) for b in range(a, 21) if gcd(a, b) == 1))What is printed?
- A. 127
- B. 120
- C. 128
- D. 255
Correct answer: C. 128
Each b contributes φ(b) coprime partners a ≤ b, so the count is Σφ(b) for b = 1..20 = 128; the pair (1, 1) is included. Starting b at a + 1 drops (1, 1) and gives 127, stopping at 19 drops φ(20) = 8 (120), and counting ordered pairs gives 2 × 128 − 1 = 255.
More Coding Interview Problems questions
- Problem: How many integers in [1, 100] are the product of exactly two distinct primes? Run the code. python def primesupto(n): s = [True]…
- Problem: What is the minimum length of a contiguous subarray of [1, 2, 3, 4, 5, 6, 7, 8] with sum at least 15? Run the two-pointer code…
- Problem: What is the integer floor of the square root of 10^12? Use binary search. python lo, hi = 0, 10 12 while lo < hi: mid = (lo + hi…
- Problem: Split [7, 2, 5, 10, 8] into 2 contiguous parts to minimise the largest part sum. What is the answer? Run the brute-force code…
- Problem: How many contiguous subarrays of [4, 1, -1, 2, 3] have a sum divisible by 4? Run the prefix-count code. python from collections…
- Problem: How many distinct substrings of length 2 does 'abcabc' contain? Run the code. python s = 'abcabc' print(len({s[i:i + 2] for i in…