Submission #95272

# Submission time Handle Problem Language Result Execution time Memory
95272 2019-01-29T09:55:40 Z rkocharyan Palindrome-Free Numbers (BOI13_numbers) C++14
85 / 100
19 ms 424 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 20;

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

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

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

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);
    if(r - l <= 1e6) {
        cout << brute(l, r) << '\n';
    } else {
        if(l == -1) {
            cout << solve(b) << '\n';
            return 0;
        }
        cout << solve(b) - solve(a) << '\n';
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 19 ms 376 KB Output is correct
4 Correct 11 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 424 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 11 ms 376 KB Output is correct
15 Correct 11 ms 256 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 5 ms 376 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 19 ms 376 KB Output is correct
20 Correct 11 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Incorrect 2 ms 376 KB Output isn't correct
6 Correct 2 ms 376 KB Output is correct
7 Incorrect 2 ms 376 KB Output isn't correct
8 Incorrect 2 ms 376 KB Output isn't correct
9 Incorrect 2 ms 376 KB Output isn't correct
10 Incorrect 2 ms 376 KB Output isn't correct
11 Correct 1 ms 252 KB Output is correct
12 Incorrect 2 ms 376 KB Output isn't correct
13 Incorrect 2 ms 376 KB Output isn't correct
14 Incorrect 2 ms 376 KB Output isn't correct
15 Incorrect 2 ms 380 KB Output isn't correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 420 KB Output is correct
23 Correct 2 ms 256 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 380 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 380 KB Output is correct
35 Correct 2 ms 256 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 2 ms 376 KB Output is correct
38 Correct 2 ms 256 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 376 KB Output is correct
41 Correct 2 ms 376 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 376 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct