답안 #88211

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
88211 2018-12-04T14:00:21 Z popovicirobert Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
3 ms 744 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

ll dp[20][10][10];
ll sum[20][10];

inline void prec() {
    for(int a = 0; a < 10; a++) {
        for(int b = 0; b < 10; b++) {
            if(a != b) {
                dp[2][b][a] = 1;
            }
        }
    }
    for(int i = 3; i < 20; i++) {
        for(int a = 0; a < 10; a++) {
            for(int b = 0; b < 10; b++) {
                for(int c = 0; c < 10; c++) {
                    if(a != b && a != c) {
                        dp[i][b][a] += dp[i - 1][c][b];
                    }
                }
            }
        }
    }
    for(int i = 0; i < 10; i++) {
        sum[1][i] = 1;
    }
    for(int i = 2; i < 20; i++) {
        for(int a = 0; a < 10; a++) {
            for(int b = 0; b < 10; b++) {
                sum[i][a] += dp[i][b][a];
            }
        }
    }
}

inline ll solve(ll x) {
    if(x == 0) {
        return -1;
    }
    vector <int> dig;
    while(x > 0) {
        dig.push_back(x % 10);
        x /= 10;
    }
    int sz = dig.size();
    ll ans = 0;
    for(int i = sz - 1; i > 0; i--) {
        for(int a = 1; a < 10; a++) {
            ans += sum[i][a];
        }
    }
    int c1 = -1, c2 = -2, c3 = -3;
    for(int i = sz - 1; i >= 0; i--) {
        for(int a = (i == sz - 1); a < dig[i]; a++) {
            if(i == 0) {
                if(a != c2 && a != c3) {
                    ans++;
                }
            }
            else {
                for(int b = 0; b < 10; b++) {
                    if(a != c2 && a != c3 && b != a && b != c3) {
                        ans += dp[i + 1][b][a];
                        //cerr << b << " " << a << "\n";
                    }
                }
            }
        }
        //cerr << "\n";
        //cerr << ans << " ";
        c1 = c2;
        c2 = c3;
        c3 = dig[i];
        if(c1 == c2 || c1 == c3 || c2 == c3) {
            break;
        }
    }
    //cerr << "\n";
    //cerr << ans << "\n";
    return ans;
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    //int ;
    ll a, b;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> a >> b;
    prec();
    cout << solve(b + 1) - solve(a);
    //cin.close();
    //cout.close();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 3 ms 500 KB Output is correct
4 Correct 2 ms 500 KB Output is correct
5 Correct 3 ms 500 KB Output is correct
6 Correct 2 ms 500 KB Output is correct
7 Correct 2 ms 544 KB Output is correct
8 Correct 2 ms 564 KB Output is correct
9 Correct 2 ms 592 KB Output is correct
10 Correct 2 ms 592 KB Output is correct
11 Correct 2 ms 592 KB Output is correct
12 Correct 2 ms 624 KB Output is correct
13 Correct 2 ms 624 KB Output is correct
14 Correct 2 ms 624 KB Output is correct
15 Correct 3 ms 636 KB Output is correct
16 Correct 2 ms 636 KB Output is correct
17 Correct 2 ms 636 KB Output is correct
18 Correct 2 ms 636 KB Output is correct
19 Correct 2 ms 636 KB Output is correct
20 Correct 2 ms 636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 636 KB Output is correct
2 Correct 2 ms 636 KB Output is correct
3 Correct 2 ms 636 KB Output is correct
4 Correct 2 ms 636 KB Output is correct
5 Correct 2 ms 636 KB Output is correct
6 Correct 2 ms 636 KB Output is correct
7 Correct 2 ms 636 KB Output is correct
8 Correct 2 ms 636 KB Output is correct
9 Correct 3 ms 636 KB Output is correct
10 Correct 2 ms 636 KB Output is correct
11 Correct 2 ms 636 KB Output is correct
12 Correct 2 ms 636 KB Output is correct
13 Correct 2 ms 636 KB Output is correct
14 Correct 3 ms 636 KB Output is correct
15 Correct 2 ms 636 KB Output is correct
16 Correct 2 ms 716 KB Output is correct
17 Correct 3 ms 716 KB Output is correct
18 Correct 3 ms 716 KB Output is correct
19 Correct 2 ms 716 KB Output is correct
20 Correct 0 ms 716 KB Output is correct
21 Correct 2 ms 716 KB Output is correct
22 Correct 2 ms 716 KB Output is correct
23 Correct 2 ms 716 KB Output is correct
24 Correct 3 ms 740 KB Output is correct
25 Correct 2 ms 740 KB Output is correct
26 Correct 2 ms 740 KB Output is correct
27 Correct 2 ms 740 KB Output is correct
28 Correct 2 ms 740 KB Output is correct
29 Correct 2 ms 740 KB Output is correct
30 Correct 2 ms 740 KB Output is correct
31 Correct 3 ms 740 KB Output is correct
32 Correct 2 ms 740 KB Output is correct
33 Correct 2 ms 744 KB Output is correct
34 Correct 2 ms 744 KB Output is correct
35 Correct 2 ms 744 KB Output is correct
36 Correct 2 ms 744 KB Output is correct
37 Correct 2 ms 744 KB Output is correct
38 Correct 2 ms 744 KB Output is correct
39 Correct 2 ms 744 KB Output is correct
40 Correct 2 ms 744 KB Output is correct
41 Correct 2 ms 744 KB Output is correct
42 Correct 2 ms 744 KB Output is correct
43 Correct 2 ms 744 KB Output is correct
44 Correct 2 ms 744 KB Output is correct
45 Correct 2 ms 744 KB Output is correct