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 ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 2e5 + 9;
const ll inf = 1e16 + 7;
typedef pair<int,int> LL;
string s;
ll n,k,dp[20][12][12][2][2],L,R;
string To_string(ll x){
string res;
while(x > 0) res += char(x%10 + '0'),x /= 10;
reverse(res.begin(),res.end());
return res;
}
ll f(ll num,ll prv1,ll prv2,ll smaller,ll st){
if (num > n) return 1;
if (dp[num][prv1][prv2][smaller][st] != -1) return dp[num][prv1][prv2][smaller][st];
ll ans = 0,now = s[num] - '0';
for (ll i = 0;i <= 9;i++){
if (!smaller && i > now) break;
if (!st){
if (!i) ans += f(num + 1,prv1,prv2,1,0);
else{
if (i < now || smaller) ans += f(num + 1,i,prv1,1,1);
else if (i == now) ans += f(num + 1,i,prv1,0,1);
}
}
else{
if (i != prv1 && i != prv2){
if (i < now || smaller) ans += f(num + 1,i,prv1,1,1);
else if (i == now) ans += f(num + 1,i,prv1,0,1);
}
}
}
return dp[num][prv1][prv2][smaller][st] = ans;
}
ll cal(ll x){
memset(dp,-1,sizeof(dp));
if (!x) return 1;
s = To_string(x); s = " " + s; n = s.size() - 1;
//cout<<f(1,10,10,0,0); exit(0);
return f(1,10,10,0,0);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "tst"
if (fopen(task".INP","r")){
freopen(task".INP","r",stdin);
//freopen(task".OUT","w",stdout);
}
cin>>L>>R;
//cout<<cal(9); return 0;
cout<<cal(R) - cal(L - 1);
}
Compilation message (stderr)
numbers.cpp: In function 'int main()':
numbers.cpp:56:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
56 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |