## 解法

### 概要

• $P \lt n$ なら $a_n = P$
• $\frac{P}{2} \lt n \le P \le n$ なら $a_n = P - 1, P - 2, \dots, 0$
• $\frac{P}{3} \lt n \le P \le \frac{P}{2}$ なら $a_n = P - 2, P - 4, \dots, 0$ あるいは $a_n = P - 1, P - 3, \dots, 1$
• $\vdots$

## 実装

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
using ll = long long;
using namespace std;

int main() {
int p, q; cin >> p >> q;

constexpr int K = 1000000;
vector<ll> small(K);
small[0] = 0;
small[1] = 0;
REP3 (i, 1, K - 1) {
small[i + 1] = small[i] + (p % i);
}
auto solve1 = [&](int r) {
ll large = 0;
if (p + 1 < r) {
large += p * (r - p - 1ll);
r = p + 1;
}
for (int k = 1; r >= small.size(); ++ k) {
int l = p / k + 1;
if (r <= l) continue;
ll a1 = p % l;
ll a2 = p % (l + 1);
ll d = a2 - a1;
ll an = a1 + (r - l - 1) * d;
large += (r - l) * (a1 + an) / 2;
r = l;
}
return small[r] + large;
};

REP (i, q) {
int l, r; cin >> l >> r;
ll answer = solve1(r + 1) - solve1(l);