답안 #579929

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
579929 2022-06-20T09:37:22 Z birthdaycake Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
1 ms 412 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[20][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);
            }
            if(x != n1) 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++){
                //cout << n2 << ' ' << n1 << ' ' << j << endl;
                if(j != n1 && j != n2) dp[i][n1][n2][tag][z] += solve(i + 1, j, n1, 1, 2);
            }
            if(x != n1 && x != n2) 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);
            }
            if(x != n1) 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);
            }
            if(x != n1 && x != n2) 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 < 20; 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 < 20; 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 < n; 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;
            
        }
    }
    if(a == "0" && b == "0"){
        cout << 1;
        return 0;
    }
    if(a == "0") good++;
    cout << ans + good;
    
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 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 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 0 ms 412 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 1 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct