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;
#define task "PALINFREE"
#define fi first
#define se second
#define pii pair <int, int>
#define pb push_back
#define ll long long
#define pip pair <int, pii>
#define pli pair <ll, int>
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const ll inf = 1e18 + 7;
long long a, b, f[25][5][5][15][15];
int n;
string s;
int myabs(int x)
{
return x < 0 ? -x : x;
}
void inp()
{
cin >> a;
cin >> b;
}
long long dp(int k, int st, int small, int pre1, int pre2)
{
if (k == n)
return 1;
if (f[k][st][small][pre1][pre2] != -1)
return f[k][st][small][pre1][pre2];
long long ans = 0;
int lim = 9;
if (small)
lim = s[k] - '0';
//cout << s[k] << " " << lim << endl;
for (int i=0; i<=lim; ++i)
{
if (i == pre1 || i == pre2)
continue;
int new_small = (small && i == lim);
int new_start = (st || i != 0);
if (new_start)
ans += dp(k + 1, new_start, new_small, i, pre1);
else ans += dp(k + 1, new_start, new_small, 10, 10);
}
//cout << k << " " << st << " " << small << " " << pre1 << " " << pre2 << " " << ans << endl;
return (f[k][st][small][pre1][pre2] = ans);
}
long long calc(long long a)
{
for (int i=0; i<20; ++i)
for (int j=0; j<2; ++j)
for (int x=0; x<2; ++x)
for (int y=0; y<11; ++y)
for (int z=0; z <11; ++z)
f[i][j][x][y][z] = -1;
s = to_string(a);
n = (int)s.size();
//cout << s << " " << n << endl;
return dp(0, 1, 1, 10, 10);
}
void solve()
{
cout << calc(b) - calc(a-1) << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
inp();
solve();
return 0;
}
Compilation message (stderr)
numbers.cpp: In function 'int main()':
numbers.cpp:83:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
numbers.cpp:84:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
84 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |