// yukicoder: No.752 mod数列 // 2019.6.17 bal4u #include #include typedef long long ll; #if 1 #define gc() getchar_unlocked() #define pc(c) putchar_unlocked(c) #else #define gc() getchar() #define pc(c) putchar(c) #endif int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } void out(ll n) // 非負整数の表示(出力) { int i; char b[50]; if (!n) pc('0'); else { i = 0; while (n) b[i++] = n % 10 + '0', n /= 10; while (i--) pc(b[i]); } pc('\n'); } int sa[31650]; ll s[31650]; int P; ll sum(int l, int r, int d) { if (l > r) return 0; return ((((P % r)<<1) + (ll)(r-l)*d)*(r-l+1)) >> 1; } int main() { int i, r, n, pl, pr, Q, L, R, M; ll ans; P = in(), Q = in(); r = (int)sqrt((double)P); M = P/r; for (i = 2; i <= M; i++) sa[i] = sa[i-1] + P % i; n = P; for (i = 1; i < r; i++) { int a = P % n, k = P/(i+1); s[i+1] = s[i] + ((((a<<1) + (ll)(n-k-1)*i)*(n-k)) >> 1); n = k; } while (Q--) { L = in(), R = in(), pl = P/L, pr = P/R; if (pr == 0) pr = 1; if (R <= M) ans = sa[R] - sa[L-1]; else if (L <= M) { ans = sa[M] - sa[L-1] + (s[r]-s[pr]) - sum(R+1, P/pr, pr); } else if (L <= P) { ans = s[pl]-s[pr] + sum(L, P/pl, pl) - sum(R+1, P/pr, pr); } if (L > P) ans = (ll)P * (R-L+1); else if (R > P) ans += (ll)P * (R-P); out(ans); } return 0; }