답안 #579916

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
579916 2022-06-20T09:12:26 Z birthdaycake Palindrome-Free Numbers (BOI13_numbers) C++17
17.5 / 100
1 ms 408 KB
#include<bits/stdc++.h>
#define int long long
#define boost ios_base::sync_with_stdio(false), cin.tie(NULL);
#define mod 1000000007
using namespace std;
 
string a,b;
int n;
int dp[18][10][10][2][3];

// n1 -> i - 1, n2 -> i - 2
int solve(int i, int n1, int n2, int tag, int z){
    if(i == n)  return 1;
    if(dp[i][n1][n2][tag][z] != -1) return dp[i][n1][n2][tag][z];
    dp[i][n1][n2][tag][z] = 0;
    if(tag){
        if(z == 0){
            for(int j = 0; j <= 9; j++){
                if(j == 0){
                    dp[i][n1][n2][tag][z] += solve(i + 1, n2, n1, tag, 0);
                    continue;
                }
               dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, tag, 1);
            }
        }
        else if(z == 1){
            for(int j = 0; j <= 9; j++){
                if(j != n1) dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, tag, 2);
            }
        }else{
            for(int j = 0; j <= 9; j++){
                if(j != n1 && j != n2) dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, tag, 2);
            }
        }
    }else{
        if(z == 0){
            int x = b[i] - '0';
            dp[i][n1][n2][tag][z] += solve(i + 1, n1, n2, 1, 0);
            for(int j = 1; j < x; j++){
               dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, 1, 1);
            }
            dp[i][n1][n2][tag][z] += solve(i + 1, x, n1, tag, 1);
        }
        else if(z == 1){
            int x = b[i] - '0';
            for(int j = 0; j < x; j++){
                if(j != n1) dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, 1, 2);
            }
            dp[i][n1][n2][tag][z] += solve(i + 1, x, n1, tag, 2);
            
        }else{
            int x = b[i] - '0';
            for(int j = 0; j < x; j++){
                if(j != n1 && j != n2) dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, 1, 2);
            }
            dp[i][n1][n2][tag][z] += solve(i + 1, x, n1, tag, 2);
        }
    }
    return dp[i][n1][n2][tag][z];
    
}
int solve2(int i, int n1, int n2, int tag, int z){
    if(i == n) return 1;
    
    if(dp[i][n1][n2][tag][z] != -1) return dp[i][n1][n2][tag][z];
    dp[i][n1][n2][tag][z] = 0;
    if(tag){
        if(z == 0){
            for(int j = 0; j <= 9; j++){
                if(j == 0){
                    dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, tag, 0);
                    continue;
                }
               dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, tag, 1);
            }
        }
        else if(z == 1){
            for(int j = 0; j <= 9; j++){
                if(j != n1) dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, tag, 2);
            }
        }else{
            for(int j = 0; j <= 9; j++){
                if(j != n1 && j != n2) dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, tag, 2);
            }
        }
    }else{
        if(z == 0){
            int x = a[i] - '0';
            dp[i][n1][n2][tag][z] += solve2(i + 1, n1, n2, 1, 0);
            for(int j = 1; j < x; j++){
               dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, 1, 1);
            }
            dp[i][n1][n2][tag][z] += solve2(i + 1, x, n1, tag, 1);
        }
        else if(z == 1){
            int x = a[i] - '0';
            for(int j = 0; j < x; j++){
                if(j != n1) dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, 1, 2);
            }
            dp[i][n1][n2][tag][z] += solve2(i + 1, x, n1, tag, 2);
            
        }else{
            int x = a[i] - '0';
            for(int j = 0; j < x; j++){
                if(j != n1 && j != n2) dp[i][n1][n2][tag][z] += solve2(i + 1, j, n1, 1, 2);
            }
            dp[i][n1][n2][tag][z] += solve2(i + 1, x, n1, tag, 2);
        }
    }
    return dp[i][n1][n2][tag][z];
    
}


