Under construction

Still being built. Things may change

The Beauty of Cycles

Time limit2sMemory limit1024 MB

Summary
Given n, m, and base k, count the distinct purely cyclic base-k reals that can be written as x/y with 1 <= x <= n and 1 <= y <= m.
Level

Hard8 of 10

Topics
Number theory, Math
Solved
No attempts yet

Problem

We say a base-kk real number is beautiful if its decimal part is purely cyclic.

Given base-10 integers nn and mm, count the distinct (in value) purely cyclic real numbers that can be written as xy\frac{x}{y}, where xx and yy are integers with 1≤x≤n1 \leq x \leq n and 1≤y≤m1 \leq y \leq m.

A real number is purely cyclic if and only if it can be written in the form a.c_1˙c_2c_3…c_p−1c_p˙\displaystyle a.\dot{c\_1} c\_2 c\_3 \ldots c\_{p-1} \dot{c\_p} where aa is a base-kk integer, p≥1p \geq 1, and for each 1≤i≤p1 \leq i \leq p, c_ic\_i is a base-kk digit.

For example, in base 10, 0.45454545⋯=0.4˙5˙0.45454545\cdots = 0.\dot{4}\dot{5} is purely cyclic and can be represented by 511\frac{5}{11} or 1022\frac{10}{22}. In base 10, 0.166666⋯=0.16˙0.166666\cdots = 0.1\dot{6} is not purely cyclic, but it can be represented by fractions such as 16\frac{1}{6}.

Attention: an integer is purely cyclic, since its decimal part can be written as repeating 0s or repeating k−1k-1s. A terminating decimal with a nonzero decimal part is not purely cyclic.

In China, the repeating part is marked by one or two dots. In some countries, it is marked by a line above the repeating part.

Input

The input is one line with three base-10 integers nn, mm, and kk. Their meanings are described in the problem statement.

Output

Print one line with the number of beautiful numbers that satisfy all the constraints.

Constraints

For all test cases, 1≤n≤1091 \leq n \leq 10^9, 1≤m≤1091 \leq m \leq 10^9, and 2≤k≤20002 \leq k \leq 2000.

Examples2

  1. Example 1

    Input
    2 6 10
    
    Expected output
    4
    
  2. Example 2

    Input
    23333 666666 310
    
    Expected output
    5089564081