Last active
August 29, 2015 14:22
-
-
Save balamark/0b9cec267b8add2150c7 to your computer and use it in GitHub Desktop.
Divisibility by Eight
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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