답안 #474773

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
474773 2021-09-19T16:04:08 Z Duy_e Palindrome-Free Numbers (BOI13_numbers) C++14
95 / 100
4 ms 332 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<long long, long long>
#define st first
#define nd second
#define file "test"
using namespace std;
const long long oo = 1e18;
const long long N = 2e5 + 5;

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

ll cal(int pos, int pre, int cur, bool f, bool lz){

    if (pos == num.size()) return 1;

    ll &ans = dp[pos][pre][cur][f][lz];
    if (ans != - 1) return ans;
    ans = 0;

    ll lmt = num[pos];
    if (f) lmt = 9;

    for (int i = 0; i <= lmt; i ++)
    if (i != pre && i != cur){
        bool new_f = f, new_lz = lz & (i == 0);
        int new_pre, new_cur;
        if (f == 0 && i < lmt) new_f = 1;

        if (new_lz) new_pre = new_cur = 10;
        else {
            new_cur = i;
            if (lz) new_pre = 10; else new_pre = cur;
        }

        ans += cal(pos + 1, new_pre, new_cur, new_f, new_lz);
    }
    return ans;
}

ll solve(ll n){
    memset(dp, -1, sizeof dp);
    if (n <= 0) return 0;
    num.clear();

    while (n > 0) num.push_back(n%10), n/=10;
    reverse(num.begin(), num.end());
    return cal(0, 10, 10, 0, 1);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    ll A, B;
    cin >> A >> B;
    cout << solve(B) - solve(A - 1);

    return 0;
}    
/**  /\_/\
 *  (= ._.)
 *  / >0  \>1
**/

Compilation message

numbers.cpp: In function 'long long int cal(int, int, int, bool, bool)':
numbers.cpp:17:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |     if (pos == num.size()) return 1;
      |         ~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Incorrect 1 ms 332 KB Output isn't correct
6 Incorrect 0 ms 332 KB Output isn't correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Incorrect 1 ms 332 KB Output isn't correct
16 Correct 1 ms 312 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Incorrect 1 ms 332 KB Output isn't correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 4 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 312 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 1 ms 332 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 1 ms 332 KB Output is correct
39 Correct 1 ms 332 KB Output is correct
40 Correct 1 ms 316 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct