結果

問題 No.189 SUPER HAPPY DAY
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-02-24 20:52:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 70 ms / 5,000 ms
コード長 3,361 bytes
コンパイル時間 1,877 ms
コンパイル使用メモリ 167,656 KB
実行使用メモリ 43,264 KB
最終ジャッジ日時 2024-04-20 16:27:09
合計ジャッジ時間 3,074 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 19 ms
13,440 KB
testcase_14 AC 26 ms
17,792 KB
testcase_15 AC 32 ms
19,328 KB
testcase_16 AC 37 ms
22,656 KB
testcase_17 AC 36 ms
23,040 KB
testcase_18 AC 29 ms
18,432 KB
testcase_19 AC 38 ms
24,448 KB
testcase_20 AC 11 ms
9,088 KB
testcase_21 AC 11 ms
8,448 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 35 ms
23,296 KB
testcase_24 AC 33 ms
23,552 KB
testcase_25 AC 70 ms
43,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template<long long mod>
struct mint {
private:
    long long x;
public:
    mint(long long x = 0) :x((mod + x) % mod) {}
    mint(std::string &s) {
        long long z = 0;
        for (int i = 0; i < s.size(); i++) {
            z *= 10;
            z += s[i] - '0';
            z %= mod;
        }
        this->x = z;
    }
    mint& operator+=(const mint &a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint &a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint &a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint& operator/=(const mint &a) {
        long long n = mod - 2;
        mint<mod> u = 1, b = a;
        while (n > 0) {
            if (n & 1) {
                u *= b;
            }
            b *= b;
            n >>= 1;
        }
        return *this *= u;
    }
    mint operator+(const mint &a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-(const mint &a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint &a) const {
        mint res(*this);
        return res *= a;
    }
    mint operator/(const mint &a) const {
        mint res(*this);
        return res /= a;
    }
    friend std::ostream& operator<<(std::ostream &os, const mint &n) {
        return os << n.x;
    }
    bool operator==(const mint &a) const {
        return this->x == a.x;
    }
};

int sum(int x) {
    int res = 0;
    while (x) {
        res += x % 10;
        x /= 10;
    }
    return res;
}

int main() {
    constexpr int MOD = 1e9 + 9;
    string m, d; cin >> m >> d;
    vector<vector<vector<mint<MOD>>>> dp(m.size() + 1, vector<vector<mint<MOD>>>(m.size() * 9 + 1, vector<mint<MOD>>(2, 0)));
    vector<vector<vector<mint<MOD>>>> dp2(d.size() + 1, vector<vector<mint<MOD>>>(d.size() * 9 + 1, vector<mint<MOD>>(2, 0)));
    dp[0][0][0] = 1;
    dp2[0][0][0] = 1;
    for (int i = 0; i < m.size(); i++) {
        for (int j = 0; j <= m.size() * 9; j++) {
            if (j + m[i] - '0' <= m.size() * 9) dp[i + 1][j + m[i] - '0'][0] += dp[i][j][0];
            for (int k = 0; k < 10; k++) {
                if (j + k <= m.size() * 9) dp[i + 1][j + k][1] += dp[i][j][1];
                else break;
            }
            for (int k = 0; k < m[i] - '0'; k++) {
                if (j + k <= m.size() * 9) dp[i + 1][j + k][1] += dp[i][j][0];
                else break;
            }
        }
    }
    for (int i = 0; i < d.size(); i++) {
        for (int j = 0; j <= d.size() * 9; j++) {
            if (j + d[i] - '0' <= d.size() * 9) dp2[i + 1][j + d[i] - '0'][0] += dp2[i][j][0];
            for (int k = 0; k < 10; k++) {
                if (j + k <= d.size() * 9) dp2[i + 1][j + k][1] += dp2[i][j][1];
                else break;
            }
            for (int k = 0; k < d[i] - '0'; k++) {
                if (j + k <= d.size() * 9) dp2[i + 1][j + k][1] += dp2[i][j][0];
                else break;
            }
        }
    }
    mint<MOD> ans = 0;
    for (int i = 1; i <= min(d.size(), m.size()) * 9; i++) {
        ans += (dp[m.size()][i][0] + dp[m.size()][i][1]) * (dp2[d.size()][i][0] + dp2[d.size()][i][1]);
    }
    cout << ans << endl;
    return 0;
}
0