Submission #59145

# Submission time Handle Problem Language Result Execution time Memory
59145 2018-07-20T18:03:14 Z SpeedOfMagic Palindrome-Free Numbers (BOI13_numbers) C++17
29.5833 / 100
5 ms 756 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);

        while (digits[i] == prob1 || digits[i] == prob2) {
            if (digits[i] == 0)
                return ans;
            digits[i]--;
        }

        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];
    }

    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 3 ms 328 KB Output is correct
2 Correct 2 ms 484 KB Output is correct
3 Incorrect 4 ms 564 KB Output isn't correct
4 Correct 3 ms 632 KB Output is correct
5 Correct 2 ms 632 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 672 KB Output is correct
9 Correct 3 ms 716 KB Output is correct
10 Correct 2 ms 716 KB Output is correct
11 Correct 3 ms 716 KB Output is correct
12 Correct 2 ms 716 KB Output is correct
13 Incorrect 3 ms 716 KB Output isn't correct
14 Correct 3 ms 716 KB Output is correct
15 Correct 3 ms 716 KB Output is correct
16 Correct 3 ms 716 KB Output is correct
17 Correct 3 ms 716 KB Output is correct
18 Correct 2 ms 716 KB Output is correct
19 Incorrect 4 ms 716 KB Output isn't correct
20 Correct 3 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 716 KB Output isn't correct
2 Incorrect 3 ms 716 KB Output isn't correct
3 Incorrect 5 ms 716 KB Output isn't correct
4 Incorrect 5 ms 716 KB Output isn't correct
5 Correct 3 ms 716 KB Output is correct
6 Incorrect 2 ms 716 KB Output isn't correct
7 Incorrect 4 ms 716 KB Output isn't correct
8 Incorrect 3 ms 716 KB Output isn't correct
9 Incorrect 2 ms 716 KB Output isn't correct
10 Correct 2 ms 716 KB Output is correct
11 Incorrect 3 ms 716 KB Output isn't correct
12 Incorrect 2 ms 716 KB Output isn't correct
13 Incorrect 3 ms 756 KB Output isn't correct
14 Correct 3 ms 756 KB Output is correct
15 Incorrect 4 ms 756 KB Output isn't correct
16 Incorrect 4 ms 756 KB Output isn't correct
17 Incorrect 4 ms 756 KB Output isn't correct
18 Incorrect 4 ms 756 KB Output isn't correct
19 Correct 3 ms 756 KB Output is correct
20 Incorrect 4 ms 756 KB Output isn't correct
21 Incorrect 4 ms 756 KB Output isn't correct
22 Incorrect 3 ms 756 KB Output isn't correct
23 Incorrect 4 ms 756 KB Output isn't correct
24 Incorrect 4 ms 756 KB Output isn't correct
25 Incorrect 5 ms 756 KB Output isn't correct
26 Incorrect 5 ms 756 KB Output isn't correct
27 Incorrect 3 ms 756 KB Output isn't correct
28 Incorrect 3 ms 756 KB Output isn't correct
29 Correct 4 ms 756 KB Output is correct
30 Incorrect 3 ms 756 KB Output isn't correct
31 Incorrect 4 ms 756 KB Output isn't correct
32 Incorrect 4 ms 756 KB Output isn't correct
33 Incorrect 3 ms 756 KB Output isn't correct
34 Incorrect 4 ms 756 KB Output isn't correct
35 Incorrect 3 ms 756 KB Output isn't correct
36 Incorrect 3 ms 756 KB Output isn't correct
37 Incorrect 3 ms 756 KB Output isn't correct
38 Incorrect 3 ms 756 KB Output isn't correct
39 Incorrect 3 ms 756 KB Output isn't correct
40 Incorrect 4 ms 756 KB Output isn't correct
41 Incorrect 3 ms 756 KB Output isn't correct
42 Incorrect 4 ms 756 KB Output isn't correct
43 Incorrect 3 ms 756 KB Output isn't correct
44 Incorrect 3 ms 756 KB Output isn't correct
45 Incorrect 3 ms 756 KB Output isn't correct