codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1249C2.md (1915B)


      1 Source: https://codeforces.com/problemset/problem/1249/C2
      2 
      3 C2. Good Numbers (hard version)
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 The only difference between easy and hard versions is the maximum value of $n$.
     22 
     23 You are given a positive integer number $n$. You really love good numbers so you want to find the smallest good number greater than or equal to $n$.
     24 
     25 The positive integer is called good if it can be represented as a sum of distinct powers of $3$ (i.e. no duplicates of powers of $3$ are allowed).
     26 
     27 For example:
     28 
     29 * $30$ is a good number: $30 = 3^3 + 3^1$,
     30 * $1$ is a good number: $1 = 3^0$,
     31 * $12$ is a good number: $12 = 3^2 + 3^1$,
     32 * but $2$ is not a good number: you can't represent it as a sum of distinct powers of $3$ ($2 = 3^0 + 3^0$),
     33 * $19$ is not a good number: you can't represent it as a sum of distinct powers of $3$ (for example, the representations $19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0$ are invalid),
     34 * $20$ is also not a good number: you can't represent it as a sum of distinct powers of $3$ (for example, the representation $20 = 3^2 + 3^2 + 3^0 + 3^0$ is invalid).
     35 
     36 Note, that there exist other representations of $19$ and $20$ as sums of powers of $3$ but none of them consists of distinct powers of $3$.
     37 
     38 For the given positive integer $n$ find such smallest $m$ ($n \le m$) that $m$ is a good number.
     39 
     40 You have to answer $q$ independent queries.
     41 
     42 Input
     43 
     44 The first line of the input contains one integer $q$ ($1 \le q \le 500$) — the number of queries. Then $q$ queries follow.
     45 
     46 The only line of the query contains one integer $n$ ($1 \le n \le 10^{18}$).
     47 
     48 Output
     49 
     50 For each query, print such smallest integer $m$ (where $n \le m$) that $m$ is a good number.
     51 
     52 Example
     53 
     54 Input
     55 
     56 ```
     57 8
     58 1
     59 2
     60 6
     61 13
     62 14
     63 3620
     64 10000
     65 1000000000000000000
     66 ```
     67 
     68 Output
     69 
     70 ```
     71 1
     72 3
     73 9
     74 13
     75 27
     76 6561
     77 19683
     78 1350851717672992089
     79 ```