# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
806750 | CESxRhino | Palindrome-Free Numbers (BOI13_numbers) | C++14 | 0 ms | 0 KiB |
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 ii pair<int,int>
#define f first
#define s second
#define pb push_back
#define ll long long
#define endl '\n'
using namespace std;
const ll MAXN = 1e18;
ll mod = 1e9 + 7;
ll a[100005];
ll dp[20][2][11][11][2];
string s;
ll Calc(ll pos,bool check,ll digit1,ll digit2,bool start)
{
if(pos == s.size())
{
return 1;
}
ll ans = 0;
if(dp[pos][check][digit1][digit2][start] != -1)
{
return dp[pos][check][digit1][digit2][start];
}
ll maxdigit = 0;
if(check)
{
maxdigit = s[pos] - '0';
}
else
{
maxdigit = 9;
}
if(!start)
{
for(ll i = 0;i <= maxdigit;i++)
{
bool newcheck = check & (i == maxdigit);
ll newdigit1 = i;
ll newdigit2 = digit1;
if(i == 0)
{
newdigit1 = -1;
}
ll newstart = 0;
if(i != 0)
{
start = 1;
}
else
{
start = 0;
}
ans += Calc(pos + 1,newcheck,newdigit1,newdigit2,start);
}
}
else
{
for(ll i = 0;i <= maxdigit;i++)
{
if(i == digit1 or i == digit2)
{
continue;
}
bool newcheck = check & (i == maxdigit);
ll newdigit1 = i;
ll newdigit2 = digit1;
ans += Calc(pos + 1,newcheck,newdigit1,newdigit2,start);
}
}
return dp[pos][check][digit1 + 1][digit2 + 1][start] = ans;
}
ll ans(ll a)
{
if(a <= 0)
{
return 0;
}
memset(dp,-1,sizeof(dp));
a = to_string(a);
return Calc(0,0,-1,-1,0);
}
void AcSolution()
{
ll a,b;
cin >> a >> b;
cout << ans(b) - ans(a - 1) << endl;
}
signed main()
{
// freopen("BDIGIT.inp", "r",stdin);
// freopen("BDIGIT.out", "w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--)
{
// memset(dp,-1,sizeof(dp));
AcSolution();
}
}