-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdigitDp.cpp
33 lines (30 loc) · 871 Bytes
/
digitDp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Name : Digit DP
* Description : generate all number upto the number give into the string st[]
Given implementation will count how many numbers and those numbers digits sum divisible by n
* Complexity : O(multiplicatins of parameters)
* Use : set n = mod number | set ln = length(st)
*/
char st[MX];
int n,ln;
int dp[11][3][101][101];
int bct(int pos,bool lst,int sum,int mult){
if(pos>=ln){
if(sum%n==0&&mult%n==0) return 1;
else return 0;
}
if(dp[pos][lst][sum][mult]!=-1)
return dp[pos][lst][sum][mult];
int x=0,y;
for(int i=0; i<=9; i++){
if(lst){
y = st[pos]-'0';
if(i>y) break;
x += bct(pos+1,i==y,(sum+i)%n,(mult*10+i)%n);
}
else{
x += bct(pos+1,0,(sum+i)%n,(mult*10+i)%n);
}
}
return dp[pos][lst][sum][mult] = x;
}