865E - Hex Dyslexia - CodeForces Solution


bitmasks brute force dp graphs *3300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
inline int geti(char c) {return isdigit(c) ? c - '0' : c - 'a' + 10;}
inline char getc(int x) {return x < 10 ? x + '0' : x + 'a' - 10;}
char s[16];int n;ll d[20],sum[1 << 14];ll ans = LLONG_MAX,f[1 << 14];
inline int lowbit(int x) {return x & -x;}
void solve()
{
int lim = 1 << (n - 1);memset(f,63,sizeof(ll) * lim);f[0] = 0;sum[0] = d[n - 1];
for (int i = 1;i < lim;i++) sum[i] = sum[i - lowbit(i)] + d[__lg(lowbit(i))];
for (int i = 0;i < lim;i++)
{
if (sum[i] < 0 || sum[i] > 15) continue;
for (int j = 0;j < n;j++)
if (!(i & (1 << j))) f[i | (1 << j)] = min(f[i | (1 << j)],f[i] + (sum[i] << (j * 4)));
}
ans = min(ans,f[lim - 1]);
}
void dfs(int pos,int cnt)
{
if (pos == n - 1)
{
if (!cnt) solve();
return;
}
if (cnt < n - pos) dfs(pos + 1,cnt);
if (cnt) { d[pos] -= 16;d[pos + 1]++;dfs(pos + 1,cnt - 1);d[pos] += 16;--d[pos + 1]; }
}
void print(ll x,int c)
{ if (c == 1) cout << getc(x);else print(x >> 4,c - 1),cout << getc(x & 15); }
int main ()
{
ios::sync_with_stdio(false);
cin >> s;n = strlen(s);ll sum = 0,tt = 1,num = 0;
for (int i = n - 1;i >= 0;--i,tt *= 16) sum += geti(s[i]) * tt,num += geti(s[i]),d[i] = geti(s[n - i - 1]);
if (num % 15) {cout << "NO" << endl;return 0;}
int cnt = num / 15;dfs(0,cnt);
if (ans >= (1ll << (n * 4)) - sum) cout << "NO" << endl;else print(ans,n);
return 0;
}


Comments

Submit
0 Comments
More Questions

1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64
755B - PolandBall and Game
808B - Average Sleep Time
1515E - Phoenix and Computers
1552B - Running for Gold
994A - Fingerprints
1221C - Perfect Team
1709C - Recover an RBS
378A - Playing with Dice
248B - Chilly Willy
1709B - Also Try Minecraft
1418A - Buying Torches
131C - The World is a Theatre
1696A - NIT orz
1178D - Prime Graph
1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary