Submission #490738

#TimeUsernameProblemLanguageResultExecution timeMemory
490738tht2005Palindrome-Free Numbers (BOI13_numbers)C++14
96.25 / 100
1 ms344 KiB
#include <stdio.h>
#include <string.h>

#define LL  long long

int n;
char s[20];
LL dp[19][11][11][2][2];

LL calc(int i, int j, int k, int tight, int lz) {
    if(i == n) return 1;
    LL& res = dp[i][j][k][tight][lz];
    if(~res) return res;
    res = 0;
    if(tight) {
        if(lz) {
            res += calc(i + 1, 10, 10, 1, 1);
            for(int dig = 1; dig < 10; ++dig)
                res += calc(i + 1, 10, dig, 1, 0);
        }
        else {
            for(int dig = 0; dig < 10; ++dig) {
                if(dig != j && dig != k)
                    res += calc(i + 1, k, dig, 1, 0);
            }
        }
    }
    else {
        if(lz) {
            res += calc(i + 1, 10, 10, s[i] > '0', 1);
            for(int dig = 1; dig < s[i] - '0'; ++dig)
                res += calc(i + 1, k, dig, 1, 0);
            if(s[i] > '0' && s[i] != j + '0' && s[i] != k + '0')
                res += calc(i + 1, k, s[i] - '0', 0, 0);
        }
        else {
            for(int dig = 0; dig < s[i] - '0'; ++dig) {
                if(dig != j && dig != k) {
                    res += calc(i + 1, k, dig, 1, 0);
                }
            }
            if(s[i] != j + '0' && s[i] != k + '0')
                res += calc(i + 1, k, s[i] - '0', 0, 0);
        }
    }
    return res;
}

LL f(LL N) {
    sprintf(s, "%lld", N);
    n = strlen(s);
    memset(dp, -1, sizeof(dp));
    return calc(0, 10, 10, 0, 1);
}

int main() {
    LL L, R;
    scanf("%lld %lld", &L, &R);
    printf("%lld", f(R) - f(L - 1));
    return 0;
}

Compilation message (stderr)

numbers.cpp: In function 'int main()':
numbers.cpp:58:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   58 |     scanf("%lld %lld", &L, &R);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...