Good Numbers (hard version) CodeForces - 1249C2

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

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.

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).

For example:

30 is a good number: 30=3^3+ 3^1,
1 is a good number: 1=3^0,
12 is a good number: 12=3^2 + 3^1,
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),
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),
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).

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.

For the given positive integer n find such smallest m (n≤m) that m is a good number.

You have to answer q independent queries.

Input

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

The only line of the query contains one integer n (1≤n≤10^18).
Output

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

Example
Input

8
1
2
6
13
14
3620
10000
1000000000000000000

Output

1
3
9
13
27
6561
19683
1350851717672992089

题意:
输出大于输入数据所给的最小数(3的n次方且n不能重复的数)

思路:
将输入数据转为三进制数,从高位往低位找到第一个为2的位置,再从第一个为2的位置向高位找到第一个为0的位置,将这个为0的位置的值改为1并舍去所有低位的数,这个数即是我们要输出的数。

代码:

#include <stdio.h>
#include <string.h>
long long a[500];
long long good(long long b,long long c)
{
    long long s=1;
    while(c)
    {
        if(c%2==1)
            s*=b;
        b*=b;
        c=c/2;
    }
    return s;
}
int main(void)
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(a,0,sizeof(a));
        long long n;
        scanf("%lld",&n);
        long long i=0,j,k,s=n;
        while(n>0)
        {
            a[i++]=n%3;
            n/=3;
        }
        j=i-1;
        for(; j>=0; j--)
        {
            if(a[j]==2)
                break;
        }
        if(j==-1)
            printf("%lld\n",s);
        else
        {
            k=j+1;
            for(; k<=i-1; k++)
            {
                if(a[k]==0)
                {
                    a[k]=1;
                    break;
                }
            }
            long long s1=0;
            if(k==i)
                a[i]=1;
            for(j=k; j<=i; j++)
            {
                if(a[j]==1)
                    s1+=good(3,j);
            }
            printf("%lld\n",s1);
        }
    }
    return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值