fork download
  1. // Divisor Summotary Function
  2. #pragma GCC optimize("Ofast")
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. using ll = long long;
  6. #define int long long
  7.  
  8. ll sum(ll l, ll r)
  9. {
  10. return r * (r + 1) / 2 - l * (l + 1) / 2 + l;
  11. }
  12. ll const N = 9;
  13. void Solve()
  14. {
  15. ll divSum = 0;
  16. for (int i = 1; i * i <= N; i++) {
  17. // value <= sqrt(N)
  18. ll multiplier = N / i;
  19. divSum += i * multiplier;
  20. // value > sqrt(N)
  21. ll NbyA = i; // count
  22.  
  23. ll highestVal = N / i;
  24. ll lowestVal = N / (i + 1) + 1;
  25.  
  26. lowestVal = max(lowestVal, i + 1); // FIX: Value must be greater than sqrtN or i
  27. if (lowestVal <= highestVal) // FIX: Removes duplicate addition in square number
  28. divSum += NbyA * sum(lowestVal, highestVal); // count * sum of values in range
  29. }
  30. cout << divSum << '\n';
  31. }
  32.  
  33. int32_t main()
  34. {
  35. ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  36. int t = 1;
  37. // cin >> t;
  38. for (int i = 1; i <= t; i++) {
  39. Solve();
  40. }
  41. return 0;
  42. }
  43. // Coded by Tahsin Arafat (@TahsinArafat)
  44.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
69