Good Numbers (hard version) (位运算)

时间:2022-07-28
本文章向大家介绍Good Numbers (hard version) (位运算),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题意描述

定义好数为 3 x 3^{x} 3x加起来的和,给定一个数n,让你求最小的大于n的好数

思路

我们可以将问题抽象成3进制,然后二分答案,寻找到最小的好数即可

AC代码

#include<bits/stdc++.h>
#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=40;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
LL n,a[N];
LL get(LL x){
    LL res=0;
    for(int i=0;i<=39;i++){
        if((x>>i)&1) res+=a[i];
    }
    return res;
}
bool check(LL x){
    if(get(x)>=n) return true;
    return false;
}
void solve(){
    cin>>n;
    LL l=1,r=(1ll<<39)-1;
    while(l<r){
        LL mid=l+r>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    cout<<get(l)<<endl;
}
int main(){
    IOS;
    a[0]=1;
    for(int i=1;i<=39;i++) a[i]=a[i-1]*3;
    int t;cin>>t;
    while(t--){
        solve();
    }
    return 0;
}