-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathDigit DP Boring Numbers.cpp
67 lines (55 loc) · 1.39 KB
/
Digit DP Boring Numbers.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
56
57
58
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
using namespace std;
string s;
int64_t n;
int64_t dp[20][2][2][20] = {};
int64_t go(int64_t curr, int64_t n, int64_t tight = 1, int64_t leading_zero = 0, int64_t cnt = 1){
if(curr == n){
return 1;
}
if(dp[curr][tight][leading_zero][cnt]!=-1) return dp[curr][tight][leading_zero][cnt];
int64_t ans = 0;
int64_t upper_bound = 9;
if(tight == 1){
upper_bound = s[curr] - '0';
}
for(int64_t i=0;i<=upper_bound;i++){
if((i!=0 or leading_zero) and cnt%2 == i%2){
ans += go(curr+1,n,tight&i==upper_bound,leading_zero||i!=0,cnt+1);
}
else if((i!=0 or leading_zero) and cnt%2 != i%2){
}
else{
ans += go(curr+1,n,tight&i==upper_bound,leading_zero||i!=0,cnt);
}
}
return dp[curr][tight][leading_zero][cnt] = ans;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("E:/CODING/input.txt", "r", stdin);
freopen("E:/CODING/output.txt", "w", stdout);
#endif
int64_t i,j,t=1;
cin>>t;
int64_t T = t;
while(t--){
cout << "Case #" << T-t << ": ";
cin >> s;
n = s.size();
bool ok = 1;
for(i=0;i<n;i++){
if((s[i]-'0')%2 != (i+1)%2) ok = 0;
}
memset(dp,-1,sizeof dp);
int64_t left = go(0,n);
cin >> s;
n = s.size();
memset(dp,-1,sizeof dp);
int64_t right = go(0,n);
cout << right - left + ok << '\n';
}
cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
return 0;
}