/* -*- coding: utf-8 -*- * * 890.cc: No.890 移調の限られた旋法 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 1000000; const int MAX_M = 8; const int MOD = 1000000007; /* typedef */ typedef long long ll; /* global variables */ bool primes[MAX_N + 1]; int pnums[MAX_N], dps[MAX_M]; int fracs[MAX_N + 1], invfs[MAX_N + 1]; /* subroutines */ template T gcd(T m, T n) { // m > 0, n > 0 if (m < n) swap(m, n); while (n > 0) { T r = m % n; m = n; n = r; } return m; } int gen_primes(int maxp, int pnums[]) { memset(primes, true, sizeof(primes)); primes[0] = primes[1] = false; int p, pn = 0; for (p = 2; p * p <= maxp; p++) if (primes[p]) { pnums[pn++] = p; for (int q = p * p; q <= maxp; q += p) primes[q] = false; } for (; p <= maxp; p++) if (primes[p]) pnums[pn++] = p; return pn; } int powmod(int a, int n) { // a^n % MOD int pm = 1; while (n > 0) { if (n & 1) pm = (ll)pm * a % MOD; a = (ll)a * a % MOD; n >>= 1; } return pm; } inline int invf(int n) { return powmod(fracs[n], MOD - 2); } inline int nck(int n, int k) { // nCk % MOD return (ll)fracs[n] * invf(n - k) % MOD * invf(k) % MOD; } /* main */ int main() { int n, k; scanf("%d%d", &n, &k); int g = gcd(n, k); int pn = gen_primes(g, pnums); int m = 0; for (int i = 0; i < pn; i++) if (g % pnums[i] == 0) dps[m++] = pnums[i]; //printf("m=%d\n", m); fracs[0] = 1; for (int i = 1; i <= n; i++) fracs[i] = (ll)fracs[i - 1] * i % MOD; int sum = 0, mbits = 1 << m; for (int bits = 1; bits < mbits; bits++) { int bn = 0, d = 1; for (int i = 0, bi = 1; i < m; i++, bi <<= 1) if (bits & bi) bn++, d *= dps[i]; int c = nck(n / d, k / d); //printf("nck(%d,%d)=%d\n", n / d, k / d, c); sum = (sum + ((bn & 1) ? c : MOD - c)) % MOD; } printf("%d\n", sum); return 0; }