Good number(3进制)
2 seconds
256 megabytes
standard input
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.
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).
For each query, print such smallest integer mm (where n≤mn≤m) that mm is a good number.
8
1
2
6
13
14
3620
10000
1000000000000000000
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进制)的更多相关文章
- python实现进制转换(二、八、十六进制;十进制)
python实现进制转换(二.八.十六进制:十进制) (一)十进制整数转为二.八.十六进制 1.format实现转换>>> format(2,"b") # (10 ...
- 枚举 + 进制转换 --- hdu 4937 Lucky Number
Lucky Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- NUMBER BASE CONVERSION(进制转换)
Description Write a program to convert numbers in one base to numbers in a second base. There are 62 ...
- poj 1220 NUMBER BASE CONVERSION(短除法进制转换)
题目连接:1220 NUMBER BASE CONVERSION 题目大意:给出两个进制oldBase 和newBase, 以及以oldBase进制存在的数.要求将这个oldBase进制的数转换成ne ...
- 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 ...
- PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)
1019 General Palindromic Number (20 分) A number that will be the same when it is written forwards ...
- 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 ...
- 11 JavaScript Number原始值&对象&科学记数法&范围&进制转换&溢出Infinity&NaN
JavaScript Number对象 是经过封装的能处理数字值的对象 由Number()构造器创建 只有一种数字类型 可以使用也可以不使用小数点书写数字 JavaScript原始值与对象: 在Jav ...
- HDU 4937 Lucky Number (数学,进制转换)
题目 参考自博客:http://blog.csdn.net/a601025382s/article/details/38517783 //string &replace(iterator fi ...
随机推荐
- thinkphp5 yii2 laravel5.1 框架性能压测对比图
nginx+php7环境,opcache已经开启,每测试一个框架都重启服务器并且预热访问三次,压测工具ab.exe. laravel,thinkphp,yii都已关闭debug,该做的优化命令都搞了, ...
- 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\......”--“拒绝访问。 ”错误
1.通常的解决方法:原因是由于系统目录下的Temp目录无相应的权限所致,具体操作如下: C:\Windows\temp-->属性-->安全-->编辑-->添加NETWORK S ...
- 【leetcode】1162. As Far from Land as Possible
题目如下: Given an N x N grid containing only values 0 and 1, where 0 represents water and 1 represents ...
- php命令行工具
https://jingyan.baidu.com/article/37bce2beb6e5681002f3a20f.html
- 4.16中Montage的一些变化
用4.16版本跟着网上的几篇Montage教程(分别是对应4.6和4.8版本)做,遇到各种各样的问题,经过各种搜索和分析,最终终于搞定了. 4.16版的Montage和之前有了不小的变化,总结如下: ...
- SpringMVC传参注解@RequestParam,@RequestBody,@ResponseBody,@ModelAttribute
参考文档:https://blog.csdn.net/walkerjong/article/details/7946109 https://www.cnblogs.com/daimajun/p/715 ...
- python3下multiprocessing、threading和gevent性能对比----暨进程池、线程池和协程池性能对比
python3下multiprocessing.threading和gevent性能对比----暨进程池.线程池和协程池性能对比 标签: python3 / 线程池 / multiprocessi ...
- slider组件
slider组件,是一个强大的滑动选择器组件: 属性: min:类型 数字 滑动选择器的(范围)最小值 max:类型 数字 滑动选择器的(范围)最大值 step:类型 数字 步长(滑一次走的距离) ...
- ORACLE DG 库参数db_file_name_convert和log_file_name_convert的作用
https://www.cnblogs.com/xqzt/p/5089826.html ORACLE DG 库参数db_file_name_convert和log_file_name_convert的 ...
- python 2和3 字符编码
在字符编码问题上,python2 和python3 还是有点不同的.今日写篇博客,彻底理清这个问题.. 字符编码问题的由来: 这要从计算发展历史来看待这个问题了,一开始,歪果仁使用ASCII码,8位( ...