[题目链接] https://www.luogu.org/problemnew/show/P3172
[题解] https://www.luogu.org/blog/user29936/solution-p3172
1.推式子里面最重要的一个套路:枚举\(di,\)忽略倍数系数的影响.在这道题里面应用于只考虑k的倍数才是有用的.
2.考虑容斥做法,即\(f[i]\)表示答案是\(i\)的倍数的方案数.
3.为避免讨论边界情况,不考虑全选同一个数的情况,即设\(f[i]=x^{n}-x,\)最后再考虑能否全选k的情况.
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
int res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
const int N = 1e5 + 5, PYZ = 1e9 + 7;
int n, K, L, H, f[N];
int qpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1ll * res * a % PYZ;
a = 1ll * a * a % PYZ;
b >>= 1;
}
return res;
}
int main() {
int i, j; n = read(); K = read(); L = read(); H = read();
if (L % K) L = L / K + 1; else L /= K; H /= K;
if (L > H) return puts("0"), 0;
for (i = 1; i <= H - L; i++) {
int l = L, r = H;
if (l % i) l = l / i + 1; else l /= i; r /= i;
if (l > r) continue;
f[i] = (qpow(r - l + 1, n) - (r - l + 1) + PYZ) % PYZ;
}
for (i = H - L; i; i--) for (j = (i << 1); j <= H - L; j += i)
f[i] = (f[i] - f[j] + PYZ) % PYZ;
if (L == 1) (f[1] += 1) %= PYZ; cout << f[1] << endl;
return 0;
}