Submission #74772262


Source Code Expand

#include <iostream>
#include <vector>
#include <string>
#include <cmath>

using namespace std;

long long MOD = 1e9 + 7;

struct Result {
    long long count_with_zero;
    long long count_without_zero;
    long long sumB_with_zero;
    long long sumB_without_zero;
};

// Memoization table: idx, is_less, is_started, has_zero, last_digit
Result memo[25][2][2][2][10];
bool visited[25][2][2][2][10];
string S;

Result solve(int idx, bool is_less, bool is_started, bool has_zero, int last_digit) {
    if (idx == (int)S.size()) {
        // Base case: we found 1 number. 
        // We categorize it by whether it has a zero.
        if (has_zero) return {1, 0, 0, 0};
        else return {0, 1, 0, 0};
    }

    if (visited[idx][is_less][is_started][has_zero][last_digit]) {
        return memo[idx][is_less][is_started][has_zero][last_digit];
    }

    Result res = {0, 0, 0, 0};
    int limit = is_less ? 9 : S[idx] - '0';

    for (int d = 0; d <= limit; d++) {
        bool next_is_less = is_less || (d < limit);
        bool next_is_started = is_started || (d > 0);
        bool next_has_zero = has_zero || (next_is_started && d == 0);
        
        Result next_res = solve(idx + 1, next_is_less, next_is_started, next_has_zero, d);
        
        long long current_diff = 0;
        if (is_started && next_is_started) {
            current_diff = abs(last_digit - d);
        }

        // 1. Update Counts
        res.count_with_zero = (res.count_with_zero + next_res.count_with_zero) % MOD;
        res.count_without_zero = (res.count_without_zero + next_res.count_without_zero) % MOD;

        // 2. Update Sums (B)
        // If we are currently "started", add the current segment's difference to the running sum
        long long diff_contribution_with = (current_diff * next_res.count_with_zero) % MOD;
        long long diff_contribution_without = (current_diff * next_res.count_without_zero) % MOD;

        res.sumB_with_zero = (res.sumB_with_zero + next_res.sumB_with_zero + diff_contribution_with) % MOD;
        res.sumB_without_zero = (res.sumB_without_zero + next_res.sumB_without_zero + diff_contribution_without) % MOD;
    }

    visited[idx][is_less][is_started][has_zero][last_digit] = true;
    return memo[idx][is_less][is_started][has_zero][last_digit] = res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    if (!(cin >> S)) return 0;

    Result final_res = solve(0, false, false, false, 0);

    // Score = (SumB of trails with 0)*2 + (SumB of trails without 0)*1
    long long total_score = (final_res.sumB_with_zero * 2) % MOD;
    total_score = (total_score + final_res.sumB_without_zero) % MOD;

    cout << total_score << endl;

    return 0;
}

Submission Info

Submission Time
Task E - Hiking Score on a Mountain Trail
User JannatulTajrin
Language C++23 (GCC 15.2.0)
Score 466
Code Size 2796 Byte
Status AC
Exec Time 1 ms
Memory 3728 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 466 / 466
Status
AC × 5
AC × 45
Set Name Test Cases
Sample sample01.txt, sample02.txt, sample03.txt, sample04.txt, sample05.txt
All sample01.txt, sample02.txt, sample03.txt, sample04.txt, sample05.txt, in01.txt, in02.txt, in03.txt, in04.txt, in05.txt, in06.txt, in07.txt, in08.txt, in09.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in40.txt
Case Name Status Exec Time Memory
in01.txt AC 1 ms 3480 KiB
in02.txt AC 1 ms 3332 KiB
in03.txt AC 1 ms 3432 KiB
in04.txt AC 1 ms 3512 KiB
in05.txt AC 1 ms 3524 KiB
in06.txt AC 1 ms 3652 KiB
in07.txt AC 1 ms 3596 KiB
in08.txt AC 1 ms 3632 KiB
in09.txt AC 1 ms 3640 KiB
in10.txt AC 1 ms 3712 KiB
in11.txt AC 1 ms 3552 KiB
in12.txt AC 1 ms 3596 KiB
in13.txt AC 1 ms 3712 KiB
in14.txt AC 1 ms 3596 KiB
in15.txt AC 1 ms 3524 KiB
in16.txt AC 1 ms 3712 KiB
in17.txt AC 1 ms 3560 KiB
in18.txt AC 1 ms 3552 KiB
in19.txt AC 1 ms 3700 KiB
in20.txt AC 1 ms 3560 KiB
in21.txt AC 1 ms 3556 KiB
in22.txt AC 1 ms 3556 KiB
in23.txt AC 1 ms 3540 KiB
in24.txt AC 1 ms 3512 KiB
in25.txt AC 1 ms 3480 KiB
in26.txt AC 1 ms 3544 KiB
in27.txt AC 1 ms 3652 KiB
in28.txt AC 1 ms 3472 KiB
in29.txt AC 1 ms 3712 KiB
in30.txt AC 1 ms 3540 KiB
in31.txt AC 1 ms 3696 KiB
in32.txt AC 1 ms 3536 KiB
in33.txt AC 1 ms 3568 KiB
in34.txt AC 1 ms 3672 KiB
in35.txt AC 1 ms 3556 KiB
in36.txt AC 1 ms 3672 KiB
in37.txt AC 1 ms 3672 KiB
in38.txt AC 1 ms 3672 KiB
in39.txt AC 1 ms 3544 KiB
in40.txt AC 1 ms 3544 KiB
sample01.txt AC 1 ms 3544 KiB
sample02.txt AC 1 ms 3428 KiB
sample03.txt AC 1 ms 3372 KiB
sample04.txt AC 1 ms 3728 KiB
sample05.txt AC 1 ms 3468 KiB