#P1587. [NOI2016] 循环之美

[NOI2016] 循环之美

Description

Niuniu (Niúniú) is a high school student who loves algorithm design. In his algorithms, he often performs computations with numbers that have fractional parts. Niuniu considers a number beautiful if, in base kk, its fractional part is purely repeating. Now, given decimal numbers nn and mm, he wants to know: in base kk, how many numerically distinct purely repeating fractions can be written as a fraction xy\frac x y, where 1xn1 \leq x \leq n, 1ym1 \leq y \leq m, and x,yx, y are integers. A number is purely repeating if and only if it can be written in the following form:

a.c1˙c2c3cp1cp˙a.\dot{c_1} c_2 c_3 \dots c_{p - 1} \dot{c_p}

Here, aa is an integer, p1p \geq 1; for 1ip1 \leq i \leq p, cic_i is a single digit in base kk.

For example, in base 10, 0.45454545...=0.4˙5˙0.45454545... = 0.\dot {4} \dot {5} is purely repeating; it can be represented by fractions such as 511\frac {5}{11} or 1022\frac{10}{22}. In base 10, 0.1666666...=0.16˙0.1666666... = 0.1\dot6 is not purely repeating; it can be represented by 16\frac 1 6. Note that we consider an integer to be purely repeating, because its fractional part can be represented as a repeating 00 or a repeating k1k - 1; however, a finite fraction with a nonzero fractional part is not purely repeating.

Input Format

One line containing three decimal integers N,M,KN, M, K as described.

Output Format

Output a single integer on one line, the number of beautiful numbers that satisfy the conditions.

2 6 10
4

Hint

Sample Explanation

The numbers that satisfy the conditions are:

11=1.0000...\frac 1 1 = 1.0000...

13=0.3333...\frac 1 3 = 0.3333...

21=2.0000...\frac 2 1 = 2.0000...

23=0.6666...\frac 2 3 = 0.6666...

Although 11\frac 1 1 and 22\frac 2 2 are both purely repeating decimals, they are equal, so they are counted only once; similarly, 13\frac 1 3 and 26\frac 2 6 are counted only once.

Constraints

For all test points, it is guaranteed that 1n1091 \leq n \leq 10^9, 1m1091 \leq m \leq 10^9, 2k2×1032 \leq k \leq 2 \times 10^3.

For each test point, the following constraints hold (a blank entry means no special constraint):

::cute-table{tuack}

Test point ID nn mm kk
11 10\leq 10 20\leq 20 =2=2
22 100\leq 100 104\leq 10^4 ^
33 103\leq 10^3
44 104\leq 10^4
55 10\leq 10 20\leq 20 =3=3
66 100\leq 100 104\leq 10^4 ^
77 103\leq 10^3
88 104\leq 10^4
99 10\leq 10 20\leq 20 100\leq 100
1010 100\leq 100 104\leq 10^4 ^
1111 103\leq 10^3 103\leq 10^3
1212 104\leq 10^4
1313 105\leq 10^5 108\leq 10^8 100\leq 100
1414 2×105\leq 2\times 10^5 103\leq 10^3
1515 5×105\leq 5\times10^5
1616 106\leq 10^6 108\leq 10^8 100\leq 100
1717 2×106\leq 2\times 10^6 103\leq 10^3
1818 5×106\leq 5\times 10^6
1919 107\leq 10^7 108\leq 10^8 100100
2020 2×107\leq 2\times10^7 103\leq 10^3
2121 ^
2222 108\leq 10^8
2323 ^
24,2524,25

Hint

This section provides a method to convert a fraction into its corresponding decimal expansion. If you are already familiar with this method, you do not need to read this part.

A fraction can be converted to its corresponding decimal by division, dividing the numerator by the denominator. For some fractions, the division does not terminate; in such cases, during the ongoing division the remainder must eventually repeat. Starting from the remainder corresponding to the ones place of the quotient, suppose that for the first remainder to repeat, the positions of its two occurrences correspond to the aa-th and bb-th digits after the decimal point in the quotient (special case: if one of them corresponds to the ones place, take a=0a = 0; assume a<ba < b). Then its repeating part can be represented by the cycle from the (a+1)(a + 1)-th digit to the bb-th digit after the decimal point.

For example: in base 10, when converting 511\frac 5 {11} to a decimal, the quotient digits starting from the ones place are 4,5,4,4, 5, 4, \ldots, and the corresponding remainders are 6,5,6,6, 5, 6, \ldots. The positions of the first repeated remainder are the ones place and the 22-nd digit after the decimal point, so a=0,b=2a = 0, b = 2.

a=0,b=2a = 0, b = 2 means its repeating part can be represented by the cycle from the 11-st to the 33-rd digits after the decimal point. Thus: 511=0.45454545...=0.4˙5˙\frac 5 {11} = 0.45454545... = 0.\dot4\dot5.

In base 10, when converting 16\frac 1 6 to a decimal, the quotient digits starting from the ones place are 1,6,6,1, 6, 6, \ldots, and the corresponding remainders are 4,4,4,4, 4, 4, \ldots. The positions of the first repeated remainder are the 11-st and 22-nd digits after the decimal point, which means its repeating part can be represented by the 22-nd digit after the decimal point. Thus: 16=0.1666...=0.16˙\frac 1 6 = 0.1666... = 0.1\dot6.

Note: a repeated quotient digit does not necessarily indicate that the repeating cycle has begun.

Translated by ChatGPT 5