This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |