#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 xx, build a rooted tree using the following rules:

  • The root node is 0,x\lang 0, x \rang.

  • For a node i,j\lang i, j \rang, if j<3j < 3, then it is a leaf node. Otherwise, its children are, for any 1k1 \le k and the number of digits of jj is k\ge k, jk,k\lang j_k, k \rang, where jkj_k is the kk-th digit of the ternary representation of jj from left to right.

Find the number of leaf nodes of this tree.

Input Format

One line with two integers p,qp, q, meaning x=pqx = p^q.

Output Format

One line with one integer, which is the required answer.

The problem guarantees that the answer fits in the int64\tt int64 range.

9 1
4
27 1
6

Hint

This problem uses bundled SubTask testing.

SubTask ID Score Special Property
00 1010 p315p \le 3^{15}, q=1q = 1
11 p335p \le 3^{35}, q=1q = 1
22 2020 p=3p = 3, q315q \le 3^{15}
33 6060 p=3p = 3, q335q \le 3^{35}

For 100%100\% of the testdata, pq3335p^q \le 3^{3^{35}} (10109<3335<102.5×10910^{10^9} \lt 3^{3^{35}} \lt 10^{2.5 \times 10^9}), and it is guaranteed that p=3lp = 3^l (lN+l \in \mathbb N^+).

Translated by ChatGPT 5