Can there be a faster solution for this !!! ???

Revision en1, by Shellhead96, 2019-02-06 09:21:52

Egor likes to play with positive integers and their divisors. Bigger the number to play with, more the fun! The boy asked you to come up with an algorithm, that could play the following game:

Let's define f(n) as the sum of all odd divisors of n. I.e. f(10) = 1 + 5 = 6 and f(21) = 1 + 3 + 7 + 21 = 32. The game is to calculate f(l) + f(l + 1) + ... + f(r — 1) + f(r) for the given integers l and r.

Have fun! But be careful, the integers might be quite big.

Input The first line of the input contains one integer T denoting the number of test cases.

The only line of the test case description contains two positive integers l and r.

Output For each test case, output the required sum on a separate line.

Constraints 1 ≤ T ≤ 10 1 ≤ l ≤ r ≤ 105 Example Input: 2 1 10 42 42

Output: 45 32 Explanation In the first example case, f(1) + f(2) + ... + f(10) = 1 + 1 + 4 + 1 + 6 + 4 + 8 + 1 + 13 + 6 = 45

In the second example case, f(42) = 32.

Editorial solution

include <bits/stdc++.h>

using namespace std;

void solve() { int l, r; cin >> l >> r;

long long answer = 0ll; for (int d = 1; d <= r; d += 2) { answer += d * 1ll * (r / d — (l — 1) / d); }

cout << answer << "\n"; }

int main() { int cases; cin >> cases; for (int i = 0; i < cases; i++) { solve(); }

return 0; }

Tags common divisors, #dynamic-programming, #dp, factor

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en4 English Shellhead96 2019-02-06 09:29:05 25
en3 English Shellhead96 2019-02-06 09:27:41 968
en2 English Shellhead96 2019-02-06 09:24:38 424
en1 English Shellhead96 2019-02-06 09:21:52 1429 Initial revision (published)