/*******************************************************
4) AtCoder — Count ordered triplets (a,b,c) in 1..n
such that a % b = c and a,b,c are pairwise distinct.
Approach used - the classic O(sqrt(n)) "group by n / i" trick.
Key idea (even simpler counting):
- For a % b = c with c >= 1:
* If a < b => a % b = a => c = a => not distinct (a == c) ❌
* So we must have a >= b (i.e., quotient q >= 1).
- For fixed b (b >= 2):
* valid a are b..n
* c = a % b must be non-zero (else c=0 not allowed)
=> count(b) = (number of a in [b..n]) - (number of multiples of b in [b..n])
Compute:
- number of a in [b..n] = n - b + 1
- number of multiples of b in [1..n] = floor(n/b)
(and all of them are >= b, so same count in [b..n])
So:
count(b) = (n - b + 1) - floor(n/b)
Total answer:
ans = sum_{b=2..n} (n - b + 1) - sum_{b=2..n} floor(n/b)
First sum is easy:
sum_{b=2..n} (n - b + 1) = 1 + 2 + ... + (n-1) = n(n-1)/2
Let H(n) = sum_{i=1..n} floor(n/i)
Then sum_{b=2..n} floor(n/b) = H(n) - floor(n/1) = H(n) - n
So:
ans = n(n-1)/2 - (H(n) - n) = n(n+1)/2 - H(n)
Now compute H(n) in O(sqrt(n)) by grouping indices where floor(n/i) is constant.
Time: O(sqrt(n))
*******************************************************/
#include <bits/stdc++.h> // include all common C++ headers
using namespace std; // avoid writing std:: everywhere
static void print_int128(__int128 x) { // helper: print big __int128 safely
if (x == 0) { // if number is zero
cout << 0; // print 0
return; // and return
}
if (x < 0) { // if negative (not expected here, but safe)
cout << '-'; // print minus sign
x = -x; // make it positive
}
string s; // we'll build digits here
while (x > 0) { // while digits remain
int digit = (int)(x % 10); // take last digit
s.push_back(char('0' + digit)); // append it
x /= 10; // remove last digit
}
reverse(s.begin(), s.end()); // digits were reversed, fix order
cout << s; // print final string
}
int main() { // program starts here
ios::sync_with_stdio(false); // fast input/output
cin.tie(nullptr); // untie cin from cout for speed
long long n; // read n (can be large)
cin >> n; // input n
// Compute H(n) = sum_{i=1..n} floor(n/i) in O(sqrt(n))
__int128 H = 0; // H can be large, use __int128
long long l = 1; // l = start of current block
while (l <= n) { // process until we cover all i from 1..n
long long q = n / l; // q = floor(n/l) (constant inside this block)
long long r = n / q; // r = largest i such that floor(n/i) == q
H += (__int128)q * (r - l + 1); // add q repeated (r-l+1) times
l = r + 1; // move to next block
}
// ans = n(n+1)/2 - H(n)
__int128 nn = (__int128)n; // cast n to __int128 once
__int128 totalPairs = nn * (nn + 1) / 2; // compute n(n+1)/2 safely
__int128 ans = totalPairs - H; // final answer
print_int128(ans); // print answer (supports huge values)
cout << "\n"; // newline
return 0; // done
}