結果
問題 |
No.3276 Make Smaller Popcount
|
ユーザー |
![]() |
提出日時 | 2025-09-19 21:36:47 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 61 ms / 2,000 ms |
コード長 | 1,016 bytes |
コンパイル時間 | 2,964 ms |
コンパイル使用メモリ | 275,700 KB |
実行使用メモリ | 7,716 KB |
最終ジャッジ日時 | 2025-09-19 21:37:17 |
合計ジャッジ時間 | 6,700 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 28 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; } bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; } int ppc(ll N) { return __builtin_popcountll(N); } void naive(ll N) { int P = ppc(N); for (int x = 0; x < 100; x++) { if (P > ppc(N + x)) { cout << x << '\n'; return; } } cout << -1 << '\n'; } void solve(ll N) { if (ppc(N) == 1) { cout << -1 << '\n'; return; } ll ans = 0; int C = 0; for (int i = 0; i < 30; i++) { if (N >> i & 1) { if (C++ == 0) ans += 1LL << i; } else { if (C) ans += 1LL << i; } if (C == 2) break; } cout << ans << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { ll N; cin >> N; if (N <= 4) naive(N); else solve(N); } }