Skip to content

Instantly share code, notes, and snippets.

@balamark
Created June 10, 2015 23:54
Show Gist options
  • Save balamark/0494cb0bb5b0129acedf to your computer and use it in GitHub Desktop.
Save balamark/0494cb0bb5b0129acedf to your computer and use it in GitHub Desktop.
think about it, how can we differentiate the case which takes zero digit with the valid case which take at least one digit?
#include <string>
#include <iostream>
using namespace std;
string s;
int n;
int dp[100][8];
int solve(int p, int num){
if(p==n){
return dp[p][num] = (num==0);
}
if(dp[p][num]!=-1) return dp[p][num];
int t = (num*10)+(s[p]-'0');
//take digit at position p or not
int a = solve(p+1, t%8);
int b = solve(p+1, num);
return dp[p][num] = max(a,b);
}
int main(){
cin>>s;
n = s.size();
memset(dp, -1, sizeof dp);
if(solve(0,0)==0) cout<<"NO\n";
else{
for(int i=0;i<n;++i){
if(dp[i][0]==1){
return cout<<"YES\n", 0;
}
}
}
cout<<"NO\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment