CF1249C2.Good Numbers (hard version)

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The only difference between easy and hard versions is the maximum value of nn .

You are given a positive integer number nn . You really love good numbers so you want to find the smallest good number greater than or equal to nn .

The positive integer is called good if it can be represented as a sum of distinct powers of 33 (i.e. no duplicates of powers of 33 are allowed).

For example:

  • 3030 is a good number: 30=33+3130 = 3^3 + 3^1 ,
  • 11 is a good number: 1=301 = 3^0 ,
  • 1212 is a good number: 12=32+3112 = 3^2 + 3^1 ,
  • but 22 is not a good number: you can't represent it as a sum of distinct powers of 33 ( 2=30+302 = 3^0 + 3^0 ),
  • 1919 is not a good number: you can't represent it as a sum of distinct powers of 33 (for example, the representations 19=32+32+30=32+31+31+31+3019 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0 are invalid),
  • 2020 is also not a good number: you can't represent it as a sum of distinct powers of 33 (for example, the representation 20=32+32+30+3020 = 3^2 + 3^2 + 3^0 + 3^0 is invalid).

Note, that there exist other representations of 1919 and 2020 as sums of powers of 33 but none of them consists of distinct powers of 33 .

For the given positive integer nn find such smallest mm ( nmn \le m ) that mm is a good number.

You have to answer qq independent queries.

输入格式

The first line of the input contains one integer qq ( 1q5001 \le q \le 500 ) — the number of queries. Then qq queries follow.

The only line of the query contains one integer nn ( 1n10181 \le n \le 10^{18} ).

输出格式

For each query, print such smallest integer mm (where nmn \le m ) that mm is a good number.

输入输出样例

  • 输入#1

    8
    1
    2
    6
    13
    14
    3620
    10000
    1000000000000000000
    

    输出#1

    1
    3
    9
    13
    27
    6561
    19683
    1350851717672992089
    
首页