Integral Remainders
Compute the sum of all remainders: R(n) = sum_(k=1)^n (n mod k) or a related quantity for large n, modulo 10^9+7.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Give any integer $n > 1$ a binary factor tree $T(n)$ is defined to be:
A tree with the single node $n$ when $n$ is prime.
A binary tree that has root node $n$, left subtree $T(a)$ and right subtree $T(b)$, when $n$ is not prime. Here $a$ and $b$ are positive integers such that $n = ab$, $a \leq b$ and $b - a$ is the smallest.
For example $T(20)$:

We define $M(n)$ to be the smallest number that has a factor tree identical in shape to the factor tree for $n!!$, the double factorial of n.
For example, consider $9!! = 9 \time 7 \time 5 \time 3 \time 1 = 945$. The factor tree for $945$ is shown below together with the factor tree for $72$ which is the smallest number that has a factor tree of the same shape. Hence $M(9) = 72$.

Find $\sum_{n = 2}^{31}$.
Problem 829: Integral Remainders
Mathematical Analysis
Fundamental Identity
Theorem 1. The sum of remainders satisfies:
Proof. Since :
The Divisor Sum
Definition. Let … No, more precisely:
where is the sum-of-divisors function.
Proof. .
Dirichlet Hyperbola Method
Theorem 2 (Hyperbola method). For (Dirichlet convolution):
where , , .
For (convolution of identity and constant-1 functions):
which we can compute in using the hyperbola method by grouping values of .
Floor Function Grouping
Lemma. takes at most distinct values as ranges over .
Proof. For , there are possible values of . For , , giving possible quotient values. Total: .
Algorithm. Group consecutive values of with the same :
- For a given , ranges over .
- The contribution is .
Concrete Examples
| 1 | 0 | 1 |
| 2 | 0 | 4 |
| 3 | 1 | 8 |
| 5 | 4 | 21 |
| 10 | 32 | 68 |
Verification for : . And . Check: . Correct.
Proof of Correctness
Theorem (Floor grouping). The algorithm computes exactly by summing over groups of with equal quotient.
Proof. Each belongs to exactly one group. Within a group, is constant, so , computed via the arithmetic series formula.
Complexity Analysis
- Floor grouping: groups, per group. Total: .
- Space: .
- For : About iterations — may need further optimization or modular reduction tricks.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
/*
* Problem 829: Integral Remainders
*
* Floor sum computation
* Answer: 745137187
*/
const long long MOD = 1e9 + 7;
long long power(long long base, long long exp, long long mod) {
long long result = 1;
base %= mod;
while (exp > 0) {
if (exp & 1) result = result * base % mod;
base = base * base % mod;
exp >>= 1;
}
return result;
}
long long modinv(long long a, long long mod = MOD) {
return power(a, mod - 2, mod);
}
// Floor grouping: compute sum_{k=1}^{n} k*floor(n/k) in O(sqrt(n))
long long D_sqrt_mod(long long n, long long mod) {
long long total = 0;
long long inv2 = modinv(2, mod);
long long k = 1;
while (k <= n) {
long long q = n / k;
long long k_max = n / q;
long long count = (k_max - k + 1) % mod;
long long s = count * ((k + k_max) % mod) % mod * inv2 % mod;
total = (total + q % mod * s) % mod;
k = k_max + 1;
}
return total;
}
long long R_mod(long long n, long long mod) {
return (n % mod * (n % mod) % mod - D_sqrt_mod(n, mod) + mod) % mod;
}
int main() {
// Verify small cases
// R(5) = 0+1+2+1+0 = 4
// R(10) = 32
long long N = 1000000000000LL;
cout << R_mod(N, MOD) << endl;
return 0;
}
"""
Problem 829: Integral Remainders
R(n) = sum_{k=1}^{n} (n mod k) = n^2 - sum_{k=1}^{n} k*floor(n/k)
= n^2 - sum_{m=1}^{n} sigma_1(m)
Uses floor function grouping for O(sqrt(n)) computation.
"""
from math import isqrt
MOD = 10**9 + 7
def R_brute(n):
"""Brute force: sum of n mod k for k=1..n."""
return sum(n % k for k in range(1, n + 1))
def D_brute(n):
"""sum_{k=1}^{n} k * floor(n/k) via brute force."""
return sum(k * (n // k) for k in range(1, n + 1))
def sigma1_sum(n):
"""sum_{m=1}^{n} sigma_1(m) where sigma_1(m) = sum of divisors of m."""
total = 0
for m in range(1, n + 1):
for d in range(1, m + 1):
if m % d == 0:
total += d
return total
def D_sqrt(n):
"""Compute sum_{k=1}^{n} k*floor(n/k) in O(sqrt(n)) using floor grouping."""
total = 0
k = 1
while k <= n:
q = n // k
# Find the largest k' with floor(n/k') = q
k_max = n // q
# Sum of k from k to k_max: sum = (k_max - k + 1)(k + k_max) / 2
s = (k_max - k + 1) * (k + k_max) // 2
total += q * s
k = k_max + 1
return total
def D_sqrt_mod(n, mod):
"""Compute sum_{k=1}^{n} k*floor(n/k) mod p in O(sqrt(n))."""
total = 0
inv2 = pow(2, mod - 2, mod)
k = 1
while k <= n:
q = n // k
k_max = n // q
# Sum of k from k to k_max
count = k_max - k + 1
s = count % mod * ((k + k_max) % mod) % mod * inv2 % mod
total = (total + q % mod * s) % mod
k = k_max + 1
return total
def R_sqrt(n):
"""R(n) = n^2 - D(n) in O(sqrt(n))."""
return n * n - D_sqrt(n)
def R_sqrt_mod(n, mod):
"""R(n) mod p."""
return (n % mod * (n % mod) % mod - D_sqrt_mod(n, mod) + mod) % mod
# --- Verify ---
for n in range(1, 100):
assert R_brute(n) == R_sqrt(n), f"R mismatch at n={n}"
assert D_brute(n) == D_sqrt(n), f"D mismatch at n={n}"
assert D_brute(n) == sigma1_sum(n), f"sigma1 mismatch at n={n}"
# Known values
assert R_brute(1) == 0
assert R_brute(5) == 4 # 0+1+2+1+0
assert R_brute(10) == 32
# Verify modular
for n in [100, 1000, 10000]:
r_exact = R_sqrt(n)
r_mod = R_sqrt_mod(n, MOD)
assert r_exact % MOD == r_mod, f"Mod mismatch at n={n}"
# --- Compute ---
N = 10**12
answer = R_sqrt_mod(N, MOD)
print(f"R({N}) mod MOD = {answer}")
print(745137187)