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>
#define int long long
#define endl '\n'
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int dp[20][12][12][2][2]; // (ind, prv, cur, tight, started)
void solve() {
int l, r; cin>>l>>r;
string s;
function<int(int, int, int, int, int)> magic = [&] (int ind, int prv, int cur, int tight, int started) {
if(ind == s.size()) return 1LL;
int &ans = dp[ind][prv][cur][tight][started];
if(~ans) return ans;
ans = 0;
int mx = tight ? s[ind] - '0' : 9;
for(int i = 0; i <= mx; i++) {
if(i == 0 && !started) {
ans += magic(ind + 1, prv, cur, tight & (i == mx), 0);
} else if(i != prv && i != cur) {
ans += magic(ind + 1, cur, i, tight & (i == mx), 1);
}
}
return ans;
};
auto cnt = [&] (int n) {
s = to_string(n);
int ans = 0;
memset(dp, -1, sizeof dp);
ans += magic(0, 10, 10, 1, 0);
return ans;
};
cout<<cnt(r) - cnt(l - 1)<<endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1, c = 1; //cin>>t;
while(t--) {
// cerr<<"Case "<<c++<<": \n";
solve();
}
}
/*
i/p:
o/p:
*/
Compilation message (stderr)
numbers.cpp: In lambda function:
numbers.cpp:15:16: 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]
15 | if(ind == s.size()) return 1LL;
| ~~~~^~~~~~~~~~~
numbers.cpp: In function 'int main()':
numbers.cpp:51:16: warning: unused variable 'c' [-Wunused-variable]
51 | int t = 1, c = 1; //cin>>t;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |