1497.Cantor Function

Time Limit: 1s Memory Limit: 256MB

Little X learned about the Cantor function from their Advanced Mathematics class. And he thinks it is interesting, so he wants to calculate this function.

The Cantor function is a function $C:[0,1] \rightarrow [0,1]$ that is defined as follows:

  • Write the ternary expansion of $x\in [0,1]$. That is, $x=\sum_{n=1}^{\infty}\frac{a_n}{3^n}$, where each $a_n$ is either 0 or 2.
  • Define the sequence ${y_n}_{n=1}^{\infty}$ by deleting all of the 1s from the ternary expansion of $x$ after the $n$th digit and replacing all of the 2s with 1s. That is, $y_n = \sum_{i=1}^{n}\frac{b_i}{2^i}$ where $b_i = 0$ if the $i$th digit in the ternary expansion of $x$ after the first $n$ digits is 0, and $b_i=1$ if it is 2.
  • Define $C(x)$ to be the limit of the sequence ${y_n}_{n=1}^{\infty}$.

The Cantor function has several properties:

  • $C$ is continuous, non-decreasing, and maps $[0,1]$ onto $[0,1]$.
  • $C$ is constant on each interval $[a,b] \subset [0,1]$ that is not in the middle-third Cantor set.
  • $C$ is strictly increasing on each interval $(a,b) \subseteq [0,1]$ that is entirely contained in the middle-third Cantor set.
  • $C$ is differentiable almost everywhere, and its derivative is zero almost everywhere on $[0,1]$.

These properties make the Cantor function an important example in mathematical analysis and fractal geometry.

To calculate it for a specific value of $x$, you can follow these steps:

  • item Write the ternary expansion of $x$. For example, if $x = \frac{13}{18}$, the ternary expansion would be $0.20111..._{(3)}$
  • Delete all digit after the first occurrences of 1 from the ternary expansion. In the case of $x = \frac{13}{18}$, the resulting sequence would be $0.201_{(3)}$
  • Replace all digits 2 with 1. In the case of $x = \frac{13}{18}$, the resulting sequence would be $0.101_{(3)}$
  • Convert the resulting sequence back to decimal representation. The result is the value of the Cantor function at $x$. In the case of $x = \frac{13}{18}$, the resulting sequence would be $0.101_{(2)} = \frac{5}{8}$

Input Format(From the terminal/stdin)

Input one line that cantains two integers $a$, $b$ ($1 \le a < b \le 1e18$), representing the value of $x = \frac{a}{b}$ for which Little X want to calculate the Cantor function.

Condition1: Given a fraction $ \frac{a}{b} = 0.eooo..._{(3)}$, where the digits in the repeating block are denoted by 'o'. For example, $\frac{13}{18} = 0.20111..._{(3)}$, where $e = 20$ and $o = 1$. It is guaranteed that $|e| + |o| \leq 60$ for the input $\frac{a}{b}$.

Condition2: Given a fraction $\frac{a}{b}$, the number of digits before the first digit '1' is $c$. For example, for $\frac{13}{18} = 0.20111..._{(3)}$, the number of digits before the first digit '1' is 2. It is guaranteed that $c \leq 60$ for the input $\frac{a}{b}$.

Note: For every input $\frac{x}{y}$, it is guaranteed to satisfy at least one of the prementioned conditions.

Output Format(To the terminal/stdout)

The output should only contain two integers $p$ and $q$($1 \le p \le q, gcd(b,q) = 1$), representing the fraction $\frac{p}{q}$ such that $C(\frac{a}{b}) = \frac{p}{q}$.

Sample Input 1

Copy
13 18
  ·  \n

Sample Output 1

Copy
5 8
 · \n

Sample Input 2

Copy
3 4
 · \n

Sample Output 2

Copy
2 3
 · \n

Sample Input 3

Copy
25 36
  ·  \n

Sample Output 3

Copy
7 12
 ·  \n

Hints

For the 2nd example, $\frac{3}{4} = 0.202020..._{(3)} \xrightarrow{Delete} 0.202020..._{(3)} \xrightarrow{Replace} 0.101010..._{(3)} \xrightarrow{Review} 0.101010..._{(2)} = \frac{2}{3}$.

Source: The 2024 Hunan University Programming Contest

Submit

请先 登录

© 2025 FAQs