zoukankan      html  css  js  c++  java
  • Good number(3进制)

    C2. Good Numbers (hard version)
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 qqqueries 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
    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdio.h>
    #include <queue>
    #include <stack>;
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 998244353
    #define PI acos(-1)
    using namespace std;
    typedef long long ll ;
    ll a[109] ;
    
    
    
    int main()
    {
        int t ;
        scanf("%d" , &t);
        while(t--)
        {
            ll n ;
            scanf("%lld" , &n);
            memset(a , 0 , sizeof(a));
            int cnt = 0 ;
            ll tmp = n ;
            while(tmp)
            {
                a[cnt++] = tmp % 3 ;
                tmp /= 3 ;
    
            }
            int pos = -1 ;
            for(int i = 0 ; i < cnt ; i++)
            {
                if(a[i] >= 2)
                {
                    pos = i ;
                    a[i] = 0 ;
                    a[i+1]++;
                }
            }
            if(pos != -1)
            {
                for(int i = 0 ; i < pos ; i++)//确保最小
                {
                    a[i] = 0 ;
                }
            }
    
            ll ans = 0 ;
            ll res = 1 ;
            for(int i = 0 ; i <= cnt ; i++)//注意取等
            {
                ans += a[i] * res;
                res *= 3 ;
    
            }
            cout << ans << endl ;
    
    
        }
    
        return 0 ;
    }
  • 相关阅读:
    ubuntu中apt-get安装与默认路径
    css计数器
    jq实现多级菜单
    video文件格式说明(笔记)
    css文字闪烁效果
    video设置视频的播放位置(本例中实现效果是视频第一次播放完成后,接下来中从视频的中间部位开始循环播放)
    css3鼠标经过出现转圈菜单(仿)
    jq弹框 (1)内容自适应宽度 2(内容框显示,几秒后自动消失)
    jq实现 元素显示后 点击页面的任何位置除元素本身外 隐藏元素
    nginx https配置记录
  • 原文地址:https://www.cnblogs.com/nonames/p/11749966.html
Copyright © 2011-2022 走看看