#P8308. 〈 TREEのOI 2022 Spring 〉Counting By Ternary
〈 TREEのOI 2022 Spring 〉Counting By Ternary
Background
On the black soil, a small sprout breaks through the ground.
Over several months, it drinks sweet rain and dew, enjoys warm sunlight, and becomes greener and greener.

It grows taller and stronger, as if it is going to reach through the clouds.
It grows into a big tree, longing to go into the sky and see this beautiful world.

Problem Description
Please note that this problem has unusual time and memory limits.
Given a number , build a rooted tree using the following rules:
-
The root node is .
-
For a node , if , then it is a leaf node. Otherwise, its children are, for any and the number of digits of is , , where is the -th digit of the ternary representation of from left to right.
Find the number of leaf nodes of this tree.
Input Format
One line with two integers , meaning .
Output Format
One line with one integer, which is the required answer.
The problem guarantees that the answer fits in the range.
9 1
4
27 1
6
Hint
This problem uses bundled SubTask testing.
| SubTask ID | Score | Special Property |
|---|---|---|
| , | ||
| , | ||
| , | ||
| , |
For of the testdata, (), and it is guaranteed that ().
Translated by ChatGPT 5