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

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

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 n find such smallest m (n≤m) that m is a good number.

You have to answer qq independent queries.

Input

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

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

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 题意:
对于每组数据, 给出一个n,要求求出大于等于n的m,m需要满足的条件是:由3的次方相加组成,且每个3的次方最多只能出现一次。 注意:
对于次方问题,最好用快速幂进行运算,运用pow会造成奇怪错误。 法一:三进制
思路:
由于求的答案与3的次方有关,所以可以把每一个给出来的n转换成三进制存到数组里再去进行判断,
比如给出一个n=14,转换成三进制的话就是112,从高位往低位进行判断,
如果存放进制的数组里面只含有1和0的话,说明该数是3的次方,直接输出即可
否则可以进行判断,从低位往高位找,找到第一个2记录下标且break
 #include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=1e4+;
typedef unsigned long long ull; ull a[N]; ull ksm(ull x,ull n)
{
ull res=;
while(n>)
{
if(n&)
res=res*x;
x=x*x;
n>>=;
}
return res;
} int main()
{
int t;
ull n;
scanf("%d",&t);
while(t--)
{
memset(a,,sizeof(a));
scanf("%llu",&n);
int p=;
ull ans=n;
while(n)
{
a[p++]=n%;
n=n/; }
// for(int i=0;i<p;i++)
// printf("%d",a[i]);
// printf("\n");//211
//从低位往高位找,找到第一个2记录下标且break
int k=-;
for(int i=p-;i>=;i--)
{
if(a[i]==)
{
k=i;
break;
}
}
if(k==-)
{
printf("%llu\n",ans);
continue;
}
// printf("%d***\n",k);
for(int i=k;i<p;i++)
{
if(a[i]==)
{
a[i]=;
a[i+]++;
// break;
// printf("%d***%d\n",i+1,a[i+1]);
}
} if(a[p]!=)
p++;
ull sum=;
for(int i=p-;i>=k;i--)
{
sum=sum+a[i]*ksm(,i);
}
printf("%llu\n",sum); }
return ;
}

法二:暴力

思路:

  • 首先数据给出的范围是1018,所以可以通过打表计算出3的几次方会超过该范围,打表得出是38,所以数字开到40就ok
ll sum=;
int k;
for(int i=;i<N;i++)
{
sum=sum*;
if(sum>=1e18)
{
printf("%d\n",i);//i==38
break;
}
}
  • 把3的次方存入a数组中。
  • 再来,开始分析题意,题意要求求的是满足条件最小的m,所以如果将a数组从小往大遍历的话,找到的m不一定是最小的,所以需要将a数组从大往小遍历,再把小于等于n的第一个数减掉。
  • 还需要知道的一点就是30+31+32+...+3n-1<3n,所以最后再从前往后遍历,找到没有被标记过的数,注意30需要特判。
 #include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=1e4+;
typedef long long ll; ll a[];
bool book[N]; ll ksm(ll x,ll n)
{
ll res=;
while(n>)
{
if(n&)
res=res*x;
x=x*x;
n>>=;
}
return res;
} int main()
{
for(int i=; i<=; i++)
a[i]=ksm(,i);
int t;
ll n;
scanf("%d",&t);
while(t--)
{
memset(book,,sizeof(book));
scanf("%lld",&n);
ll sum=;
while()
{
int flag=;
for(int i=; i>=; i--)
{
if(a[i]<=n&&book[i]==)//第一个找到的就是最大的
{
sum+=a[i];
flag=;
book[i]=;
n-=a[i];
break;
}
}
if(flag==)
break;
}
if(n==)
{
printf("%lld\n",sum);
continue;
}
ll kk;
int k;
for(int i=;i<=;i++)
{
if(book[i]==)
{
kk=a[i];
k=i;
break;
}
}
sum=sum+kk;
for(int i=;i<k;i++)
sum-=a[i];
printf("%lld\n",sum);
}
return ;
}

