#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 7;
const long long mod = 2e9 + 7;
long long n, w[N], M, K;
long long cnt, pri[N], sqr, tot, m, id1[N], id2[N], g[N], h[N], sum[N];
bool vis[N];
long long priid;
inline long long qpow(long long x, long long k) {
  long long ans = 1;
  while (k) {
    if (k & 1) ans = (ans * x) % mod;
    k >>= 1;
    x = (x * x) % mod;
  }
  return ans;
}
inline void get_prime(long long n) {
  for (long long i = 2; i <= n; i++) {
    if (!vis[i]) pri[++cnt] = i;
    for (long long j = 1; j <= cnt && i * pri[j] <= n; j++) {
      vis[i * pri[j]] = 1;
      if (i % pri[j] == 0) break;
    }
  }
}
long long S(long long x, long long y) {
  if (x <= 1 || pri[y] > x) return 0;
  long long pos = (x <= sqr) ? id1[x] : id2[n / x];
  long long ret = h[pos] - (y - 1);
  ret = (ret + mod) % mod;
  for (long long i = y; i <= cnt && 1ll * pri[i] * pri[i] <= x; i++) {
    long long p1 = pri[i], p2 = 1ll * pri[i] * pri[i];
    for (long long e = 1; p2 <= x; e++, p1 = p2, p2 *= pri[i]) {
      ret += (1ll * S(x / p1, i + 1) + 1);
      ret %= mod;
    }
  }
  return ret;
}
long long solve(long long n) {
  if (n <= 1 || n < pri[priid]) return 0;
  m = 0;
  for (long long l = 1, r; l <= n; l = r + 1) {
    r = n / (n / l);
    w[++m] = n / l;
    if (w[m] <= sqr)
      id1[w[m]] = m;
    else
      id2[n / w[m]] = m;
    h[m] = (w[m] - 1) % mod;
  }
  long long res = 0;
  for (long long j = 1; j <= cnt; j++) {
    for (long long i = 1; i <= m && 1ll * pri[j] * pri[j] <= w[i]; i++) {
      long long k = (w[i] / pri[j] <= sqr) ? id1[w[i] / pri[j]]
                                           : id2[n / (w[i] / pri[j])];
      h[i] = (h[i] - (h[k] - (j - 1)) + mod) % mod;
      if (j == priid && i == 1) res = h[k] - (j - 1);
    }
  }
  return res + 1;
}
signed main() {
  scanf("%lld %lld %lld", &M, &n, &K);
  sqr = sqrt(2e9 + 7);
  get_prime(sqr);
  priid = -1;
  for (long long i = 1; i <= cnt && 1ll * pri[i] <= K; i++) {
    if (K % pri[i] == 0) {
      if (K == pri[i]) {
        priid = i;
        break;
      } else {
        priid = 0;
        break;
      }
    }
  }
  if (priid == 0) {
    printf("0\n");
  } else if (priid == -1) {
    long long sum1 = 0, sum2 = 0;
    long long p = K;
    while (p <= 2e9 && p <= n) {
      if (p <= M) sum2++;
      if (p <= n) sum1++;
      p *= K;
    }
    printf("%lld\n", sum1 - sum2);
  } else {
    long long sum1 = solve(n);
    long long sum2 = solve(M - 1);
    printf("%lld\n", sum1 - sum2);
  }
  return 0;
}
