-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25 CHEF_LONG ENCODING.cpp
201 lines (147 loc) · 3.72 KB
/
25 CHEF_LONG ENCODING.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
Prob : CHEF LONG ENCODING
Let's define a function f(x) for a positive integer x as follows:
1) Consider the decimal representation of x (without leading zeros).
Split it into the minimum number of contiguous subsequences of digits
such that in each subsequence, all digits are identical.
2) For each subsequence, look at its position in the original decimal representation of x
.Let's say that the most significant digit it contained was the e-th digit,
where e=0 corresponds to the least significant digit of x.
For example, 388,822,442 can be split into subsequences "3", "888", "22", "44", "2", where e=7 for the sequence "888" and e=4
for the sequence "22".
3) The value of a subsequence which contains a digit d repeated one or more times is d*10^e.
4) f(x) is the sum of values of these subsequences. For example,
f(388,822,442)=3*1e8+8*1e7+2*1e4+4*1e2+2*1e0
The oven has a screen which displays two long integers L
and R (with NL and NR digits respectively, without leading zeros).
The password is the sum of f(x) for each x between L and R inclusive.
Chef does not have a lot of time, so he asks for your help.
Please find the password for the oven so that he can start cooking as fast as possible.
Since this number could be very large, compute it modulo 109+7
Constraints
-----------
1≤T≤10
1≤NL,NR≤105
1≤L≤R<1e100000
L and R do not contain leading zeros
**/
/**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 PLL pair<LL,LL>
#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 inp;
string A,B;
PLL dp[100005][2][2][20];
LL ten[100005];
LL MOD = 1e9+7;
inline void precalculate ()
{
ten[0] = 1;
for(int i=1;i<=100000;i++)
ten[i] = ((ten[i-1]%MOD) *10)%MOD;
}
LL bigMod(LL x,LL n,LL mo)
{
if(n==0)
return 1;
LL u = bigMod(x,n/2,mo);
u = ((u%mo)*(u%mo))%mo;
if(n%2==1)
u = ((u%mo)*(x%mo))%mo;
return u;
}
PLL go(int pos,int choto,int boro,int prev)
{
if(pos<0)
return MP(0,1);
PLL &ret = dp[pos][choto][boro][prev];
if(ret.F!=-1 && boro && choto)
return ret;
ret = MP(0,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++)
{
PLL temp = go(pos-1, choto | (j<hi), boro | (j>lo),j);
temp.F %= MOD;
temp.S %= MOD;
if(j!=prev)
{
ret.F = (ret.F%MOD + (((j*temp.S)%MOD)*ten[pos]))%MOD;
}
ret.F = (ret.F%MOD + temp.F)%MOD;
ret.S = (ret.S%MOD + temp.S)%MOD;
}
return ret;
}
int main()
{
//optimizeIO();
precalculate ();
int tc;
scanf("%d",&tc);
//cin>>tc;
memset(dp,-1,sizeof dp);
for(int q=1; q<=tc; q++)
{
//cout<<"----------------------"<<endl;
//cout<<"Case "<<q<<": ";
LL la,lb;
string a,b;
char s1[100007],s2[100007];
scanf("%lld",&la);
scanf("%s",s1);
scanf("%lld",&lb);
scanf("%s",s2);
//cin>>la>>a>>lb>>b;
A = s1;
B = s2;
while(A.size()<B.size())
{
A.insert(A.begin(),'0');
}
reverse(A.begin(),A.end());
reverse(B.begin(),B.end());
PLL ans = go(B.size()-1,0,0,-1);
ans.F %= MOD;
printf("%lld\n",ans.F);
//cout<<ans.F<<endl;
}
return 0;
}
/**
Example Input
3
1 9
2 97
1 8
2 12
1 1
1 8
Example Output
4681
49
36
**/