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 (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 qqqueries 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

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[] ; int main()
{
int t ;
scanf("%d" , &t);
while(t--)
{
ll n ;
scanf("%lld" , &n);
memset(a , , sizeof(a));
int cnt = ;
ll tmp = n ;
while(tmp)
{
a[cnt++] = tmp % ;
tmp /= ; }
int pos = - ;
for(int i = ; i < cnt ; i++)
{
if(a[i] >= )
{
pos = i ;
a[i] = ;
a[i+]++;
}
}
if(pos != -)
{
for(int i = ; i < pos ; i++)//确保最小
{
a[i] = ;
}
} ll ans = ;
ll res = ;
for(int i = ; i <= cnt ; i++)//注意取等
{
ans += a[i] * res;
res *= ; }
cout << ans << endl ; } return ;
}

Good number(3进制)的更多相关文章

  1. python实现进制转换(二、八、十六进制;十进制)

    python实现进制转换(二.八.十六进制:十进制) (一)十进制整数转为二.八.十六进制 1.format实现转换>>> format(2,"b") # (10 ...

  2. 枚举 + 进制转换 --- hdu 4937 Lucky Number

    Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)To ...

  3. NUMBER BASE CONVERSION(进制转换)

    Description Write a program to convert numbers in one base to numbers in a second base. There are 62 ...

  4. poj 1220 NUMBER BASE CONVERSION(短除法进制转换)

    题目连接:1220 NUMBER BASE CONVERSION 题目大意:给出两个进制oldBase 和newBase, 以及以oldBase进制存在的数.要求将这个oldBase进制的数转换成ne ...

  5. LeetCode 405. Convert a Number to Hexadecimal (把一个数转化为16进制)

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...

  6. PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)

    1019 General Palindromic Number (20 分)   A number that will be the same when it is written forwards ...

  7. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) (进制转换,回文数)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  8. 11 JavaScript Number原始值&对象&科学记数法&范围&进制转换&溢出Infinity&NaN

    JavaScript Number对象 是经过封装的能处理数字值的对象 由Number()构造器创建 只有一种数字类型 可以使用也可以不使用小数点书写数字 JavaScript原始值与对象: 在Jav ...

  9. HDU 4937 Lucky Number (数学,进制转换)

    题目 参考自博客:http://blog.csdn.net/a601025382s/article/details/38517783 //string &replace(iterator fi ...

随机推荐

  1. flask基础之一

    flask基础之一 hello world #从flask这个包中导入Flask这个类 #Flask这个类是项目的核心,以后的很多操作都是基于这个类的对象 #注册url,注册蓝图都是这个类的对象 fr ...

  2. 【机器人M号】题解

    题目 题目描述 3030年,Macsy正在火星部署一批机器人. 第1秒,他把机器人1号运到了火星,机器人1号可以制造其他的机器人. 第2秒,机器人1号造出了第一个机器人--机器人2号. 第3秒,机器人 ...

  3. Win10离线安装.NET Framework 3.5的方法补充(附cab格式离线安装包下载) - 转载

    MS酋长很早以前已经分享了<Win10离线安装.NET Framework 3.5的方法技巧>,同时分享了exe格式的.NET Framework 3.5离线安装包下载地址.但有部分网友反 ...

  4. 【leetcode】Smallest Rotation with Highest Score

    题目如下: Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], ...

  5. 【leetcode】313. Super Ugly Number

    题目如下: 解题思路:总结一下这么几点,一出一进,优先级队列排序,保证每次输出的都是当前的最小值.解法大致如图: 代码如下: #include<map> #include<queue ...

  6. java mar --->JSONArray.fromObject

    <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</art ...

  7. 最大 k 乘积问题 ( 经典区间DP )

    题意 : 设 NUM 是一个 n 位十进制整数.如果将 NUM 划分为 k 段,则可得到 k 个整数.这 k 个整数的乘积称为 NUM 的一个 k 乘积.试设计一个算法,对于给定的 NUM 和 k,求 ...

  8. layer.confirm

    layer.confirm('确定不选择花车?', { title: false, btn: ['确定','取消'] //按钮 }, function(ind){ layer.close(ind); ...

  9. Solr分组查询

     项目中需要实时的返回一下统计的东西,因此就要进行分组,在获取一些东西,代码拿不出来,因此分享一篇,还是很使用的. facet搜索 /** * * 搜索功能优化-关键词搜索 * 搜索范围:商品名称.店 ...

  10. 重新理解了重定向,利用重定向可以防止用户重复提交表单(兼谈springmvc重定向操作)

    自己用springmvc框架有一段时间了,但是都还一直分不清楚什么时候应该用转发,什么时候应该用重定向.可能用转发的情形太多了,以致于自己都忘记了还有重定向. 当用户提交post请求之后,刷新页面就会 ...