-
Notifications
You must be signed in to change notification settings - Fork 4
/
digitdp.cpp
58 lines (51 loc) · 1.06 KB
/
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
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
// Find count of numbers not containing digit 3 upto x
int dp[10][2][2];
int G(int idx, int tight, int isThree, const string& str, const int& len) {
if (idx == len) {
if (isThree)
return 1;
else
return 0;
}
if (dp[idx][tight][isThree] != -1)
return dp[idx][tight][isThree];
int res = 0;
if (!tight) {
for (int i = 0; i <= 9; i++) {
if (i == 3)
res += G(idx + 1, 0, 1, str, len);
else
res += G(idx + 1, 0, isThree, str, len);
}
} else {
for (int i = 0; i <= (str[idx] - '0'); i++) {
if (i == 3) {
if (i == (str[idx] - '0'))
res += G(idx + 1, 1, 1, str, len);
else
res += G(idx + 1, 0, 1, str, len);
} else {
if (i == (str[idx] - '0'))
res += G(idx + 1, 1, isThree, str, len);
else
res += G(idx + 1, 0, isThree, str, len);
}
}
}
return dp[idx][tight][isThree] = res;
}
void solve() {
memset(dp, -1, sizeof(dp));
string str;
cin >> str;
int len = str.length();
cout << stoi(str) - G(0, 1, 0, str, len) << endl;
}
signed main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}