GCD Sums
Chef has an array $a_1,\ldots, a_n$ of $n$ elements. He wants you to answer queries of the following type: Compute $f(L, R)$, where $$f(L, R) = \sum_{i = L}^{R} \sum_{j= i}^{R} \gcd(a_i, a_{i+1}, \dots a_j).$$ In other words, we want to compute the sum of the greatest common divisors over all subarrays of some range $[L, R]$. Help Chef answer $q$ queries of the described type. ###Input
HINT LADDERno hints yet
L1 Observation
L2 Technique
L3 Approach
L4 Pseudo-code
🔒
L5 Full solution
L5 unlocks only if you insist twice
solution.cppC++17
CodeSearch Tutor
Hints, not spoilers — it won’t hand over the full solution unless you insist.
Sign in to chat with the tutor and save your progress.
Sign in to start