Skip to content

Instantly share code, notes, and snippets.

@mhmoodlan
Created October 7, 2017 17:14
Show Gist options
  • Save mhmoodlan/8a5ecd8c47278c6aac07c76343055698 to your computer and use it in GitHub Desktop.
Save mhmoodlan/8a5ecd8c47278c6aac07c76343055698 to your computer and use it in GitHub Desktop.
#DP #Math #DivisibleBy8 #BruteForce #Solved #Codeforces
//http://codeforces.com/contest/550/problem/C
#include <bits/stdc++.h>
#define ll long long
#define sz(v) ((int) ((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define lp(i, n) for(int i = 0; i < (int)(n); ++i)
#define rep(i, v) for(int i = 0; i < sz(v); ++i)
using namespace std;
map<string, string> cache;
int getLast3Digits(string s) {
int l = s.length();
int x = s[l-1]+'\0';
x += (s[l-2] + '\0' )*10;
x += (s[l-3] + '\0') *100;
return x;
}
string findDiv8(string s) {
//cout << s << endl;
//getchar();
if(s.length() >= 3 ){ if(getLast3Digits(s) %8 == 0) return s; }
else {
if(s.length() == 2) {
int x = (s[0] + '\0' )* 10;
x += s[1] + '\0';
if(x % 8 == 0) return s;
else {
if(s[1] == '0' || s[1] == '8') return s.substr(1, 1);
if(s[0] == '0' || s[0] == '8') return s.substr(0, 1);
return "NO";
}
} else {
if(s == "8" || s == "0") return s;
return "NO";
}
}
if(cache.find(s) != cache.end()) return cache[s];
string &ret = cache[s];
for(int i = 1; i <= 3; i++) {
string tmp = s.substr(0, s.length()-i);
tmp += s.substr(s.length()-i+1, i-1);
ret = findDiv8(tmp);
if(ret != "NO")
return ret;
}
return ret;
}
int main() {
string s;
cin>>s;
cache.clear();
string output = findDiv8(s);
//cout << output << endl;
if(output != "NO" ) {
cout << "YES" << endl << output << endl;
} else {
cout << "NO" << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment