結果

問題 No.3549 SigMax Digits (Judge ver.)
コンテスト
ユーザー zawakasu
提出日時 2026-05-22 22:36:35
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1,284 ms / 3,000 ms
コード長 3,371 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,636 ms
コンパイル使用メモリ 224,508 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-05-22 22:36:43
合計ジャッジ時間 7,415 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_1
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <tuple>
#include <ranges>
#include <random>
// #include "Src/Number/IntegerDivision.hpp"
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// #include "Src/Algebra/Group/AdditiveGroup.hpp"
// #include "Src/DataStructure/FenwickTree/FenwickTree.hpp"
// #include "Src/DataStructure/SegmentTree/SegmentTree.hpp"
// #include "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
// #include "Src/DataStructure/Heap/BinaryHeap.hpp"
namespace zawa {}
using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;
// #include <array>
// #include <bit>
// #include <bitset>
// #include <climits>
// #include <cmath>
// #include <set>
// #include <unordered_set>
// #include <map>
// #include <unordered_map>
// #include <optional>
// #include <queue>
// #include <stack>
// #include <deque>
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0 ; i < ssize(v) ; i++)
        os << v[i] << (i + 1 == ssize(v) ? "" : " ");
    return os;
}
/*
 * むずそう。
 * mod 9でうまくできない?
 * 997,998,999,1000,1001,1002,1003,...,1010,
 * +1,+1,+2,+3,+4,+5,+6,+7,+8,+9,+1,+1,
 * +1,+2,+4,+7,+11,+16,+22,+29,+37,+46,+47,+48,...
 * +1,+2,+4,+7,+2,+7,
 *
 * Judge verの方が簡単説ある
 * g(R)-g(L-1)
 * 桁dp
 */
long long f(long long N) {
    const string S = to_string(N);
    const int n = ssize(S);
    vector dp(n+1,vector(2,vector<long long>(10)));
    vector vis(n+1,vector(2,vector<bool>(10)));
    auto rec = [&](auto rec,int dig,int up,int mx) -> long long {
        long long& resdp = dp[dig][up][mx];
        if (vis[dig][up][mx])
            return resdp;
        vis[dig][up][mx] = 1;
        if (dig == n)
            return resdp = 1;
        for (int i = 0 ; i <= mx ; i++) {
            if (up and i > S[dig]-'0')
                break;
            int nup = up and i == S[dig]-'0';
            int nmx = max(mx,i);
            resdp += rec(rec,dig+1,nup,nmx);
        }
        return resdp;
    };
    vector<long long> cnt(10);
    for (int i = 0 ; i <= 9 ; i++)
        cnt[i] = rec(rec,0,1,i);
    long long res = 0;
    for (int i = 1 ; i <= 9 ; i++)
        res += (long long)i*(cnt[i]-cnt[i-1]);
    return res;
}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);
#if !defined DEBUG
    int T;
    cin >> T;
    while (T--) {
        long long L,R;
        cin >> L >> R;
        cout << f(R)-f(L-1) << '\n';
    }
#else
    mt19937_64 mt{random_device{}()};
    for (int testcase = 0 ; ; ) {
        cerr << "----------" << ++testcase << "----------" << endl;
        
        auto a = solve(), b = naive();
        if (a != b) {
            // print testcase

            cerr << "you: " << a << endl;
            cout << "correct: " << b << endl;
            exit(0);
        }
    }
#endif
}
0