「题解」Good Numbers

可爱的数学题++……

一 题目

Source

Descriptions

The only difference between easy and hard versions is the maximum value of n.

You are given a positive integer number n. 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 3 (i.e. no duplicates of powers of 3 are allowed).

For example:
30 is a good number: 30=33+31,
1 is a good number: 1=30,
12 is a good number: 12=32+31,
but 2 is not a good number: you can’t represent it as a sum of distinct powers of 3 (2=30+30),
19 is not a good number: you can’t represent it as a sum of distinct powers of 3 (for example, the representations 19=32+32+30=32+31+31+31+30 are invalid),
20 is also not a good number: you can’t represent it as a sum of distinct powers of 3 (for example, the representation 20=32+32+30+30 is invalid).
Note, that there exist other representations of 19 and 20 as sums of powers of 3 but none of them consists of distinct powers of 3.

For the given positive integer n find such smallest m (n≤m) that m is a good number.

You have to answer q independent queries.

Input

The first line of the input contains one integer q (1≤q≤500) — the number of queries. Then q 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.


二 题解

考虑 n⩽10^4 的情况。我们现在要在数列 [3^0,3^1,3^2,…,3^k](3^k>n) 中选出一些数,使得它们的和 ⩾n ,并使得这个和最小。
考虑 k 的范围,此时 k⩽log3(10^4)≈14 ,所以只需要 2^k 枚举每个数字选不选就好了。总复杂度 O(q·2^k) ,解决了 easy version 。

考虑 n⩽10^18 的情况。实际上上述枚举过程可以通过枚举二进制状态来实现,即用一个数字二进制位上的 0/1 来表示数列某一项选不选。假设我们当前二进制枚举的状态码为 S ,那么实际上该状态码所对应的数值是单调的;换句话说,随着 S 变大,其表示的那个数字也变大。

简单的 proof ,请理性偷税。
考虑把二进制数 0100 变成 0101 (前者表示 3^2 ,后者表示 3^2+3^0) ,一定变大,因为又多了新的一项。考虑把二进制数 0100 变成 1000 (前者表示 3^2 ,后者表示 3^3),也一定变大,因为次数+1。

那么也就是说现在枚举的数值具有单调性,那么为什么不二分呢?只需要二分查找出一个 ⩾n 的最小位置就好了。总复杂度 O(qk),其中 k 为常数,它大概是 log2(2^40) = 40 ,这已经很小了,于是切掉。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;

#define LL long long

LL read(){
LL s=0,ne=1; char c=getchar();
for(;c<'0'||c>'9';c=getchar()) if(c=='-') ne=-1;
for(;c>='0'&&c<='9';c=getchar()) s=(s<<1)+(s<<3)+c-'0';
return s*ne;
}

int q; LL n;

LL Pow3(int a){
LL rec = 1,base = 3;
while(a) {if(a & 1) rec *= base; base *= base,a >>= 1;}
return rec;
}
LL gen(LL u){
LL g = 0;
for(int i=0;i<40;i++) if(u & (1ll << i)) g += Pow3(i);
return g;
}

int main()
{
q = read();
while(q--){
n = read();
LL l = 0,r = (1ll << 40);
while(l < r){
LL m = (l + r) >> 1;
if(gen(m) >= n) r = m;
else l = m + 1;
}
printf("%lld\n",gen((l + r) >> 1));
}
return 0;
}
作者

ce-amtic

发布于

2019-10-28

更新于

2020-12-27

许可协议

CC BY-NC-SA 4.0

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×