답안 #107974

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
107974 2019-04-26T11:13:12 Z bestin_1 Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
5 ms 512 KB
#include <iostream>
#include <random>
#include <chrono>
#include <algorithm>
#include <cstring>

#define rep(i, x) for(int i = 0; i < x; i++)
using namespace std;
typedef long long ll;
ll MOD = (ll) 1e9 + 7;
const int N = 1e5 + 5, inf = 1e9 + 5;

ll add(ll x, ll y) {
    x += y;
    if (x >= MOD) return x - MOD;
    return x;
}

ll sub(ll x, ll y) {
    x -= y;
    if (x < 0) return x + MOD;
    return x;
}

ll mult(ll x, ll y) { return (x * y) % MOD; }

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll dp[20][2][11][11];
vector<int> num;

// function for [0, x]
ll f(int pos, bool less, int slast, int last) {
    if (pos == num.size()) return 1;
    if (dp[pos][less][slast][last] != -1) return dp[pos][less][slast][last];

    ll ans = 0;
    for (int digit = 0; digit <= 9; digit++) {
        if (!less && digit > num[pos]) break;
        if (slast == digit || last == digit) continue;
        // if last is a leading zero (i.e. a 10) then the current zero is also a leading zero.
        ans += f(pos + 1, less || digit < num[pos], last, last == 10 && digit == 0 ? 10 : digit);
    }
    return dp[pos][less][slast][last] = ans;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll a, b;
    cin >> a >> b;
    if (b == 0) cout << 1 << endl;
    else {
        for (; b > 0; b /= 10) num.push_back(b % 10);
        reverse(begin(num), end(num));
        memset(dp, -1, sizeof(dp));
        ll b_ans = f(0, false, 10, 10); // 10 is leading zero
        if (a == 0) cout << b_ans << endl;
        else {
            num.clear();
            a--;
            if (a == -1) cout << b_ans << endl;
            for (; a > 0; a /= 10) num.push_back(a % 10);
            reverse(begin(num), end(num));
            memset(dp, -1, sizeof(dp));
            ll a_ans = f(0, false, 10, 10); // 10 is leading zero
            cout << b_ans - a_ans << endl;
        }
    }
}

Compilation message

numbers.cpp: In function 'll f(int, bool, int, int)':
numbers.cpp:34:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if (pos == num.size()) return 1;
         ~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 2 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 380 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 4 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 3 ms 512 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 3 ms 384 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 3 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 3 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 3 ms 384 KB Output is correct
35 Correct 3 ms 384 KB Output is correct
36 Correct 3 ms 384 KB Output is correct
37 Correct 2 ms 384 KB Output is correct
38 Correct 3 ms 384 KB Output is correct
39 Correct 3 ms 384 KB Output is correct
40 Correct 2 ms 384 KB Output is correct
41 Correct 3 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct
43 Correct 3 ms 384 KB Output is correct
44 Correct 3 ms 384 KB Output is correct
45 Correct 3 ms 384 KB Output is correct