Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define pb push_back
  3. #define mp make_pair
  4.  
  5. using namespace std;
  6.  
  7. typedef long long int ll;
  8. typedef pair<int,int> pii;
  9. typedef vector<pii> vpii;
  10. typedef vector<ll> vll;
  11.  
  12. const ll MOD = 1e9 + 7;
  13.  
  14. /* Commonly used stuff */
  15.  
  16. ll gcd(ll a, ll b){
  17.     if(b == 0)
  18.         return a;
  19.     return gcd(b, a % b);
  20. }
  21.  
  22. bool isPrime(ll n){
  23.     if(n <= 1)
  24.         return false;
  25.     for(ll i = 2; i <= sqrt(n); ++i){
  26.         if(n % i == 0)
  27.             return false;
  28.     }
  29.     return true;
  30. }
  31.  
  32. ll modExp(ll b, ll e, ll mod){
  33.     ll res = 1;
  34.     while(e > 0){
  35.         if(e & 1)
  36.             res = res * b % mod;
  37.         b = b * b % mod;
  38.         e>>=1;
  39.     }
  40.     return res % mod;
  41. }
  42.  
  43. ll factorial(ll n){
  44.     ll r = 1;
  45.     for(ll i = 1; i <= n; ++i){
  46.         r *= i;
  47.     }
  48.     return r;
  49. }
  50.  
  51. //////////////////////////
  52. /* Put globals here */
  53.  
  54. string n, result="";
  55. unordered_map<string,unordered_map<int, string>> dp;
  56. ll r = 0;
  57.  
  58. string go(string &a, int i, string curr, int val){
  59.     if(i >= a.size())
  60.         return "";
  61.     if(dp.count(curr)){
  62.         if(dp[curr].count(i))
  63.             return dp[curr][i];
  64.     }
  65.     string withCurr = curr + a[i];
  66.     int nextVal = ((val * 10) + (a[i] - '0')) % 8;
  67.     if(nextVal == 0)
  68.         return withCurr;
  69.     string withoutResult = go(a, i + 1, curr, val);
  70.     if(withoutResult.size() > 0)
  71.         return dp[curr][i] = withoutResult;
  72.     return dp[curr][i] = go(a, i + 1, withCurr, nextVal);
  73. }
  74.  
  75. int main(){
  76.     cin>>n;
  77.     string r = go(n, 0, "", 0);
  78.     if(r.size() > 0){
  79.         cout<<"YES"<<endl;
  80.         cout<<r<<endl;
  81.     } else {
  82.         cout<<"NO"<<endl;
  83.     }                                  
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement