結果

問題 No.3276 Make Smaller Popcount
ユーザー Today03
提出日時 2025-09-19 22:31:21
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 416 ms / 2,000 ms
コード長 2,989 bytes
コンパイル時間 3,137 ms
コンパイル使用メモリ 281,356 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-19 22:31:37
合計ジャッジ時間 16,550 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for(ll i=0; i<(ll)(n); i++)

int lob(const auto& v, auto x) { return lower_bound(ALL(v),x)-v.begin(); }
bool chmax(auto& a, auto b) { return a<b ? a=b, true : false; }
bool chmin(auto& a, auto b) { return a>b ? a=b, true : false; }
using ll=long long; const int INF=1e9+10; const ll INFL=4e18;
using VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>;
using PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>;

#ifdef LOCAL
#include "./debug.hpp"
#else
#define debug(...)
#define print_line
#endif



/// @brief ビット演算
namespace Bit{
    /// @brief 1であるビットの個数を返す
    int PopCount(int n) { return __builtin_popcount(n); }

    /// @brief 1であるビットの個数を返す
    int PopCount(ll n) { return __builtin_popcountll(n); }

    /// @brief popcountの偶奇を返す
    int Parity(int n) { return __builtin_parity(n); }

    /// @brief popcountの偶奇を返す
    int Parity(ll n) { return __builtin_parityll(n); }

    /// @brief 最上位ビットの位置を返す
    int TopBit(int n) { return n ? 31-__builtin_clz(n) : -1; }

    /// @brief 最上位ビットの位置を返す
    int TopBit(ll n) { return n ? 63-__builtin_clzll(n) : -1; }

    /// @brief 2進表現の長さを返す
    int BitLength(int n) { return n ? 32-__builtin_clz(n) : 1; }

    //// @brief 2進表現の長さを返す
    int BitLength(ll n) { return n ? 64-__builtin_clzll(n) : 1; }

    /// @brief 最下位ビットの位置を返す
    int LowBit(int n) { return n ? __builtin_ctz(n) : -1; }

    /// @brief 最下位ビットの位置を返す
    int LowBit(ll n) { return n ? __builtin_ctzll(n) : -1; }

    /// @brief 2のべき乗か否かを返す
    bool IsPowerOfTwo(int n) { return n && (n&-n)==n; }

    /// @brief 0~n-1 ビットを立てたビットマスクを返す
    ll Mask(int n) { return (1LL<<n)-1; }

    /// @brief iビット目が立っているか否かを返す
    bool HasBit(ll n,int i) { return (n>>i&1); }

    /// @brief 整数 n の2進表現を返す
    /// @param len ビット数
    /// @param rev 反転するか否か
    string ToBinary(ll n,int len=32,bool rev=false) {
        string ret;
        for(int i=0; i<len; i++) ret+=HasBit(n,rev?len-1-i:i)?'1':'0';
        return ret;
    }
}

//----------------------------------------------------------

void solve() {
    ll N; cin>>N;

    VL B;
    for(int i=0; i<30; i++) if(Bit::HasBit(N,i)) B.push_back(i);

    if(B.size()==1) {
        cout<<-1<<endl;
        return;
    }

    ll s=B[1]+1;
    while(Bit::HasBit(N,s)) s++;

    ll ans=N;
    ans|=1ll<<s;
    for(int i=0; i<s; i++) if(Bit::HasBit(ans,i)) ans^=1ll<<i;
    cout<<ans-N<<endl;
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    //cout<<fixed<<setprecision(15);
    int T=1; cin>>T;
    while(T--) solve();
}
0