Submission #579901

# Submission time Handle Problem Language Result Execution time Memory
579901 2022-06-20T08:41:15 Z birthdaycake Palindrome-Free Numbers (BOI13_numbers) C++17
0 / 100
2 ms 596 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][9][9][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(!z){
        dp[i][n1][n2][tag][z] += solve(i + 1, n1, n2, 1,z);
    }
    if(tag){
        if(z == 0){
            for(int j = 0; j <= 9; j++){
               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';
            for(int j = 0; 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(!z){
        dp[i][n1][n2][tag][z] += solve2(i + 1, n1, n2, 1,z);
    }
    if(tag){
        if(z == 0){
            for(int j = 0; j <= 9; j++){
               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';
            for(int j = 0; 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:145: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]
  145 |     for(int i = 0; i < a.size(); i++){
      |                    ~~^~~~~~~~~~
numbers.cpp:137:43: warning: iteration 9 invokes undefined behavior [-Waggressive-loop-optimizations]
  137 |                         dp[i][j][k][l][z] = -1;
      |                         ~~~~~~~~~~~~~~~~~~^~~~
numbers.cpp:134:30: note: within this loop
  134 |             for(int k = 0; k <= 9; k++){
      |                            ~~^~~~
numbers.cpp:124:43: warning: iteration 9 invokes undefined behavior [-Waggressive-loop-optimizations]
  124 |                         dp[i][j][k][l][z] = -1;
      |                         ~~~~~~~~~~~~~~~~~~^~~~
numbers.cpp:121:30: note: within this loop
  121 |             for(int k = 0; k <= 9; k++){
      |                            ~~^~~~
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 576 KB Execution killed with signal 11
2 Runtime error 1 ms 596 KB Execution killed with signal 11
3 Runtime error 1 ms 596 KB Execution killed with signal 11
4 Runtime error 1 ms 580 KB Execution killed with signal 11
5 Runtime error 1 ms 596 KB Execution killed with signal 11
6 Runtime error 1 ms 468 KB Execution killed with signal 11
7 Runtime error 1 ms 576 KB Execution killed with signal 11
8 Runtime error 1 ms 468 KB Execution killed with signal 11
9 Runtime error 1 ms 468 KB Execution killed with signal 11
10 Runtime error 1 ms 576 KB Execution killed with signal 11
11 Runtime error 1 ms 584 KB Execution killed with signal 11
12 Runtime error 1 ms 596 KB Execution killed with signal 11
13 Runtime error 1 ms 596 KB Execution killed with signal 11
14 Runtime error 1 ms 576 KB Execution killed with signal 11
15 Runtime error 1 ms 468 KB Execution killed with signal 11
16 Runtime error 1 ms 596 KB Execution killed with signal 11
17 Runtime error 1 ms 596 KB Execution killed with signal 11
18 Runtime error 1 ms 596 KB Execution killed with signal 11
19 Runtime error 1 ms 596 KB Execution killed with signal 11
20 Runtime error 1 ms 468 KB Execution killed with signal 11
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 468 KB Execution killed with signal 11
2 Runtime error 1 ms 596 KB Execution killed with signal 11
3 Runtime error 1 ms 596 KB Execution killed with signal 11
4 Runtime error 1 ms 596 KB Execution killed with signal 11
5 Runtime error 1 ms 596 KB Execution killed with signal 11
6 Runtime error 1 ms 596 KB Execution killed with signal 11
7 Runtime error 1 ms 580 KB Execution killed with signal 11
8 Runtime error 1 ms 576 KB Execution killed with signal 11
9 Runtime error 1 ms 596 KB Execution killed with signal 11
10 Runtime error 1 ms 596 KB Execution killed with signal 11
11 Runtime error 1 ms 596 KB Execution killed with signal 11
12 Runtime error 1 ms 596 KB Execution killed with signal 11
13 Runtime error 1 ms 580 KB Execution killed with signal 11
14 Runtime error 1 ms 576 KB Execution killed with signal 11
15 Runtime error 1 ms 468 KB Execution killed with signal 11
16 Runtime error 1 ms 596 KB Execution killed with signal 11
17 Runtime error 1 ms 468 KB Execution killed with signal 11
18 Runtime error 1 ms 596 KB Execution killed with signal 11
19 Runtime error 1 ms 580 KB Execution killed with signal 11
20 Runtime error 1 ms 468 KB Execution killed with signal 11
21 Runtime error 1 ms 468 KB Execution killed with signal 11
22 Runtime error 1 ms 596 KB Execution killed with signal 11
23 Runtime error 1 ms 580 KB Execution killed with signal 11
24 Runtime error 2 ms 468 KB Execution killed with signal 11
25 Runtime error 1 ms 596 KB Execution killed with signal 11
26 Runtime error 1 ms 596 KB Execution killed with signal 11
27 Runtime error 1 ms 468 KB Execution killed with signal 11
28 Runtime error 1 ms 468 KB Execution killed with signal 11
29 Runtime error 1 ms 596 KB Execution killed with signal 11
30 Runtime error 1 ms 596 KB Execution killed with signal 11
31 Runtime error 1 ms 468 KB Execution killed with signal 11
32 Runtime error 1 ms 596 KB Execution killed with signal 11
33 Runtime error 1 ms 596 KB Execution killed with signal 11
34 Runtime error 1 ms 468 KB Execution killed with signal 11
35 Runtime error 1 ms 596 KB Execution killed with signal 11
36 Runtime error 1 ms 580 KB Execution killed with signal 11
37 Runtime error 1 ms 468 KB Execution killed with signal 11
38 Runtime error 1 ms 596 KB Execution killed with signal 11
39 Runtime error 1 ms 468 KB Execution killed with signal 11
40 Runtime error 1 ms 596 KB Execution killed with signal 11
41 Runtime error 1 ms 576 KB Execution killed with signal 11
42 Runtime error 1 ms 596 KB Execution killed with signal 11
43 Runtime error 1 ms 596 KB Execution killed with signal 11
44 Runtime error 1 ms 596 KB Execution killed with signal 11
45 Runtime error 1 ms 596 KB Execution killed with signal 11