Submission #61378996


Source Code Expand

Copy
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;
vector<vector<vector<ll>>> dp;
ll countSnakeNumbers(const string& num, int pos, bool isLimit, bool isLeading, int firstDigit) {
if (pos == num.size()) {
return 1;
}
if (!isLimit && !isLeading && dp[pos][isLimit][firstDigit] != -1) {
return dp[pos][isLimit][firstDigit];
}
ll res = 0;
int limit = isLimit ? num[pos] - '0' : 9;
for (int d = 0; d <= limit; ++d) {
if (isLeading && d == 0) {
res += countSnakeNumbers(num, pos + 1, isLimit && d == limit, true, firstDigit);
} else {
if (firstDigit == 0 || d < firstDigit) {
res += countSnakeNumbers(num, pos + 1, isLimit && d == limit, false, isLeading ? d : firstDigit);
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;
vector<vector<vector<ll>>> dp;
ll countSnakeNumbers(const string& num, int pos, bool isLimit, bool isLeading, int firstDigit) {
    if (pos == num.size()) {
        return 1;
    }
    if (!isLimit && !isLeading && dp[pos][isLimit][firstDigit] != -1) {
        return dp[pos][isLimit][firstDigit];
    }
    ll res = 0;
    int limit = isLimit ? num[pos] - '0' : 9;
    for (int d = 0; d <= limit; ++d) {
        if (isLeading && d == 0) {
            res += countSnakeNumbers(num, pos + 1, isLimit && d == limit, true, firstDigit);
        } else {
            if (firstDigit == 0 || d < firstDigit) {
                res += countSnakeNumbers(num, pos + 1, isLimit && d == limit, false, isLeading ? d : firstDigit);
            }
        }
    }
    if (!isLimit && !isLeading) {
        dp[pos][isLimit][firstDigit] = res;
    }
    return res;
}
ll solve(ll L, ll R) {
    string RStr = to_string(R);
    dp.assign(RStr.size(), vector<vector<ll>>(2, vector<ll>(10, -1)));
    ll countR = countSnakeNumbers(RStr, 0, true, true, 0);
    string LStr = to_string(L - 1);
    dp.assign(LStr.size(), vector<vector<ll>>(2, vector<ll>(10, -1)));
    ll countL = countSnakeNumbers(LStr, 0, true, true, 0);
    return countR - countL;
}
int main() {
    ll L, R;
    cin >> L >> R;
    cout << solve(L, R) << endl;
    return 0;
}

Submission Info

Submission Time
Task C - Snake Numbers
User BagginWithBaggin
Language C++ 20 (gcc 12.2)
Score 350
Code Size 1465 Byte
Status AC
Exec Time 1 ms
Memory 3664 KB

Compile Error

Main.cpp: In function ‘ll countSnakeNumbers(const std::string&, int, bool, bool, int)’:
Main.cpp:8:13: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
    8 |     if (pos == num.size()) {
      |         ~~~~^~~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 350 / 350
Status
AC × 3
AC × 29
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 03_random3_04.txt, 04_handmade_00.txt, 04_handmade_01.txt, 04_handmade_02.txt, 04_handmade_03.txt, 04_handmade_04.txt, 04_handmade_05.txt, 04_handmade_06.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3472 KB
00_sample_01.txt AC 1 ms 3436 KB
00_sample_02.txt AC 1 ms 3468 KB
01_random_00.txt AC 1 ms 3472 KB
01_random_01.txt AC 1 ms 3476 KB
01_random_02.txt AC 1 ms 3480 KB
01_random_03.txt AC 1 ms 3480 KB
01_random_04.txt AC 1 ms 3476 KB
01_random_05.txt AC 1 ms 3464 KB
01_random_06.txt AC 1 ms 3524 KB
01_random_07.txt AC 1 ms 3604 KB
01_random_08.txt AC 1 ms 3504 KB
01_random_09.txt AC 1 ms 3444 KB
02_random2_00.txt AC 1 ms 3664 KB
02_random2_01.txt AC 1 ms 3472 KB
02_random2_02.txt AC 1 ms 3508 KB
02_random2_03.txt AC 1 ms 3664 KB
03_random3_00.txt AC 1 ms 3576 KB
03_random3_01.txt AC 1 ms 3544 KB
03_random3_02.txt AC 1 ms 3480 KB
03_random3_03.txt AC 1 ms 3476 KB
03_random3_04.txt AC 1 ms 3472 KB
04_handmade_00.txt AC 1 ms 3484 KB
04_handmade_01.txt AC 1 ms 3476 KB
04_handmade_02.txt AC 1 ms 3480 KB
04_handmade_03.txt AC 1 ms 3508 KB
04_handmade_04.txt AC 1 ms 3472 KB
04_handmade_05.txt AC 1 ms 3468 KB
04_handmade_06.txt AC 1 ms 3520 KB


2025-03-05 (Wed)
18:02:55 +00:00