#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
int dp[18][11][11][2];
int solve(string s,int i,int last,int secondlast,int tight)
{
if(i == s.length())
{
return 1;
}
if(dp[i][last][secondlast][tight]!= -1)return dp[i][last][secondlast][tight];
int limit = tight ? s[i] - '0' : 9;
int ans = 0;
for(int d = 0;d<=limit;d++)
{
if(d == last)
continue;
if(d == secondlast)
continue;
ans = ans + solve(s,i+1,d,last,tight && (d == limit));
}
return dp[i][last][secondlast][tight] = ans;
}
int getAns(string s)
{
int ans = 1;
int l = s[0] - '0';
for(int i=1;i<=l;i++)
{
ans = ans + solve(s,1,i,i,i == l);
}
for(int i=2;i<s.size();i++)
{
for(int j=1;j<=9;j++)
{
ans += solve(s,i,j,j,0);
}
}
return ans;
}
signed main()
{
int t;
cin >> t;
while(t--)
{
int l,r;
cin >> l >> r;
memset(dp,-1,sizeof dp);
int x = getAns(to_string(l-1));
memset(dp,-1,sizeof dp);
int y = getAns(to_string(r));
// cout << x << ' ' << y << '\n';
cout << y - x << '\n';
}
}
Compilation message
numbers.cpp: In function 'long long int solve(std::string, long long int, long long int, long long int, long long int)':
numbers.cpp:11:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | if(i == s.length())
| ~~^~~~~~~~~~~~~
numbers.cpp: In function 'long long int getAns(std::string)':
numbers.cpp:46:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i=2;i<s.size();i++)
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
3 |
Execution timed out |
1082 ms |
596 KB |
Time limit exceeded |
4 |
Incorrect |
1 ms |
260 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
7 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
10 |
Incorrect |
3 ms |
204 KB |
Output isn't correct |
11 |
Incorrect |
357 ms |
328 KB |
Output isn't correct |
12 |
Incorrect |
352 ms |
576 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
14 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
16 |
Execution timed out |
1090 ms |
540 KB |
Time limit exceeded |
17 |
Execution timed out |
1090 ms |
456 KB |
Time limit exceeded |
18 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
19 |
Execution timed out |
1089 ms |
448 KB |
Time limit exceeded |
20 |
Incorrect |
2 ms |
204 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1086 ms |
444 KB |
Time limit exceeded |
2 |
Execution timed out |
1094 ms |
316 KB |
Time limit exceeded |
3 |
Execution timed out |
1087 ms |
448 KB |
Time limit exceeded |
4 |
Execution timed out |
1090 ms |
328 KB |
Time limit exceeded |
5 |
Execution timed out |
1067 ms |
484 KB |
Time limit exceeded |
6 |
Execution timed out |
1086 ms |
520 KB |
Time limit exceeded |
7 |
Incorrect |
888 ms |
544 KB |
Output isn't correct |
8 |
Execution timed out |
1054 ms |
580 KB |
Time limit exceeded |
9 |
Incorrect |
10 ms |
204 KB |
Output isn't correct |
10 |
Incorrect |
534 ms |
324 KB |
Output isn't correct |
11 |
Execution timed out |
1046 ms |
532 KB |
Time limit exceeded |
12 |
Execution timed out |
1088 ms |
496 KB |
Time limit exceeded |
13 |
Execution timed out |
1090 ms |
528 KB |
Time limit exceeded |
14 |
Execution timed out |
1036 ms |
720 KB |
Time limit exceeded |
15 |
Incorrect |
168 ms |
324 KB |
Output isn't correct |
16 |
Execution timed out |
1097 ms |
432 KB |
Time limit exceeded |
17 |
Execution timed out |
1097 ms |
344 KB |
Time limit exceeded |
18 |
Execution timed out |
1093 ms |
452 KB |
Time limit exceeded |
19 |
Execution timed out |
1095 ms |
452 KB |
Time limit exceeded |
20 |
Execution timed out |
1087 ms |
452 KB |
Time limit exceeded |
21 |
Execution timed out |
1097 ms |
472 KB |
Time limit exceeded |
22 |
Execution timed out |
1093 ms |
584 KB |
Time limit exceeded |
23 |
Execution timed out |
1093 ms |
468 KB |
Time limit exceeded |
24 |
Execution timed out |
1093 ms |
452 KB |
Time limit exceeded |
25 |
Execution timed out |
1092 ms |
336 KB |
Time limit exceeded |
26 |
Execution timed out |
1097 ms |
452 KB |
Time limit exceeded |
27 |
Execution timed out |
1080 ms |
580 KB |
Time limit exceeded |
28 |
Execution timed out |
1097 ms |
572 KB |
Time limit exceeded |
29 |
Execution timed out |
1094 ms |
444 KB |
Time limit exceeded |
30 |
Execution timed out |
1086 ms |
456 KB |
Time limit exceeded |
31 |
Execution timed out |
1096 ms |
436 KB |
Time limit exceeded |
32 |
Execution timed out |
1091 ms |
460 KB |
Time limit exceeded |
33 |
Execution timed out |
1093 ms |
572 KB |
Time limit exceeded |
34 |
Execution timed out |
1091 ms |
564 KB |
Time limit exceeded |
35 |
Execution timed out |
1087 ms |
352 KB |
Time limit exceeded |
36 |
Execution timed out |
1086 ms |
356 KB |
Time limit exceeded |
37 |
Execution timed out |
1087 ms |
468 KB |
Time limit exceeded |
38 |
Execution timed out |
1092 ms |
444 KB |
Time limit exceeded |
39 |
Execution timed out |
1089 ms |
320 KB |
Time limit exceeded |
40 |
Execution timed out |
1091 ms |
328 KB |
Time limit exceeded |
41 |
Execution timed out |
1086 ms |
440 KB |
Time limit exceeded |
42 |
Execution timed out |
1095 ms |
572 KB |
Time limit exceeded |
43 |
Execution timed out |
1081 ms |
476 KB |
Time limit exceeded |
44 |
Execution timed out |
1091 ms |
588 KB |
Time limit exceeded |
45 |
Execution timed out |
1089 ms |
348 KB |
Time limit exceeded |