# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
688341 | stevancv | Palindrome-Free Numbers (BOI13_numbers) | C++14 | 1 ms | 212 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 ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 800 + 2;
const int inf = 1e9;
ll dp[20][4];
/*
dp[i][0] - is the same string possible
dp[i][1] - only one position is used
dp[i][2] - >= 2 is used
*/
ll F(string a) {
if (a == "0") return 0;
dp[0][0] = 1;
dp[0][1] = a[0] - '1';
int x = a.size();
for (int i = 1; i < x; i++) {
dp[i][0] = dp[i - 1][0];
if (a[i] == a[i - 1]) dp[i][0] = 0;
if (i > 1 && a[i] == a[i - 2]) dp[i][0] = 0;
dp[i][1] = 9;
int k = a[i] - '0';
if (a[i - 1] < a[i]) k -= 1;
if (i > 1 && a[i - 2] < a[i]) k -= 1;
dp[i][2] = k * dp[i - 1][0] + 9 * dp[i - 1][1] + 8 * dp[i - 1][2];
}
return dp[x - 1][0] + dp[x - 1][1] + dp[x - 1][2];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string a, b;
cin >> a >> b;
assert(a[0] != '0' || a.size() == 1);
assert(b[0] != '0' || b.size() == 1);
ll ans = F(b) - F(a);
int ok = 1;
for (int i = 0; i < a.size(); i++) {
if (i > 1 && a[i] == a[i - 1]) ok = 0;
if (i > 2 && a[i] == a[i - 2]) ok = 0;
}
cout << ans + ok << en;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |