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 ...
随机推荐
- CodeMix使用教程:构建自定义DevStyle主题
[MyEclipse CI 2019.4.0安装包下载] DevStyle主题允许开发人员自定义工作台,无论是喜欢带有明亮图标的浅色背景还是带有柔和色彩的神色背景,开发人员都可以将工作台调整到适合的色 ...
- vue项目中 指令 v-html 中使用过滤器filters功能
转载于简书 链接:http://www.jianshu.com/p/29b7eaabd1ba 问题 2.0 filters only work in mustache tags and v-bind. ...
- 如何将上个SQL的结果作为参数传递给下个SQL
如何将上个SQL的结果作为参数传递给下个SQL: ##source认证 kinit认证 source /home/omm/ficlient/bigdata_env kinit -k -t /ETL/c ...
- margin-top百分比问题
(1)其实margin-top和margin-bottom的百分比,一般是按容器元素的宽度而不是高度来计算的,padding同理.
- HBase设计规范(转载)
在这里讲什么设计规范实在是有些不知天高地厚,毕竟笔者本人也只是一个大数据技术的初学者,断然不敢制订什么设计规范的,所以请原谅我的狂妄,这个设计规范,只是本人对自己制订的,与别人无关. 之前,HBase ...
- 6377. 【NOIP2019模拟2019.10.05】幽曲[埋骨于弘川]
题目描述 题解 随便bb 详细题解见 https://www.cnblogs.com/coldchair/p/11624979.html https://blog.csdn.net/alan_cty/ ...
- document.referer
参考文章: 深入理解document.referrer的用法
- 使用struts2的内置标签,采用submit()提交表单时,浏览器报404
如图 url是没有问题的,结果我将提交方式改为get时,发现有2个参数的name值是一样的,如下图, 解决方法:将name的值修改就OK了.
- 如何降低Vue.js项目中Webpack打包文件的大小?
https://blog.csdn.net/maray/article/details/50988500?utm_source=blogxgwz0 import Blur from ‘vux/src/ ...
- mktime夏令时处理
https://www.cnblogs.com/dongzhiquan/archive/2011/11/05/2237075.html 我们的最终目的是把字符串格式的时间转换为内部使用的“日历时间”, ...