Submission #95273

# Submission time Handle Problem Language Result Execution time Memory
95273 2019-01-29T10:48:01 Z rkocharyan Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
2 ms 636 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 20;

int n;
long long dp[N][N][N][2][2];
string s;

long long calc(int pos, int c1, int c2, int f, int z) {
    if(pos == n) return 1;
    auto &res = dp[pos][c1][c2][f][z];
    if(res != -1) {
        return res;
    }
    res = 0;
    int lmt = f ? 9 : s[pos] - '0';
    for(int i = 0; i <= lmt; i++) {
        if(i == c1 || i == c2) continue;
        if(z + i == 0) {
            res += calc(pos + 1, 10, 10, 1, 0);
        } else {
            res += calc(pos + 1, c2, i, f | (i < lmt), 1);
        }
    }
    return res;
}

long long solve(string &x) {
    s = x;
    n = (int) x.size();
    memset(dp, -1, sizeof dp);
    return calc(0, 10, 10, 0, 0);
}

bool check(long long x) {
    string s = to_string(x);
    int m = (int) s.size();
    for(int i = 0; i < m - 1; i++) {
        if(s[i] == s[i + 1]) {
            return 0;
        }
    }
    for(int i = 0; i < m - 2; i++) {
        if(s[i] == s[i + 2]) {
            return 0;
        }
    }
    return 1;
}

long long brute(long long l, long long r) {
    int ans = 0;
    for(long long i = l + 1; i <= r; i++) {
        ans += check(i);
    }
    return ans;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    long long l, r;
    cin >> l >> r;
    l--;
    string a, b;
    a = to_string(l);
    b = to_string(r);
    cout << solve(b) - (l >= 0 ? solve(a) : 0) << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 632 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 504 KB Output is correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 504 KB Output is correct
8 Correct 2 ms 636 KB Output is correct
9 Correct 2 ms 636 KB Output is correct
10 Correct 2 ms 632 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 632 KB Output is correct
13 Correct 2 ms 632 KB Output is correct
14 Correct 2 ms 504 KB Output is correct
15 Correct 2 ms 504 KB Output is correct
16 Correct 2 ms 504 KB Output is correct
17 Correct 2 ms 632 KB Output is correct
18 Correct 2 ms 632 KB Output is correct
19 Correct 2 ms 504 KB Output is correct
20 Correct 2 ms 632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 504 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 632 KB Output is correct
9 Correct 2 ms 632 KB Output is correct
10 Correct 2 ms 632 KB Output is correct
11 Correct 2 ms 504 KB Output is correct
12 Correct 2 ms 504 KB Output is correct
13 Correct 2 ms 504 KB Output is correct
14 Correct 2 ms 632 KB Output is correct
15 Correct 2 ms 632 KB Output is correct
16 Correct 2 ms 632 KB Output is correct
17 Correct 2 ms 632 KB Output is correct
18 Correct 2 ms 504 KB Output is correct
19 Correct 2 ms 632 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 2 ms 504 KB Output is correct
22 Correct 2 ms 632 KB Output is correct
23 Correct 2 ms 504 KB Output is correct
24 Correct 2 ms 504 KB Output is correct
25 Correct 2 ms 504 KB Output is correct
26 Correct 2 ms 504 KB Output is correct
27 Correct 2 ms 504 KB Output is correct
28 Correct 2 ms 504 KB Output is correct
29 Correct 2 ms 508 KB Output is correct
30 Correct 2 ms 504 KB Output is correct
31 Correct 2 ms 632 KB Output is correct
32 Correct 2 ms 504 KB Output is correct
33 Correct 2 ms 504 KB Output is correct
34 Correct 2 ms 504 KB Output is correct
35 Correct 2 ms 632 KB Output is correct
36 Correct 2 ms 632 KB Output is correct
37 Correct 2 ms 504 KB Output is correct
38 Correct 2 ms 632 KB Output is correct
39 Correct 2 ms 504 KB Output is correct
40 Correct 2 ms 632 KB Output is correct
41 Correct 2 ms 632 KB Output is correct
42 Correct 2 ms 632 KB Output is correct
43 Correct 2 ms 632 KB Output is correct
44 Correct 2 ms 632 KB Output is correct
45 Correct 2 ms 504 KB Output is correct