CodeForces-1249C2-Good Numbers (hard version) -巧妙进制/暴力的更多相关文章

  1. PAT甲级——1100 Mars Numbers (字符串操作、进制转换)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90678474 1100 Mars Numbers (20 分) ...

  2. HDU 1887 Weird Numbers(负数的进制转化)

    题目要求有两种情况,第一种from情况,正常输出即可,很简单.第二种是to情况,给一个数字,输出负进制R的原码,这个有点小麻烦...解决方法如下; 首先,把这个数n按正常方式展开,形式如下: .... ...

  3. CodeForces 1B-字符串,进制转换与数学

    一个萌新的成长之路 Background 同学们都回家了,只有我和wjh还有邢神在机房敲代码,吃random口味的方便面-- Description Translated by @PC_DOS fro ...

  4. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  5. Codeforces Round #538 (Div. 2) C 数论 + 求b进制后缀零

    https://codeforces.com/contest/1114/problem/C 题意 给你一个数n(<=1e8),要你求出n!在b进制下的后缀零个数(b<=1e12) 题解 a ...

  6. 蓝桥杯 问题 1110: 2^k进制数 (排列组合+高精度巧妙处理)

    题目链接 题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2 ...

  7. Codeforces 552C Vanya and Scales(进制转换+思维)

    题目链接:http://codeforces.com/problemset/problem/552/C 题目大意:有101个砝码重量为w^0,w^1,....,w^100和一个重量为m的物体,问能否在 ...

  8. 暴力/进制转换 Codeforces Round #308 (Div. 2) C. Vanya and Scales

    题目传送门 /* 题意:问是否能用质量为w^0,w^1,...,w^100的砝码各1个称出重量m,砝码放左边或在右边 暴力/进制转换:假设可以称出,用w进制表示,每一位是0,1,w-1.w-1表示砝码 ...

  9. Codeforces 878 E. Numbers on the blackboard

    Codeforces 878 E. Numbers on the blackboard 解题思路 有一种最优策略是每次选择最后面一个大于等于 \(0\) 的元素进行合并,这样做完以后相当于给这个元素乘 ...

随机推荐

  1. line-height继承

    父元素设置ling-height;子元素继承父元素的line-height 1.ling-height:固定像素 body{ font-size:20px; line-height: 24px; } ...

  2. python json格式参数遍历所有key、value 及替换key对于的value

    1.对于接口自动化测试,一般接口以json形式发送返回,往往我们就需要遍历json文件中所有key,value以及修改替换key对于的value. 例如json发送/接收的文件: SendRegist ...

  3. 执行sudo apt-get update报错

    1.在官网源https://mirrors.ustc.edu.cn/repogen/下载对应版本最新的源,比如我是16.04版本的ubuntu,对应下载的是这个 2.下载sources.list完成之 ...

  4. Cocos2d-x在Windows平台环境的搭建

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. windows平台下配置Cocos2d-X引擎所需的压缩文件和可执行文件,如下: 下载链接: adt:    http://tools.a ...

  5. 使用mybatis时,sql查出来的结果映射不进逆向工程生成的该模型

    为了逆向工程生成驼峰效果,我们会在两个单词中间加_,但是在mybatis中,写sql语句时,要加别名,使别名和你逆向工程生成的该数据模型里面的属性名一致,否则查出来的结果映射不进去.

  6. Linux设置以root用户开机自动登录桌面

    目录 Ubuntu 18.04系统下设置 Redhat7.6系统下设置 Ubuntu 18.04系统下设置 1. 允许使用root用户登录桌面    Ubuntu默认不允许使用root用户登录桌面的, ...

  7. 【CSS】三栏布局的经典实现

    要求:自适应宽度,左右两栏固定宽度,中间栏优先加载: <!DOCTYPE html> <html> <head> <title>layout</t ...

  8. 【LeetCode】拓扑排序

    [207] Course Schedule 排课问题,n门课排课,有的课程必须在另外一些课程之前上,问能不能排出来顺序. 题解:裸的拓扑排序.参考代码见算法竞赛入门指南这本书. class Solut ...

  9. VMware下Ubuntu全屏显示

      开始是这样的 完了之后应该是这样的 1.点开菜单栏的 虚拟机---------> 安装VMware Tools 安装完了之后桌面会出现一个这样的图标 双击这个DVD,进去之后左侧目录出现了 ...

  10. js-打印九九乘法表

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...