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. Flask-Restless初步了解

    Flask-Restless是Flask框架的一个扩展库 1. 功能介绍      通过使用SQLAlchemy或Flask-SQLAlchemy框架定义的数据库模型,提供一个简单的ReSTful A ...

  2. 关于sql中日期操作

    select * from account where  DAYOFWEEK('2019-11-30') =7 limit 10 DAYOFWEEK对应结果: 周日:1 周一:2 周二:3 周三:4 ...

  3. Android 将drawable下的图片转换成bitmap、Drawable

    将drawable下的图片转换成bitmap . Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xxx ...

  4. LeetCode N皇后 & N皇后 II

    题目链接:https://leetcode-cn.com/problems/n-queens/ 题目链接:https://leetcode-cn.com/problems/n-queens-ii/ 题 ...

  5. 【Movie】绿皮书

    今天和室友一起去看了<绿皮书>,一部获得奥斯卡的电影. 起初我是没什么太大期望的,纯粹是因为特价票10块钱,加上身边一个小姐姐的力荐. 看完我觉得,啊不愧是奥斯卡电影啊.推荐. 以下可能会 ...

  6. activiti网关

    activiti中有两种网关:并行网关,排他网关. 排他网关用于任务选择等情况,流程图如下 bpnm代码如下 <?xml version="1.0" encoding=&qu ...

  7. linux常用命令-1系统相关命令

    hostname #计算机名 passwd #修改密码 reboot #重启 shutdown –r now #立刻重启(root用户使用) shutdown –r 10 #过10分钟自动重启(roo ...

  8. 64位 __int 与 long long写法

    在做ACM题时,经常都会遇到一些比较大的整数.而常用的内置整数类型常常显得太小了:其中long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647.而unsig ...

  9. 了解跨站请求伪造CSRF

    参考以下两篇文章: https://www.cnblogs.com/Erik_Xu/p/5481441.html https://www.cnblogs.com/4littleProgrammer/p ...

  10. vue 项目 多选 问题

    近期 vue 项目用到多选功能,引用的elementUI的级联多选 Cascader,但是没有效果. 后来发现是elementUI版本问题,我们项目用的是2.7.2版本,版本太低. 后来 卸载了 重新 ...