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;
typedef double dd;
typedef long long ll;
ll k;
ll dp[20][11][11][2][2];
vector <int> convert(ll x)
{
vector <int> v;
if (x == 0)
{
v.push_back(0);
return v;
}
while (x)
{
v.push_back(x % 10 + 1);
x /= 10;
}
reverse(v.begin(), v.end());
return v;
}
ll calc(const vector <int> &v, ll n, ll prev2, ll prev1, ll k0, ll f)
{
// cout << n << " " << s << " " << f <<"\n";
if (n == v.size())
{
return 1;
}
if (dp[n][prev2][prev1][k0][f] != -1) return dp[n][prev2][prev1][k0][f];
int lim;
if (f == 1) lim = 10;
else lim = v[n];
// cout << v[n] << "\n";
ll res = 0;
for (int i = 1; i <= lim; ++i)
{
int nf = f;
if (i <lim) nf = 1;
int nk0 = k0;
if (i > 1) nk0 = 1;
if (k0 == 0 && i == 1)
{
res += calc(v, n + 1, prev2, prev1, k0, nf);
continue;
}
if (prev2 != 0 && prev1 != 0 && i == prev2) continue;
if (prev1 != 0 && i == prev1) continue;
res += calc(v, n + 1, prev1, i, nk0, nf);
}
return dp[n][prev2][prev1][k0][f] = res;
}
ll solve(ll x)
{
if (x <= 10) return x + 1;
if (x < 100) return x + 1 - x / 11;
for (ll i = 0; i < 20; ++i)
for (ll j = 0; j < 11; ++j)
for (ll z = 0; z < 11; ++z)
for (ll t = 0; t < 2; ++t)
for (ll t1 = 0; t1 < 2; ++t1) dp[i][j][z][t][t1] = -1;
vector <int> v = convert(x);
// for (auto it :v) cout << it;
// cout << "\n";
return calc(v, 0, 0, 0, 0, 0);
return 0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("input.inp", "r", stdin);
// freopen("output.out", "w", stdout);
int tc = 1;
// cin >> tc;
for (int i = 1; i <= tc; ++i)
{
ll a, b;
cin >> a >> b >> k;
ll res1 = solve(b);
ll res2 = solve(a - 1);
// cout << res1 << " " << res2 << '\n';
cout << res1 - res2 << "\n";
// cout <<"___----------------\n";
}
}
Compilation message (stderr)
numbers.cpp: In function 'll calc(const std::vector<int>&, ll, ll, ll, ll, ll)':
numbers.cpp:31:11: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | if (n == v.size())
| ~~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |