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 ...
随机推荐
- python-类对象的比较
#类对象的比较 class Person: def __init__(self,age,height): self.age=age self.height=height def __eq__(self ...
- 【机器人M号】题解
题目 题目描述 3030年,Macsy正在火星部署一批机器人. 第1秒,他把机器人1号运到了火星,机器人1号可以制造其他的机器人. 第2秒,机器人1号造出了第一个机器人--机器人2号. 第3秒,机器人 ...
- Java各种锁机制简述
线程安全是多线程领域的问题,线程安全可以简单理解为一个方法或者一个实例可以在多线程环境中使用而不会出现问题. 在 Java 多线程编程当中,提供了多种实现 Java 线程安全的方式: 最简单的方式,使 ...
- python-unittest生成报告的几种方式
import unittest suite = unittest.TestSuite() #构造套件 #按测试方法添加 suite.addTest(测试类名('方法名')) suite.addTest ...
- webstorm注册码,亲测2016.1.1版
打开webstorm,点击帮助,注册 注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog. ...
- kohana 语言资源国际化、本地化
语言配置开关: root@DESKTOP-I4OIMJC /cygdrive/e/html/tproject/framebota/platform # grep -n2 'I18n::lang' bo ...
- WEB编程 入门简单 进阶难
其实不论是WEB还是其他什么编程,都是这个道理,至于为什么,我贴几段代码,大家感受下. JS 计算今天是星期几 入门级 // 计算系统当前是星期几 var str =""; var ...
- 纯CSS手动滑动轮播图(隐藏滚动条)
HTML: <div class="bigder"> <div class="big"> <dl> <dt>&l ...
- Twice Equation
题目链接:https://nanti.jisuanke.com/t/A1541 题意:给你一个L,要你求一个不小于L的最小数字n,对于一个整数m,满足2*(m+1)*m=n*(n+1). 思路:打表找 ...
- No plugin found for prefix 'war' in the current project and in the plugin groups
解决办法: 在pom里面添加 : <dependency> <groupId>org.apache.maven.plugins</groupId> <arti ...