我们发现不合法的整除因子在 m 的因子里面, 然后枚举m的因子暴力容斥, 或者用莫比乌斯系数容斥。
#include#define LL long long#define LD long double#define ull unsigned long long#define fi first#define se second#define mk make_pair#define PLL pair #define PLI pair #define PII pair #define SZ(x) ((int)x.size())#define ALL(x) (x).begin(), (x).end()#define fio ios::sync_with_stdio(false); cin.tie(0);using namespace std;const int N = 1e5 + 7;const int inf = 0x3f3f3f3f;const LL INF = 0x3f3f3f3f3f3f3f3f;const int mod = 1e9 + 7;const double eps = 1e-8;const double PI = acos(-1);template inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}template inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}template inline bool chkmax(T& a, S b) { return a < b ? a = b, true : false;}template inline bool chkmin(T& a, S b) { return a > b ? a = b, true : false;}int miu[N];vector fac[N];int cnt[N], n, m;int F[N], Finv[N], inv[N];int C(int n, int m) { if(n < 0 || n < m) return 0; return 1LL * F[n] * Finv[m] % mod * Finv[n - m] % mod;}inline int calc(int n, int m) { if(m < n) return 0; return C(m - 1, n - 1);}int main() { inv[1] = F[0] = Finv[0] = 1, miu[1] = 1; for(int i = 1; i < N; i++) for(int j = i + i; j < N; j += i) miu[j] -= miu[i]; for(int i = 2; i < N; i++) inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod; for(int i = 1; i < N; i++) F[i] = 1LL * F[i - 1] * i % mod; for(int i = 1; i < N; i++) Finv[i] = 1LL * Finv[i - 1] * inv[i] % mod; for(int i = 1; i < N; i++) for(int j = i; j < N; j += i) fac[j].push_back(i); int T; scanf("%d", &T); while(T--) { scanf("%d%d", &m, &n); int ans = 0; for(int i = 0; i < SZ(fac[m]); i++) ans = (ans + 1LL * miu[fac[m][i]] * calc(n, m / fac[m][i]) % mod + mod) % mod; printf("%d\n", ans); } return 0;}/**/