How to find the number of arithmetic progressions in {1,2,3,...,n}, including trivial arithmetic progressions of lengths 1,2.
n <= 1e10
a(n+1) = (n + 1)(1 + Sum_{i=1..n} floor(n/i)) - Sum_{i=1..n} itau(i).
I used this formula, and my code is Time Limit Exceeded
//C++
const int mod = 1000000007;
long long L(long long n){
long long res = n;
for (int i = 1; i <= n-1; i++)
res = (res + (n-1)/i * (n+(n-1)%i-i+1) / 2) % mod;
return res;
}
now I can improve it:
Sum_{k=1..n} floor(n/k) = 2*(Sum_{i=1..floor(sqrt(n))} floor(n/i)) - floor(sqrt(n))^2
Sum_{i=1..n} itau(i) = Sum_{i=1..floor(sqrt(n))} ifloor(n/i)(1+floor(n/i)) - [floor(sqrt(n))(1+floor(sqrt(n)))/2]^2
I need just O(sqrt(n))