-
Notifications
You must be signed in to change notification settings - Fork 0
/
19 Number of zeros.cpp
101 lines (70 loc) · 1.6 KB
/
19 Number of zeros.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
Problem : LightOj 1140
Number of zeros
Jimmy writes down the decimal representations of all natural numbers
between and including m and n, (m ≤ n).
How many zeroes will he write down?
**/
/**Which of the favors of your Lord will you deny ?**/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define PII pair<int,int>
#define MP make_pair
#define F first
#define S second
#define INF INT_MAX
inline void optimizeIO()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int nmax = 1e3+7;
const LL LINF = 1e17;
string to_str(LL x)
{
stringstream ss;
ss<<x;
return ss.str();
}
string A,B;
LL dp[10][2][2][2][12];
LL go(int pos,int choto,int boro,int not_first_pos,int val)
{
if(pos==B.size())
return val;
LL &ret = dp[pos][choto][boro][not_first_pos][val];
if(ret!=-1)
return ret;
ret = 0;
int lo = 0, hi = 9;
if(!choto) hi = B[pos] - '0';
if(!boro) lo = A[pos] - '0';
for(int j=lo; j<=hi; j++)
ret += go(pos+1, choto | (j<hi), boro | (j>lo), not_first_pos | (j>0) , (j==0 && not_first_pos)+val);
return ret;
}
int main()
{
optimizeIO();
int tc;
cin>>tc;
for(int q=1; q<=tc; q++)
{
//cout<<"----------------------"<<endl;
cout<<"Case "<<q<<": ";
LL a,b;
cin>>a>>b;
B = to_str(b);
A = to_str(a);
while(A.size()<B.size())
{
A.insert(A.begin(),'0');
}
//cout<<inp<<endl;
memset(dp,-1,sizeof dp);
LL ans = go(0,0,0,0,0) + (a==0);
cout<<ans<<endl;
}
return 0;
}