require 'prime' N, K, M = gets.split.map(&:to_i) V = Prime.prime_division(N).map{|ne| ne.tap{ ne[1] *= K } } def dfs(i, m) n = V[i][0] e = V[i][1] (0 .. e).inject(0) do |s, j| q = m * (n ** j) break s unless q <= M if i + 1 < V.size s + dfs(i + 1, q) else s + 1 end end end puts dfs(0, 1)