結果

問題 No.3276 Make Smaller Popcount
コンテスト
ユーザー Facade
提出日時 2025-10-19 16:02:52
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 1,044 bytes
コンパイル時間 7,019 ms
コンパイル使用メモリ 275,288 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-10-19 16:03:09
合計ジャッジ時間 7,778 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf=1e18;
int solve(int n){
    int cnt=0;
    for(int i=0;i<40;i++){
        cnt+=(n>>i)&1;
        if(cnt>=2&&((n>>i)&1)==0){
            int ans=0;
            int id=i;
            ans=(1LL<<id);
            id++;
            while(ans<n)ans+=(1LL<<id)*(n>>id);
            return ans-n;
        }
    }
    return -1;
}
int naive(int n){
    int cnt=0;
    for(int i=0;i<31;i++){
        cnt+=((n>>i)&1);
    }
    for(int x=0;x<10000;x++){
        int cur=0;
        for(int j=0;j<40;j++){
            cur+=(((n+x)>>j)&1);
        }
        if(cur<cnt){
            return x;
        }
    }
    return -1;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;cin>>t;
    while(t--){
        int n;cin>>n;
        int sol=solve(n);
        int nai=naive(n);
        cout<<sol<<"\n";
        // if(sol!=nai){
        //     cout<<"testcase : "<<n<<endl;
        //     cout<<"(sol,nai) : "<<sol<<" "<<nai<<endl;
        // }
    }
}
0