Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
- #define Bye return 0
- #define CodeTyper main
- #define ll int long long
- using namespace std;
- ll dp[20][2][10][2];
- ll digit_dp(int pos, int flag, int previous, bool zero, string str){
- if(pos >= str.size())
- return 1;
- if(dp[pos][flag][previous][zero] != -1)
- return dp[pos][flag][previous][zero];
- ll res = 0;
- if(zero)
- res = digit_dp(pos + 1, 0, 0, 1, str);
- else
- if(previous != 0)
- res = digit_dp(pos + 1, (str[pos] - '0' == 0) ? flag : 0, 0, 0, str);
- int end = flag ? str[pos] - '0' : 9;
- for (int j = 1; j<=end; j++){
- if(previous == j)
- continue;
- int new_flag = (j == str[pos] - '0') ? flag : 0;
- res += digit_dp(pos + 1, new_flag, j, false, str);
- }
- return dp[pos][flag][previous][zero] = res;
- }
- void reset_dp(){
- memset(dp, -1, sizeof dp);
- }
- void solve() {
- ll a, b; cin>>a>>b; a--;
- string str_b = to_string(b);
- string str_a = to_string(a);
- reset_dp();
- ll r = digit_dp(0, true, -1, true, str_b);
- reset_dp();
- ll l = digit_dp(0, true, -1, true, str_a);
- l = a < 0 ? 0 : l;
- cout<<(r - l)<<endl;
- }
- int CodeTyper()
- {
- boost;
- solve();
- Bye;
- }
- /*
- 'Think FIRST, then CODE.'
- 'May the CODE be with YOU.'
- */
Add Comment
Please, Sign In to add comment