signed main(){
 
   
    boost;
    
    cin >> a >> b;
    n = b.size();
    for(int i = 0; i < 18; i++){
        for(int j = 0; j <= 9; j++){
            for(int k = 0; k <= 9; k++){
                for(int l = 0; l < 2; l++){
                    for(int z = 0; z < 3; z++){
                        dp[i][j][k][l][z] = -1;
                    }
                }
            }
        }
    }
    int ans = solve(0,0,0,0,0);
    n = a.size();
    for(int i = 0; i < 18; i++){
        for(int j = 0; j <= 9; j++){
            for(int k = 0; k <= 9; k++){
                for(int l = 0; l < 2; l++){
                    for(int z = 0; z < 3; z++){
                        dp[i][j][k][l][z] = -1;
                    }
                }
            }
        }
    }
    ans -= solve2(0,0,0,0,0);
    int good = 1;
    for(int i = 0; i < a.size(); i++){
        if(i - 1 >= 0){
            if(a[i] == a[i - 1]) good = 0;
        }
        if(i - 2 >= 0){
            if(a[i] == a[i - 2]) good = 0;
            
        }
    }
    cout << ans + good;
    
    return 0;
}

Compilation message

numbers.cpp: In function 'int main()':
numbers.cpp:148:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |     for(int i = 0; i < a.size(); i++){
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Correct 1 ms 340 KB Output is correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Incorrect 0 ms 340 KB Output isn't correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Incorrect 1 ms 340 KB Output isn't correct
12 Correct 1 ms 340 KB Output is correct
13 Incorrect 1 ms 340 KB Output isn't correct
14 Correct 1 ms 340 KB Output is correct
15 Incorrect 1 ms 340 KB Output isn't correct
16 Incorrect 1 ms 340 KB Output isn't correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Incorrect 1 ms 340 KB Output isn't correct
20 Incorrect 1 ms 340 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Incorrect 1 ms 340 KB Output isn't correct
3 Incorrect 1 ms 340 KB Output isn't correct
4 Incorrect 1 ms 340 KB Output isn't correct
5 Correct 1 ms 340 KB Output is correct
6 Incorrect 1 ms 408 KB Output isn't correct
7 Incorrect 1 ms 340 KB Output isn't correct
8 Incorrect 1 ms 340 KB Output isn't correct
9 Incorrect 1 ms 340 KB Output isn't correct
10 Correct 1 ms 320 KB Output is correct
11 Incorrect 1 ms 340 KB Output isn't correct
12 Incorrect 1 ms 340 KB Output isn't correct
13 Incorrect 1 ms 340 KB Output isn't correct
14 Correct 1 ms 320 KB Output is correct
15 Incorrect 1 ms 324 KB Output isn't correct
16 Incorrect 1 ms 340 KB Output isn't correct
17 Incorrect 1 ms 324 KB Output isn't correct
18 Incorrect 1 ms 324 KB Output isn't correct
19 Incorrect 1 ms 340 KB Output isn't correct
20 Incorrect 1 ms 340 KB Output isn't correct
21 Incorrect 1 ms 340 KB Output isn't correct
22 Incorrect 1 ms 320 KB Output isn't correct
23 Incorrect 1 ms 340 KB Output isn't correct
24 Incorrect 1 ms 340 KB Output isn't correct
25 Incorrect 1 ms 340 KB Output isn't correct
26 Incorrect 1 ms 340 KB Output isn't correct
27 Incorrect 1 ms 340 KB Output isn't correct
28 Incorrect 1 ms 328 KB Output isn't correct
29 Incorrect 1 ms 340 KB Output isn't correct
30 Incorrect 1 ms 340 KB Output isn't correct
31 Incorrect 1 ms 340 KB Output isn't correct
32 Incorrect 1 ms 340 KB Output isn't correct
33 Incorrect 1 ms 340 KB Output isn't correct
34 Incorrect 1 ms 340 KB Output isn't correct
35 Incorrect 1 ms 324 KB Output isn't correct
36 Incorrect 1 ms 340 KB Output isn't correct
37 Incorrect 1 ms 340 KB Output isn't correct
38 Incorrect 1 ms 320 KB Output isn't correct
39 Incorrect 1 ms 340 KB Output isn't correct
40 Incorrect 1 ms 324 KB Output isn't correct
41 Incorrect 1 ms 340 KB Output isn't correct
42 Incorrect 1 ms 340 KB Output isn't correct
43 Incorrect 1 ms 324 KB Output isn't correct
44 Incorrect 1 ms 340 KB Output isn't correct
45 Incorrect 1 ms 340 KB Output isn't correct