luogu#P9154. 「GLR-R4」立夏

「GLR-R4」立夏

Background

  "When the spring flowers fade away, there is no need to regret; the summer trees are lush and pleasantly shaded."


  The post-contest meetup that was agreed on with Old V is finally happening!

  It is rare to be together with the juniors, so Tianyi and Ayling naturally will not miss this good chance, and they definitely will not let go of the chance to rua the fox-eared Constellation’s ears that they have wanted to for a long time!

  "Um... Tianyi..."

  Tianyi and the fox-eared Constellation on her lap tilt their heads at the same time and look at someone called Ayling, a jealous "vinegar jar".

  "Sister Ayling, sister Ayling, it hurts!"

  So when Tianyi is tying the little fox’s hair, the little fox’s ears have already been揉蔫啦 (rou nian la, kneaded until limp)!


  Start of Summer  "In two or three quick steps, you discover the transparent summer chapter arriving right on time."

Problem Description

  This problem provides a brief statement.

  The gauze hairband that Tianyi prepared for the fox-eared Constellation is made of white and purple small squares. Small squares of the same size are connected into a long enough strip. Let the position that Tianyi is pinching with her hand be square 00. To the right are square 11, square 22, and so on; to the left are square 1-1, square 2-2, and so on. Tianyi folds the hairband in half. Then square 1-1 overlaps with square 11, square 2-2 overlaps with square 22, ... square k-k overlaps with square kk (kk is a positive integer). In particular, we consider square 00 to remain as it is: it does not overlap with any other square, and it does not overlap with itself.

  Because the hairband is semi-transparent, after folding, the squares on the folded hairband may show three colors: white, light purple, and dark purple. Two white squares overlapping appear white; one purple and one white overlapping appear light purple; two purple squares overlapping appear dark purple. In particular, if square 00 was originally white, then it is still white after folding; otherwise, if square 00 was originally purple, then it becomes light purple after folding.

  If we record white as 00, light purple as 11, and dark purple as 22, and take folded square 00 as the least significant digit, then write down the digit corresponding to each square’s color in order, we obtain a long ternary integer, denoted as xx. Now, Tianyi tells you the value of xx. Can you compute how many different styles the hairband before folding could have? We say two hairbands have different colors if and only if there exists an integer kk such that the color of square kk on the two hairbands is different.

  There are many possible hairband styles, and you need to answer the result for each of TT given values of xx.

Brief statement

  For a set SS that contains integers, define its weight as aS3a\sum_{a\in S}3^{|a|} (that is, enumerate each element aa in SS, compute 3a3^{|a|}, and sum them up). Given a non-negative integer xx, compute how many sets have weight equal to xx. Note that a set cannot contain duplicate elements.

Input Format

The first line contains an integer TT, indicating the number of test cases you need to process.

The next TT lines each contain an integer xx, representing the ternary number corresponding to the colors of the hairband after folding. Note that xx is given in decimal.

Output Format

Output TT lines. The ii-th line contains one integer, representing the number of valid ways for the ii-th given xx.

2
12
2
4
0

Hint

Explanation for Sample #1

When x=12x=12, there are four possible hairband styles. The positions of the purple squares are {1,2}\{-1,-2\}, {1,2}\{-1,2\}, {1,2}\{1,-2\}, {1,2}\{1,2\}, respectively.

When x=2x=2, there is no hairband style that satisfies the condition, so output 00.

Constraints

For 100%100\% of the testdata, 1T105,0x10181 \leq T \leq 10^5, 0 \leq x \leq 10^{18}.

For different test points, the following constraints apply:

Test Point ID xx Special Property
131\sim3 310\leq 3^{10} None
44 1018\leq 10^{18} xmod3=1x \bmod 3 = 1
55 xmod3=2x \bmod 3=2
66 xmod3=0x \bmod 3 =0
7107\sim10 None

Translated by ChatGPT 5