1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include <cstdio> #include <map> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <class Type, class ...rest> void read(Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write(Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } typedef long long LL; const int N = 1e6 + 10, mod = 1e9 + 7; int n, m, L, R, f[N]; map <int, int> F; int cnt, p[N]; bool vis[N]; void init() { f[1] = 1; vis[1] = true; for (int i = 2; i < N; ++i) { if (!vis[i]) p[++cnt] = i, f[i] = -1; for (int j = 1; i * p[j] < N; ++j) { vis[i * p[j]] = true; if (i % p[j] == 0) { f[i * p[j]] = 0; break; } f[i * p[j]] = f[i] * f[p[j]]; } } for (int i = 2; i < N; ++i) f[i] += f[i - 1]; } int calc (int x) { if (x < N) return f[x]; if (F.count(x)) return F[x]; int res = 1; for (int l = 2, r; l <= x; l = r + 1) { r = x / (x / l); res -= (r - l + 1) * calc(x / l); } return F[x] = res; } int binpow (int b, int k = n) { int res = 1; for (; k; k >>= 1, b = (LL)b * b % mod) if (k & 1) res = (LL)res * b % mod; return res; } int main () { init(); read(n, m, L, R); L = (L - 1) / m, R = R / m; int res = 0; for (int l = 1, r; l <= R; l = r + 1) { r = !(L / l) ? R / (R / l) : min(L / (L / l), R / (R / l)); (res += (LL)(calc(r) - calc(l - 1)) * binpow(R / l - L / l) % mod) %= mod; } write((res + mod) % mod); return 0; }
|