1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include<bits/stdc++.h> #define LL long long
using namespace std;
const int MAXN = 35; const LL INF = 0x3f3f3f3f3f3f3f3f;
LL a, b, c; LL cnta, cntb, cntc, len; LL f[MAXN][MAXN][MAXN][MAXN][2];
inline int bitLen(LL x) { int len = 1; while ((1LL << len) < x) len++; return len; } inline LL dfs(int pos, int aa, int bb, int cc, bool carry) { if (aa < 0 || bb < 0 || cc < 0) return INF; if (pos == len) return (!aa && !bb && !cc && !carry) ? 0 : INF; if (f[pos][aa][bb][cc][carry] != -1) return f[pos][aa][bb][cc][carry];
LL res = INF; for (int x = 0; x <= 1; ++x) for (int y = 0; y <= 1; ++y) { LL sum = x + y + carry; LL z = sum & 1; if (aa >= x && bb >= y && cc >= z) res = min(res, (z << pos) + dfs(pos + 1, aa - x, bb - y, cc - z, sum > z)); } return f[pos][aa][bb][cc][carry] = res; }
int main() { freopen("ab.in", "r", stdin); freopen("ab.out", "w", stdout);
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> a >> b >> c; len = max({bitLen(a), bitLen(b), bitLen(c)}); cnta = __builtin_popcount(a); cntb = __builtin_popcount(b); cntc = __builtin_popcount(c);
memset(f, -1, sizeof(f)); LL ans = dfs(0, cnta, cntb, cntc, 0); cout << (ans >= INF ? -1 : ans); return 0; }
|