import std.stdio, std.string, std.conv; import std.range, std.algorithm, std.array, std.typecons, std.container; import std.math, std.numeric, core.bitop; enum lim = 10^^9; enum kmax = 10^^4; void main() { int p, q; scan(p, q); auto l = new int[](q); auto r = new int[](q); int[] bs; foreach (i ; 0 .. q) { scan(l[i], r[i]); bs ~= l[i] - 1; bs ~= r[i]; } auto rw = new long[](10^^5 + 1); foreach (i ; 1 .. 10^^5 + 1) { rw[i] = rw[i-1] + (p % i); } debug { //writeln(rw[0..30]); } bs.sort(); long[int] cs; int d = p / kmax; while (!bs.empty && bs.front <= d) { cs[bs.front] = rw[bs.front]; bs.popFront(); } long res = rw[d]; foreach_reverse (k ; 0 .. kmax) { int s = p / (k + 1); int t = k == 0 ? 10^^9 + 7 : (p + k - 1) / k; while (!bs.empty && bs.front < t) { long rest = res; int let = max(0, bs.front + 1 - s - 1); rest += 1L * p * let; rest -= 1L * k * ksum(s + 1, bs.front + 1); cs[bs.front] = rest; bs.popFront(); } if (bs.empty) { break; } int le = t - s - 1; if (le <= 0) continue; debug { //writefln("%s, %s", s, t); } res += 1L * p * le; res -= 1L * k * ksum(s + 1, t); } foreach (i ; 0 .. q) { long ans = cs[r[i]] - cs[l[i]-1]; writeln(ans); } } // sum_{i = l}^{r - 1} k long ksum(int l, int r) { if (l >= r) { return 0; } return 1L * r * (r - 1) / 2 - 1L * l * (l - 1) / 2; } void scan(T...)(ref T args) { import std.stdio : readln; import std.algorithm : splitter; import std.conv : to; import std.range.primitives; auto line = readln().splitter(); foreach (ref arg; args) { arg = line.front.to!(typeof(arg)); line.popFront(); } assert(line.empty); } void fillAll(R, T)(ref R arr, T value) { static if (is(typeof(arr[] = value))) { arr[] = value; } else { foreach (ref e; arr) { fillAll(e, value); } } }