結果

問題 No.2867 NOT FOUND 404 Again
ユーザー InTheBloom
提出日時 2024-08-30 22:51:14
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 444 ms / 3,000 ms
コード長 1,580 bytes
コンパイル時間 1,164 ms
コンパイル使用メモリ 98,020 KB
実行使用メモリ 162,452 KB
最終ジャッジ日時 2024-08-30 22:51:24
合計ジャッジ時間 9,339 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <tuple>

using namespace std;
using ll = long long;

int main () {
    string n; cin >> n;
    vector<int> N(n.size());
    for (int i = 0; i < n.size(); i++) N[i] = n[i] - '0';
    const int len = static_cast<int>(n.size());

    const ll MOD = 998244353;
    const auto ban = vector<int>({4, 0, 4});
    int sz = static_cast<int> (n.size()) * 2 * 4;
    vector<ll> mp(sz + 10, -1);

    auto encode = [&] (int pos, bool less, int cur) {
        int res = 0;
        int p = 1;
        res += p * pos;
        p *= len;
        res += p * static_cast<int> (less);
        p *= 2;
        res += p * cur;
        return res;
    };

    auto dp = [&] (auto dp, int pos, bool less, int cur) {
        auto key = encode(pos, less, cur);
        if (mp[key] != -1) return mp[key];
        if (pos == len) {
            if (cur == 3) return 0LL;
            return 1LL;
        }

        ll res = 0;
        for (int nex = 0; nex < 10; nex++) {
            if (!less && N[pos] < nex) continue;
            bool nl = less || nex < N[pos];
            int nc = cur;
            if (nc < 3) {
                if (ban[nc] == nex) {
                    nc++;
                }
                else if (nex == 4) {
                    nc = 1;
                }
                else {
                    nc = 0;
                }
            }

            res += dp(dp, pos + 1, nl, nc);
            res %= MOD;
        }

        return mp[key] = res;
    };

    cout << dp(dp, 0, false, 0) - 1 << "\n";
}
0