E - Good Numbers (hard version) CodeForces - 1249C2

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 (n≤mn≤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 (1≤q≤5001≤q≤500 ) — the number of queries. Then qq queries follow.

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

Output

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

Example

Input

8
1
2
6
13
14
3620
10000
1000000000000000000

Output

1
3
9
13
27
6561
19683
1350851717672992089

题意与思路:

给定一个数n,不小于n的数,满足为3的某些次幂和,要求这些次幂不能重复;

思路:就是先判断给的这个数是不是一个好数,对应的好数3的38次方,所以开数组把每一个3的次幂存储起来以便使用

我认为最重要的一点就是知道3的n次幂必然大于3的前n-1项3的次幂的和;

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int t;
long long n,a[50],book[50];
void f()
{
    a[0]=1;
    for(int i=1; i<=39; i++)//求出前38个3的次幂;
        a[i]=a[i-1]*3;
}
int main()
{
    f();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        long long sum=0;
        memset(book,0,sizeof(book));
        long long h=n;
        for(int i=39; i>=0; i--){//用来判断这个n本身是否为一个好数
            if(n>=a[i]){
                n-=a[i];
                sum+=a[i];//n可以分解成的3的次幂之和
                book[i]=1;//标记n分解成3的次幂
            }
        }
        if(n==0){//如果n为好数就可以直接输出
            printf("%lld\n",h);
            continue;
        }
        long long ans=0;
        for(int i=0; i<=39; i++){
            if(book[i]==1)//在未找到大于等于n的好数之前把前面的数加起来
                ans+=a[i];
            else{
                if(a[i]>=n){
                    printf("%lld\n",a[i]-ans+sum);//在这里我觉得用到的点包含前n-1项3的次幂之和必然小与3的n次方;
                    break;
                }
            }
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值