zoukankan      html  css  js  c++  java
  • C2. Good Numbers (hard version)

    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=33+31,
    • 11 is a good number: 1=301=30,
    • 1212 is a good number: 12=32+3112=32+31,
    • but 22 is not a good number: you can't represent it as a sum of distinct powers of 33 (2=30+302=30+30),
    • 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=32+32+30=32+31+31+31+30 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=32+32+30+30 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≤m) that mm is a good number.

    You have to answer qq independent queries.

    Input

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

    The only line of the query contains one integer nn (1n10181≤n≤1018).

    Output

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

    Example
    input
    Copy
    8
    1
    2
    6
    13
    14
    3620
    10000
    1000000000000000000
    
    output
    Copy
    1
    3
    9
    13
    27
    6561
    19683
    1350851717672992089
    

     与拆分2进制同样的道理,这里,3进制里每一位上必须小于等于1,如果大于1,则此位及之前全部清为0,在此位之后出现的第一个0改成1

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    #include <unordered_set>
    #include <unordered_map>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const double eps = 1e-6;
    const int mod = 998244353;
    const int N = 1e6 + 5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    
    int main()
    {
        ll T;
        cin >> T;
        while (T--)
        {
            ll n;
            cin >> n;
            ll tmp = n;
            vector<ll> a;
            while (tmp)
            {
                a.push_back(tmp % 3);
                tmp /= 3;
            }
            int pos = 0,fg1=0;
            for (int i = 0; i < a.size(); i++)
            {
                if (a[i] > 1)
                    pos = i,a[i]=0,fg1=1;
            }
            if (fg1)
            {
                int fg = 1;
                for (int i = 0; i < a.size(); i++)
                {
                    if (a[i] == 0 && i>pos)
                    {
                        a[i] = 1;
                        fg = 0;
                        break;
                    }
                    else
                        a[i] = 0;
                }
                if (fg)
                    a.push_back(1);
            }
            ll res = 0;
            for (int i = a.size()-1; i >=0 ; i--)
            {
                res = res * 3 + a[i];
            }
            cout << res << endl;
        }
        return 0;
    }
  • 相关阅读:
    Unique Paths II
    Subsets II
    Subsets
    Jump Game II
    Jump Game
    Valid Sudoku
    Valid Parentheses
    Length of Last Word
    Trapping Rain Water
    Sum Root to Leaf Numbers
  • 原文地址:https://www.cnblogs.com/dealer/p/13041815.html
Copyright © 2011-2022 走看看