답안 #482977

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
482977 2021-10-27T08:31:11 Z lto5 Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
1 ms 460 KB
#include<bits/stdc++.h>
using namespace std;

const int MAX_N = 20;
const int MAX_D = 15;

int num[MAX_N];
long long f[MAX_N][2][2][MAX_D][MAX_D];

//position i, <=, trailing zero, num[i - 2], num[i - 1]
long long dp(int i, bool limit, bool f0, int prev1, int prev2){
  if(i < 0)
    return 1;

  if(f[i][limit][f0][prev1][prev2] != -1)
    return f[i][limit][f0][prev1][prev2];

  int max_d = (limit ? num[i] : 9);
  long long res = 0;

  for(int c = 0; c <= max_d; c++){
    if(c == prev1 || c == prev2)
      continue;

    bool new_limit = limit && c == max_d;
    bool new_f0 = f0 && i >= 1 && c == 0;
    int new_prev1, new_prev2;

    if(new_f0 == true)
      new_prev1 = 10, new_prev2 = 10;
    else
      new_prev1 = prev2, new_prev2 = c;

    res += dp(i - 1, new_limit, new_f0, new_prev1, new_prev2);
  }

  return f[i][limit][f0][prev1][prev2] = res;
}

long long solve(long long x){
  int n = 0;
  do{
    num[n++] = x % 10;
    x /= 10;
  } while(x > 0);

  memset(f, -1, sizeof(f));
  return dp(n - 1, true, true, 10, 10);
}

int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  long long a, b;
  cin >> a >> b;

  cout << solve(b) - solve(a - 1);

  return 0;
}
    
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 0 ms 332 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 448 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 0 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 404 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 444 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 460 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 448 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 440 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 440 KB Output is correct
40 Correct 1 ms 440 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct