-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigit_DP.cpp
More file actions
74 lines (71 loc) · 1.72 KB
/
Copy pathdigit_DP.cpp
File metadata and controls
74 lines (71 loc) · 1.72 KB
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
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod (int)(1e9+7)
#define inf (int)(1e18)
#define sz(s) (int)s.size()
#define all(v) (v).begin(),(v).end()
#define input(v) for (auto &i : v) cin >> i;
#define print(v) for (auto &j : v) cout << j << " "; cout << "\n";
int a,b,d,k,dp[12][12][2];
vector<int>num;
int f(int pos,int cnt,bool small) {
if(cnt>k) return 0;
if(pos==sz(num)) {
if(cnt==k) return true;
return false;
}
if(dp[pos][cnt][small]!=-1) return dp[pos][cnt][small];
int res = 0;
if(small) {
for(int i=0;i<10;i++) {
if(i!=d) res += f(pos+1,cnt,small);
else res += f(pos+1,cnt+1,small);
}
}
else {
for(int i=0;i<10;i++) {
if(i<num[pos]) {
if(i==d) res += f(pos+1,cnt+1,true);
else res += f(pos+1,cnt,true);
}
else if(i==num[pos]) {
if(i==d) res += f(pos+1,cnt+1,small);
else res += f(pos+1,cnt,small);
}
}
}
return dp[pos][cnt][small] = res;
}
void solve() {
cin>>a>>b>>d>>k;
memset(dp,-1,sizeof(dp));
int tmp = b;
while(tmp) {
num.push_back(tmp%10);
tmp/=10;
}
reverse(all(num));
int ans = f(0,0,false);
num.clear();
memset(dp,-1,sizeof(dp));
tmp = a-1;
while(tmp) {
num.push_back(tmp%10);
tmp/=10;
}
reverse(all(num));
ans -= f(0,0,false);
cout<<ans<<"\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int testcases=1;
cin>>testcases;
for(int i=1;i<=testcases;i++) {
cout<<"Case "<<i<<": ";
solve();
}
return 0;
}