-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDIGIT_DP_range_sum.cpp
55 lines (42 loc) · 1.05 KB
/
DIGIT_DP_range_sum.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
51
52
53
54
55
#include<bits/stdc++.h>
using namespace std;
#if 1
typedef long long let;
#else
typedef int let;
#endif
let dp[20][200][2];
let digits(let x, vector<let> &arr){
while(x){
arr.push_back(x%10);
x /= 10;
}
}
let digit_sum(let index, let tight, let sum, vector<let> &arr){
if(index == -1)
return sum;
if(dp[index][sum][tight] != -1 and tight != 1){
return dp[index][sum][tight];
}
let return_sum = 0;
let range = tight?arr[index]:9;
for(let i=0;i<=range;++i){
let new_tight = (arr[index] == i)?tight: 0;
return_sum += digit_sum(index-1, new_tight, sum+i, arr);
}
if(!tight)
dp[index][sum][tight] = return_sum;
return return_sum;
}
int main(){
let l,r;
memset(dp, -1, sizeof(dp));
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif //ONLINE_JUDGE
cin>>l>>r;
vector<let> arr_l, arr_r;
digits(l-1, arr_l);
digits(r, arr_r);
cout<<digit_sum(arr_r.size()-1, 1, 0, arr_r) - digit_sum(arr_l.size()-1, 1, 0, arr_l);
}