Submission #1792020
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
using VS = vector<string>; using LL = long long;
using VI = vector<int>; using VVI = vector<VI>;
using PII = pair<int, int>; using PLL = pair<LL, LL>;
using VL = vector<LL>; using VVL = vector<VL>;
#define ALL(a) begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9; const LL LINF = 1e17;
const LL MOD = 1000000007; const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 }; int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };
/* ----- 2017/11/19 Problem: kupc2015_h / Link: https://kupc2015.contest.atcoder.jp/tasks/kupc2015_h ----- */
/* ------問題------
-----問題ここまで----- */
/* -----解説等-----
----解説ここまで---- */
LL dp[64][64][128];
LL f(LL n, int i, int one, int diff) {
if (i == -1) {
if (one + diff == 63)return 0;
else return LINF;
}
if (dp[i][one][diff] != -1)return dp[i][one][diff];
LL res = LINF;
// 0
if (n&(1LL << i)) res = min(res, f(n, i - 1, one + 1, diff));
else res = min(res, f(n, i - 1, 0, diff + one));
// 1
if (n&(1LL << i)) res = min(res, f(n, i - 1, 0, diff) | (1LL << i));
else res = min(res, f(n, i - 1, one + 1, diff - 1) | (1LL << i));
return dp[i][one][diff] = res;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int T; cin >> T;
//reverse ?
FOR(_, 0, T) {
LL n; cin >> n;
fill(**dp, **dp + 64 * 64 * 128, -1);
cout << f(n, 62, 0, 63) << endl;
}
return 0;
}
Submission Info
Submission Time |
|
Task |
H - Bit Count |
User |
Yang33 |
Language |
C++14 (GCC 5.4.1) |
Score |
300 |
Code Size |
1984 Byte |
Status |
AC |
Exec Time |
69 ms |
Memory |
4352 KB |
Judge Result
Set Name |
All |
Score / Max Score |
300 / 300 |
Status |
|
Set Name |
Test Cases |
All |
00_sample.txt, 10_small_00.txt, 20_medium_01.txt, 20_medium_02.txt, 20_medium_03.txt, 30_large_04.txt, 30_large_05.txt, 30_large_06.txt, 80_power_of_2.txt |
Case Name |
Status |
Exec Time |
Memory |
00_sample.txt |
AC |
3 ms |
4352 KB |
10_small_00.txt |
AC |
30 ms |
4352 KB |
20_medium_01.txt |
AC |
34 ms |
4352 KB |
20_medium_02.txt |
AC |
39 ms |
4352 KB |
20_medium_03.txt |
AC |
36 ms |
4352 KB |
30_large_04.txt |
AC |
64 ms |
4352 KB |
30_large_05.txt |
AC |
69 ms |
4352 KB |
30_large_06.txt |
AC |
64 ms |
4352 KB |
80_power_of_2.txt |
AC |
4 ms |
4352 KB |