-
Notifications
You must be signed in to change notification settings - Fork 0
/
Digit DP.cpp
50 lines (50 loc) · 1.07 KB
/
Digit DP.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int tot;
vector<int>dig;
void calc(ll num) {
dig.clear();
while (num) {
dig.push_back(num % 10);
num /= 10;
}
reverse(dig.begin(),dig.end());
tot = dig.size();
}
ll dp[82][2][2];
ll call(int pos, int isSmall, int isStart){
if(pos == tot){
return something;
}
ll &ans = dp[pos][isSmall][isStart];
if(ans != -1)return ans;
ans=0;
int lim = isSmall ? 9 : dig[pos];
if(!isStart){
for(int i = 0; i <= lim; i++){
ans += call(pos+1, isSmall | (i < dig[pos]), isStart);
}
}
else{
ans += call(pos+1, 1, 1);
for(int i = 1; i <= lim; i++){
ans += call(pos+1, isSmall | (i < dig[pos]), 0);
}
}
return ans;
}
void ProcessInput(){
ll l, r;
cin >> l >> r ;
if(l > r) swap(l,r);
l -= 1;
ll ansa,ansb;
if(l < 0)ansa=0;
else{
SET(dp);
calc(l);
ansa=call(0, 0, 1);
}
SET(dp);
calc(r);
ansb=call(0, 0, 1);
cout << ansb - ansa << endl;
}