Submission #1121061

# Submission time Handle Problem Language Result Execution time Memory
1121061 2024-11-28T11:15:39 Z AtabayRajabli Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
2 ms 508 KB
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define int long long
using namespace std;

const int sz = 20;
int dp[sz][sz][sz][2];
string s;

int f(int ind, int prv, int prv2, bool same)
{
    if(dp[ind][prv][prv2][same] != -1) return dp[ind][prv][prv2][same];
    if(ind == s.size()) return 1;
    int ans = 0;
    for(int i = 0; i <= 9; i++)
    {
        if((i > s[ind] - '0' && same) || i == prv || i == prv2) continue;
        int val = i;
        if(prv == 10 && i == 0) val = 10;
        if(i == s[ind] - '0')
        {
            ans += f(ind + 1, val, prv, same);
        }
        else
        {
            if(same) 
            {
                if(s[ind] - '0' > i) ans += f(ind + 1, val, prv, !same);
            }
            else
            {
                ans += f(ind + 1, val, prv, same);
            }
        }
    }
    return dp[ind][prv][prv2][same] = ans;
}

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

    int l, r;
    cin >> l >> r;

    memset(dp, -1, sizeof(dp));
    s = to_string(r);
    r = f(0, 10, 10, 1);

    if(l > 0)
    {
        memset(dp, -1, sizeof(dp));
        s = to_string(l - 1);
        l = f(0, 10, 10, 1);
    }

    cout << r - l;
}  

Compilation message

numbers.cpp: In function 'long long int f(long long int, long long int, long long int, bool)':
numbers.cpp:13:12: 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]
   13 |     if(ind == s.size()) return 1;
      |        ~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 508 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 504 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 1 ms 336 KB Output is correct
8 Correct 1 ms 336 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 424 KB Output is correct
12 Correct 1 ms 336 KB Output is correct
13 Correct 1 ms 336 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 1 ms 336 KB Output is correct
17 Correct 2 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 1 ms 336 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 2 ms 336 KB Output is correct
31 Correct 1 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 1 ms 336 KB Output is correct
39 Correct 1 ms 336 KB Output is correct
40 Correct 1 ms 336 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 1 ms 508 KB Output is correct
43 Correct 1 ms 336 KB Output is correct
44 Correct 1 ms 336 KB Output is correct
45 Correct 1 ms 336 KB Output is correct