Submission #59149

# Submission time Handle Problem Language Result Execution time Memory
59149 2018-07-20T18:14:29 Z SpeedOfMagic Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
5 ms 960 KB
/** MIT License Copyright (c) 2018 Vasilyev Daniil **/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
template<typename T> using v = vector<T>;
#define int long long
typedef string str;
typedef vector<int> vint;
#define rep(a, l, r) for(int a = (l); a < (r); a++)
#define pb push_back
#define sz(a) ((int) a.size())
const long long inf = 4611686018427387903; //2^62 - 1
#if 0  //FileIO
const string fileName = "";
ifstream fin ((fileName == "" ? "input.txt"  : fileName + ".in" ));
ofstream fout((fileName == "" ? "output.txt" : fileName + ".out"));
#define get fin>>
#define put fout<<
#else
#define get cin>>
#define put cout<<
#endif
#define eol put endl
void read() {}     template<typename Arg,typename... Args> void read (Arg& arg,Args&... args){get (arg)     ;read(args...) ;}
void print(){}     template<typename Arg,typename... Args> void print(Arg  arg,Args...  args){put (arg)<<" ";print(args...);}
void debug(){eol;} template<typename Arg,typename... Args> void debug(Arg  arg,Args...  args){put (arg)<<" ";debug(args...);}
int getInt(){int a; get a; return a;}
//code goes here
int solve(int r) {
    if (r <= 9)
        return r + 1;

    int ans = 0;

    int dd = 1;
    while (dd * 10 <= r)
        dd *= 10;
    ans += solve(dd - 1);

    vint digits;
    while (r) {
        digits.pb(r % 10);
        r /= 10;
    }
    reverse(digits.begin(), digits.end());
    int n = sz(digits);

    int dp[n][10][11]; //dp[i][j][k] - i-th digit; j-th cur digit; k-th cur previous digit.
    rep(i, 0, n) {
        int prob1 = (i ? digits[i - 1] : 10),
            prob2 = (i > 1 ? digits[i - 2] : 10);

        bool cantGoFurther = 0;
        if (digits[i] == prob1 || digits[i] == prob2)
            cantGoFurther = 1;

        rep(j, 0, 10) {
            rep(k, 0, 11)
                dp[i][j][k] = 0;

            if (digits[i] < j || (i + 1 < n && digits[i] == j) || (i == 0 && j == 0))
                continue;

            if (j != prob1 && j != prob2)
                dp[i][j][prob1] = 1;
        }

        rep(p, i + 1, n)
            rep(j, 0, 10)
                rep(k, 0, 11) {
                    dp[p][j][k] = 0;
                    if (k == 10)
                        break;

                    if (j != k)
                        rep(l, 0, 11)
                            if (l != j && l != k)
                                dp[p][j][k] += dp[p - 1][k][l];
                }

        rep(j, 0, 10)
            rep(k, 0, 10)
                ans += dp[n - 1][j][k];

        if (cantGoFurther)
            break;
    }

    return ans;
}

void run() {
    int a, b;
    read(a, b);
    put solve(b) - solve(a - 1);
}

int32_t main() {srand(time(0)); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); put fixed; put setprecision(15); run(); return 0;}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 496 KB Output is correct
3 Correct 3 ms 496 KB Output is correct
4 Correct 2 ms 496 KB Output is correct
5 Correct 2 ms 496 KB Output is correct
6 Correct 2 ms 496 KB Output is correct
7 Correct 2 ms 524 KB Output is correct
8 Correct 3 ms 576 KB Output is correct
9 Correct 2 ms 704 KB Output is correct
10 Correct 2 ms 704 KB Output is correct
11 Correct 3 ms 704 KB Output is correct
12 Correct 3 ms 704 KB Output is correct
13 Correct 3 ms 768 KB Output is correct
14 Correct 3 ms 768 KB Output is correct
15 Correct 2 ms 768 KB Output is correct
16 Correct 2 ms 768 KB Output is correct
17 Correct 2 ms 768 KB Output is correct
18 Correct 2 ms 768 KB Output is correct
19 Correct 3 ms 768 KB Output is correct
20 Correct 2 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 768 KB Output is correct
2 Correct 2 ms 768 KB Output is correct
3 Correct 4 ms 768 KB Output is correct
4 Correct 3 ms 768 KB Output is correct
5 Correct 3 ms 768 KB Output is correct
6 Correct 3 ms 768 KB Output is correct
7 Correct 3 ms 768 KB Output is correct
8 Correct 5 ms 768 KB Output is correct
9 Correct 3 ms 824 KB Output is correct
10 Correct 2 ms 824 KB Output is correct
11 Correct 4 ms 824 KB Output is correct
12 Correct 3 ms 824 KB Output is correct
13 Correct 3 ms 824 KB Output is correct
14 Correct 2 ms 824 KB Output is correct
15 Correct 2 ms 824 KB Output is correct
16 Correct 3 ms 824 KB Output is correct
17 Correct 3 ms 824 KB Output is correct
18 Correct 4 ms 824 KB Output is correct
19 Correct 3 ms 824 KB Output is correct
20 Correct 2 ms 824 KB Output is correct
21 Correct 2 ms 824 KB Output is correct
22 Correct 3 ms 824 KB Output is correct
23 Correct 3 ms 824 KB Output is correct
24 Correct 3 ms 824 KB Output is correct
25 Correct 2 ms 824 KB Output is correct
26 Correct 3 ms 824 KB Output is correct
27 Correct 3 ms 824 KB Output is correct
28 Correct 3 ms 824 KB Output is correct
29 Correct 3 ms 824 KB Output is correct
30 Correct 2 ms 832 KB Output is correct
31 Correct 3 ms 836 KB Output is correct
32 Correct 2 ms 840 KB Output is correct
33 Correct 3 ms 844 KB Output is correct
34 Correct 3 ms 848 KB Output is correct
35 Correct 3 ms 852 KB Output is correct
36 Correct 2 ms 960 KB Output is correct
37 Correct 2 ms 960 KB Output is correct
38 Correct 3 ms 960 KB Output is correct
39 Correct 2 ms 960 KB Output is correct
40 Correct 3 ms 960 KB Output is correct
41 Correct 4 ms 960 KB Output is correct
42 Correct 2 ms 960 KB Output is correct
43 Correct 3 ms 960 KB Output is correct
44 Correct 4 ms 960 KB Output is correct
45 Correct 3 ms 960 KB Output is correct