답안 #252183

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
252183 2020-07-24T15:26:06 Z m3r8 Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
1 ms 384 KB
#include <stdio.h>
#include <vector>
#include <string.h>
#define ll long long

ll dp[10][10][20];
ll sm[20];
std::vector<int> num;

void con(ll a){
  num.clear();
  while(a){
    num.push_back(a%10);
    a /= 10;
  };  
};
ll calc(ll a){
  if(a < 0)return 0;
  if(a < 10)return a+1;
  if(a < 100){
    ll tmp = a/10;  
    if(tmp > a % 10)return a - tmp + 2;
    else return a - tmp + 1;
  };
  con(a);
  memset(dp,0,sizeof(dp));
  memset(sm,0,sizeof(sm));
  sm[1] = 10;
  for(int i = 0;i<10;i++){
    for(int j = 0;j<10;j++){
      if(i != j){
        dp[i][j][2] = 1;  
      };
      if(i != 0){
        sm[2] += dp[i][j][2];
      };
    };  
  };
  for(int i = 3;i<20;i++){
    for(int j = 0;j<10;j++){
      for(int k = 0;k<10;k++){
        for(int l = 0;l<10;l++){
          if(j != k && j != l){
            dp[j][k][i] += dp[k][l][i-1];   
          };
        };
        if(j != 0){
          sm[i] += dp[j][k][i]; 
        };
      };  
    };
  };
  ll erg = 0;
  int prev = -1;
  int pprev = -1;
  for(int i = 0;i<num.size();i++){
    int mx = num[num.size()-1-i];
    int lf = num.size() - i;
    if(lf >= 2){
      int start = prev == -1 ? 1 : 0;
      for(int j = start;j<mx;j++){
        if(j != prev && j != pprev){
          for(int k = 0;k<10;k++){
            if(k != prev){
              erg += dp[j][k][lf];  
            };
          };  
        };
      };
    }else{
      for(int j = 0;j<=mx;j++){
        if(j != prev && j != pprev)erg++;  
      };
    };
    if(pprev == mx || prev == mx)break;
    pprev = prev;
    prev = mx;
  };
  for(int i = 1;i<num.size();i++){
    erg += sm[i];  
  };
  return erg;
};




int main(void){
  ll a,b;
  scanf("%lld %lld",&a,&b);
  ll erg = calc(b) - calc(a-1);
  printf("%lld\n",erg);
  return 0;
};

Compilation message

numbers.cpp: In function 'long long int calc(long long int)':
numbers.cpp:56:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 0;i<num.size();i++){
                 ~^~~~~~~~~~~
numbers.cpp:79:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 1;i<num.size();i++){
                 ~^~~~~~~~~~~
numbers.cpp: In function 'int main()':
numbers.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld %lld",&a,&b);
   ~~~~~^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 256 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 0 ms 256 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 1 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 0 ms 256 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 1 ms 256 KB Output is correct
10 Correct 0 ms 256 KB Output is correct
11 Correct 0 ms 256 KB Output is correct
12 Correct 0 ms 384 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 0 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 1 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 384 KB Output is correct
20 Correct 0 ms 256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 256 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 0 ms 256 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 256 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 256 KB Output is correct
16 Correct 1 ms 256 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 256 KB Output is correct
19 Correct 0 ms 256 KB Output is correct
20 Correct 0 ms 256 KB Output is correct
21 Correct 1 ms 256 KB Output is correct
22 Correct 0 ms 256 KB Output is correct
23 Correct 1 ms 256 KB Output is correct
24 Correct 0 ms 256 KB Output is correct
25 Correct 0 ms 256 KB Output is correct
26 Correct 0 ms 256 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 0 ms 256 KB Output is correct
29 Correct 0 ms 256 KB Output is correct
30 Correct 1 ms 256 KB Output is correct
31 Correct 0 ms 256 KB Output is correct
32 Correct 0 ms 384 KB Output is correct
33 Correct 1 ms 256 KB Output is correct
34 Correct 0 ms 256 KB Output is correct
35 Correct 0 ms 256 KB Output is correct
36 Correct 0 ms 256 KB Output is correct
37 Correct 0 ms 256 KB Output is correct
38 Correct 1 ms 256 KB Output is correct
39 Correct 0 ms 256 KB Output is correct
40 Correct 0 ms 256 KB Output is correct
41 Correct 0 ms 256 KB Output is correct
42 Correct 0 ms 256 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 0 ms 256 KB Output is correct
45 Correct 0 ms 256 KB Output is correct