Submission #102364

# Submission time Handle Problem Language Result Execution time Memory
102364 2019-03-24T13:45:18 Z WLZ Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
3 ms 512 KB
#include <bits/stdc++.h>
using namespace std;

long long dp[20][2][2][11][11];
vector<int> num;
int n;

long long solve(int i, int sm, int lz, int prev1, int prev2) {
  if (i == n) {
    return !lz;
  }
  if (dp[i][sm][lz][prev1][prev2] != -1) {
    return dp[i][sm][lz][prev1][prev2];
  }
  long long ans = 0;
  int mx;
  if (sm) {
    mx = num[i];
  } else {
    mx = 9;
  }
  for (int d = 0; d <= mx; d++) {
    if (d == prev1 || d == prev2) {
      continue;
    }
    if (lz && d == 0) {
      ans += solve(i + 1, (sm && d == num[i]), 1, 10, 10);
    } else {
      ans += solve(i + 1, (sm && d == num[i]), 0, prev2, d);
    }
  }
  return (dp[i][sm][lz][prev1][prev2] = ans);
}

long long calc(long long x) {
  num.clear();
  while (x > 0) {
    num.push_back(x % 10);
    x /= 10;
  }
  reverse(num.begin(), num.end());
  n = (int) num.size();
  memset(dp, -1, sizeof dp);
  return solve(0, 1, 1, 10, 10);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long a, b;
  cin >> a >> b;
  long long ans = calc(b);
  if (a > 1) {
    ans -= calc(a - 1);
  }
  if (a == 0) {
    ans++;
  }
  cout << ans << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 256 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 512 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 512 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 3 ms 384 KB Output is correct
33 Correct 2 ms 384 KB Output is correct
34 Correct 2 ms 384 KB Output is correct
35 Correct 3 ms 384 KB Output is correct
36 Correct 3 ms 384 KB Output is correct
37 Correct 2 ms 384 KB Output is correct
38 Correct 2 ms 384 KB Output is correct
39 Correct 3 ms 384 KB Output is correct
40 Correct 2 ms 384 KB Output is correct
41 Correct 2 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct
43 Correct 2 ms 384 KB Output is correct
44 Correct 2 ms 384 KB Output is correct
45 Correct 3 ms 384 KB Output is correct