Submission #74772226


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
long long dp4[20][10][2][163];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string s;
    cin >> s;
    int n = s.size();
    for (int last = 0; last < 10; last++)
        for (int hz = 0; hz < 2; hz++)
            for (int B = 0; B < 163; B++)
                dp4[0][last][hz][B] = ((hz ? 2LL : 1LL) * B) % MOD;

    for (int rem = 1; rem <= 19; rem++)
        for (int last = 0; last < 10; last++)
            for (int hz = 0; hz < 2; hz++)
                for (int B = 0; B < 163; B++) {
                    long long val = 0;
                    for (int d = 0; d <= 9; d++) {
                        int nB = B + abs(last - d);
                        if (nB >= 163) continue;
                        int nHz = (hz || (d == 0)) ? 1 : 0;
                        val = (val + dp4[rem-1][d][nHz][nB]) % MOD;
                    }
                    dp4[rem][last][hz][B] = val;
                }

    long long ans = 0;
    for (int l = 1; l < n; l++)
        for (int d1 = 1; d1 <= 9; d1++)
            ans = (ans + dp4[l-1][d1][0][0]) % MOD;

    int cur_last = 0, cur_hz = 0, cur_B = 0;
    for (int pos = 0; pos < n; pos++) {
        int limit = s[pos] - '0';
        int start = (pos == 0) ? 1 : 0;

        for (int d = start; d < limit; d++) {
            int nLast = d, nHz, nB;
            if (pos == 0) { nHz = 0; nB = 0; }
            else {
                nB  = cur_B + abs(cur_last - d);
                nHz = (cur_hz || (d == 0)) ? 1 : 0;
            }
            ans = (ans + dp4[n - pos - 1][nLast][nHz][nB]) % MOD;
        }

        if (pos == 0) { cur_last = limit; cur_hz = 0; cur_B = 0; }
        else {
            cur_B  += abs(cur_last - limit);
            cur_hz  = (cur_hz || (limit == 0)) ? 1 : 0;
            cur_last = limit;
        }
    }
    ans = (ans + (long long)(cur_hz ? 2 : 1) * cur_B % MOD) % MOD;

    cout<<ans <<endl;
}

Submission Info

Submission Time
Task E - Hiking Score on a Mountain Trail
User bplizard
Language C++23 (GCC 15.2.0)
Score 466
Code Size 2027 Byte
Status AC
Exec Time 5 ms
Memory 4164 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 5 ms 3900 KiB
in02.txt AC 3 ms 3948 KiB
in03.txt AC 3 ms 4072 KiB
in04.txt AC 3 ms 4140 KiB
in05.txt AC 3 ms 4164 KiB
in06.txt AC 3 ms 4056 KiB
in07.txt AC 3 ms 3948 KiB
in08.txt AC 3 ms 4004 KiB
in09.txt AC 3 ms 3988 KiB
in10.txt AC 3 ms 4068 KiB
in11.txt AC 3 ms 4072 KiB
in12.txt AC 3 ms 4096 KiB
in13.txt AC 3 ms 4096 KiB
in14.txt AC 3 ms 4036 KiB
in15.txt AC 3 ms 3956 KiB
in16.txt AC 3 ms 4072 KiB
in17.txt AC 3 ms 4036 KiB
in18.txt AC 3 ms 4164 KiB
in19.txt AC 3 ms 3988 KiB
in20.txt AC 3 ms 4068 KiB
in21.txt AC 3 ms 4096 KiB
in22.txt AC 3 ms 4096 KiB
in23.txt AC 3 ms 3988 KiB
in24.txt AC 3 ms 4072 KiB
in25.txt AC 3 ms 3980 KiB
in26.txt AC 3 ms 4096 KiB
in27.txt AC 3 ms 4068 KiB
in28.txt AC 3 ms 4084 KiB
in29.txt AC 3 ms 4056 KiB
in30.txt AC 3 ms 4068 KiB
in31.txt AC 3 ms 4096 KiB
in32.txt AC 3 ms 3988 KiB
in33.txt AC 3 ms 4028 KiB
in34.txt AC 3 ms 3948 KiB
in35.txt AC 3 ms 4044 KiB
in36.txt AC 3 ms 4128 KiB
in37.txt AC 3 ms 4040 KiB
in38.txt AC 3 ms 4036 KiB
in39.txt AC 3 ms 4128 KiB
in40.txt AC 3 ms 3920 KiB
sample01.txt AC 3 ms 4040 KiB
sample02.txt AC 3 ms 4164 KiB
sample03.txt AC 3 ms 4140 KiB
sample04.txt AC 3 ms 3956 KiB
sample05.txt AC 3 ms 3944 KiB