Skip to content

Instantly share code, notes, and snippets.

@balamark
Last active August 29, 2015 14:22
Show Gist options
  • Save balamark/0b9cec267b8add2150c7 to your computer and use it in GitHub Desktop.
Save balamark/0b9cec267b8add2150c7 to your computer and use it in GitHub Desktop.
Divisibility by Eight
int solve(int p, int num){
if(p==n) printf("%d\n", num);
if(dp[p][num]!=-1) return dp[p][num]; //this line must go first!
//if(p==n && num==0) return dp[n][0] = 0; //ignore empty case, but this over write the correct value
if(p==n) return dp[p][num%8] = (num%8==0);
int t = (num*10)+(s[p]-'0');
//take digit at position p or not
return dp[p][num%8] = max(solve(p+1, t%8), solve(p+1, num));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment