답안 #77207

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
77207 2018-09-24T01:38:03 Z Flying_dragon_02 Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
10 ms 2300 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

typedef pair<int, int> ii;

string a, b, s;

long long dp[21][15][15][21][2];

long long f(int pos, int num1, int num2, int fA, int fB) {
    if(pos == 20)
        return 1;
    long long ret = dp[pos][num1][num2][fA][fB];
    if(fB) {
        ret = 0;
        int l = 0, r = 9;
        if(fB) r = s[pos] - '0';
        for(int i = l; i <= r; i++) {
            int newA = fA, newB = 0;
            if(fA == 0 && i > 0) newA = pos;
            if(fB && i == r) newB = 1;
            if(newA >= 1 && abs(pos - newA) >= 1 && num1 == i) continue;
            if(newA >= 1 && abs(pos - newA) >= 2 && num2 == i) continue;
            ret += f(pos + 1, i, num1, newA, newB);
        }
        return ret;
    }
    else {
        if(ret >= 0) return ret;
        ret = 0;
        int l = 0, r = 9;
        for(int i = l; i <= r; i++) {
            int newA = fA, newB = 0;
            if(fA == 0 && i > 0) newA = pos;
            if(newA >= 1 && abs(pos - newA) >= 1 && num1 == i) continue;
            if(newA >= 1 && abs(pos - newA) >= 2 && num2 == i) continue;
            ret += f(pos + 1, i, num1, newA, newB);
        }
        dp[pos][num1][num2][fA][fB] = ret;
        return ret;
    }
}

long long get(string h) {
    while(h.length() <= 19)
        h = "0" + h;
    s = h;
    return f(1, 0, 0, 0, 1);
}

int main() {
    cin.tie(0), ios::sync_with_stdio(0);
    //freopen("Numbers.inp", "r", stdin);
    cin >> a >> b;
    for(int i = 0; i <= 20; i++) {
        for(int j = 0; j < 15; j++) {
            for(int k = 0; k < 15; k++) {
                for(int l = 0; l <= 20; l++) {
                    for(int o = 0; o < 2; o++)
                        dp[i][j][k][l][o] = -1;
                }
            }
        }
    }
    long long ans = get(b) - get(a);
    int ck = 0;
    for(int i = 1; i < a.length(); i++) {
        if(a[i] == a[i - 1])
            ck = 1;
    }
    for(int i = 2; i < a.length(); i++) {
        if(a[i] == a[i - 2])
            ck = 1;
    }
    if(ck == 0) ans++;
    cout << ans << "\n";
}

Compilation message

numbers.cpp: In function 'int main()':
numbers.cpp:73:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1; i < a.length(); i++) {
                    ~~^~~~~~~~~~~~
numbers.cpp:77:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 2; i < a.length(); i++) {
                    ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1912 KB Output is correct
2 Correct 3 ms 2044 KB Output is correct
3 Correct 4 ms 2044 KB Output is correct
4 Correct 3 ms 2044 KB Output is correct
5 Correct 3 ms 2044 KB Output is correct
6 Correct 3 ms 2044 KB Output is correct
7 Correct 3 ms 2180 KB Output is correct
8 Correct 3 ms 2180 KB Output is correct
9 Correct 3 ms 2180 KB Output is correct
10 Correct 3 ms 2180 KB Output is correct
11 Correct 3 ms 2180 KB Output is correct
12 Correct 3 ms 2180 KB Output is correct
13 Correct 3 ms 2180 KB Output is correct
14 Correct 3 ms 2180 KB Output is correct
15 Correct 3 ms 2180 KB Output is correct
16 Correct 4 ms 2180 KB Output is correct
17 Correct 3 ms 2180 KB Output is correct
18 Correct 3 ms 2180 KB Output is correct
19 Correct 4 ms 2180 KB Output is correct
20 Correct 4 ms 2180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2180 KB Output is correct
2 Correct 4 ms 2180 KB Output is correct
3 Correct 4 ms 2180 KB Output is correct
4 Correct 4 ms 2180 KB Output is correct
5 Correct 3 ms 2180 KB Output is correct
6 Correct 3 ms 2180 KB Output is correct
7 Correct 3 ms 2180 KB Output is correct
8 Correct 4 ms 2180 KB Output is correct
9 Correct 3 ms 2180 KB Output is correct
10 Correct 3 ms 2180 KB Output is correct
11 Correct 3 ms 2180 KB Output is correct
12 Correct 3 ms 2180 KB Output is correct
13 Correct 4 ms 2180 KB Output is correct
14 Correct 3 ms 2180 KB Output is correct
15 Correct 3 ms 2180 KB Output is correct
16 Correct 4 ms 2180 KB Output is correct
17 Correct 4 ms 2180 KB Output is correct
18 Correct 4 ms 2180 KB Output is correct
19 Correct 6 ms 2180 KB Output is correct
20 Correct 5 ms 2180 KB Output is correct
21 Correct 4 ms 2180 KB Output is correct
22 Correct 4 ms 2180 KB Output is correct
23 Correct 4 ms 2180 KB Output is correct
24 Correct 4 ms 2180 KB Output is correct
25 Correct 10 ms 2300 KB Output is correct
26 Correct 4 ms 2300 KB Output is correct
27 Correct 4 ms 2300 KB Output is correct
28 Correct 4 ms 2300 KB Output is correct
29 Correct 5 ms 2300 KB Output is correct
30 Correct 4 ms 2300 KB Output is correct
31 Correct 4 ms 2300 KB Output is correct
32 Correct 4 ms 2300 KB Output is correct
33 Correct 4 ms 2300 KB Output is correct
34 Correct 4 ms 2300 KB Output is correct
35 Correct 4 ms 2300 KB Output is correct
36 Correct 5 ms 2300 KB Output is correct
37 Correct 5 ms 2300 KB Output is correct
38 Correct 4 ms 2300 KB Output is correct
39 Correct 4 ms 2300 KB Output is correct
40 Correct 4 ms 2300 KB Output is correct
41 Correct 4 ms 2300 KB Output is correct
42 Correct 4 ms 2300 KB Output is correct
43 Correct 5 ms 2300 KB Output is correct
44 Correct 4 ms 2300 KB Output is correct
45 Correct 4 ms 2300 KB Output is correct