結果

問題 No.1086 桁和の桁和2
ユーザー kibunakibuna
提出日時 2020-06-19 23:27:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 689 ms / 3,000 ms
コード長 4,575 bytes
コンパイル時間 1,962 ms
コンパイル使用メモリ 174,628 KB
実行使用メモリ 7,168 KB
最終ジャッジ日時 2024-07-22 17:53:54
合計ジャッジ時間 13,073 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,948 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 44 ms
7,168 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 28 ms
6,944 KB
testcase_11 AC 9 ms
6,940 KB
testcase_12 AC 3 ms
6,944 KB
testcase_13 AC 36 ms
6,940 KB
testcase_14 AC 23 ms
6,948 KB
testcase_15 AC 84 ms
6,940 KB
testcase_16 AC 480 ms
6,940 KB
testcase_17 AC 42 ms
6,944 KB
testcase_18 AC 666 ms
7,040 KB
testcase_19 AC 547 ms
6,944 KB
testcase_20 AC 69 ms
6,944 KB
testcase_21 AC 471 ms
6,940 KB
testcase_22 AC 660 ms
7,040 KB
testcase_23 AC 405 ms
6,944 KB
testcase_24 AC 283 ms
6,940 KB
testcase_25 AC 562 ms
6,940 KB
testcase_26 AC 465 ms
6,944 KB
testcase_27 AC 332 ms
6,940 KB
testcase_28 AC 274 ms
6,944 KB
testcase_29 AC 306 ms
6,940 KB
testcase_30 AC 684 ms
7,168 KB
testcase_31 AC 686 ms
7,040 KB
testcase_32 AC 685 ms
7,168 KB
testcase_33 AC 685 ms
7,168 KB
testcase_34 AC 689 ms
7,040 KB
testcase_35 AC 44 ms
7,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

template <int mod>
struct modint {
    lint v;
    modint() : v(0) {}
    modint(signed v) : v(v) {}
    modint(lint t) {
        v = t % mod;
        if (v < 0)
            v += mod;
    }

    modint pow(lint k) {
        modint res(1), tmp(v);
        while (k) {
            if (k & 1)
                res *= tmp;
            tmp *= tmp;
            k >>= 1;
        }
        return res;
    }
    static modint add_identity() { return modint(0); }
    static modint mul_identity() { return modint(1); }
    modint inv() { return pow(mod - 2); }

    modint &operator+=(modint a) {
        v += a.v;
        if (v >= mod)
            v -= mod;
        return *this;
    }
    modint &operator-=(modint a) {
        v += mod - a.v;
        if (v >= mod)
            v -= mod;
        return *this;
    }
    modint &operator*=(modint a) {
        v = v * a.v % mod;
        return *this;
    }
    modint &operator/=(modint a) { return (*this) *= a.inv(); }

    modint operator+(modint a) const { return modint(v) += a; };
    modint operator-(modint a) const { return modint(v) -= a; };
    modint operator*(modint a) const { return modint(v) *= a; };
    modint operator/(modint a) const { return modint(v) /= a; };

    modint operator-() const { return v ? modint(mod - v) : modint(v); }

    bool operator==(const modint a) const { return v == a.v; }
    bool operator!=(const modint a) const { return v != a.v; }
    bool operator<(const modint a) const { return v < a.v; }
};
using mint = modint<mod>;
ostream &operator<<(ostream &os, mint m) { return os << m.v; }

template <size_t N, typename R>
struct SquareMatrix {
    using arr = array<R, N>;
    using mat = array<arr, N>;
    mat dat;

    SquareMatrix() {
        for (size_t i = 0; i < N; i++)
            for (size_t j = 0; j < N; j++)
                dat[i][j] = R::add_identity();
    }
    SquareMatrix &operator=(const SquareMatrix &a) {
        dat = a.dat;
        return (*this);
    }
    bool operator==(const SquareMatrix &a) const { return dat == a.dat; }

    size_t size() const { return N; };
    arr &operator[](size_t k) { return dat[k]; };
    const arr &operator[](size_t k) const { return dat[k]; };

    static SquareMatrix add_identity() { return SquareMatrix(); }
    static SquareMatrix mul_identity() {
        SquareMatrix res;
        for (size_t i = 0; i < N; i++)
            res[i][i] = R::mul_identity();
        return res;
    }

    SquareMatrix operator*(const SquareMatrix &B) const {
        SquareMatrix res;
        for (size_t i = 0; i < N; i++)
            for (size_t j = 0; j < N; j++)
                for (size_t k = 0; k < N; k++)
                    res[i][j] = res[i][j] + (dat[i][k] * B[k][j]);
        return res;
    }

    SquareMatrix operator+(const SquareMatrix &B) const {
        SquareMatrix res;
        for (size_t i = 0; i < N; i++)
            for (size_t j = 0; j < N; j++)
                res[i][j] = dat[i][j] + B[i][j];
        return res;
    }

    SquareMatrix pow(long long n) const {
        SquareMatrix a = *this, res = mul_identity();
        while (n) {
            if (n & 1)
                res = res * a;
            a = a * a;
            n >>= 1;
        }
        return res;
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    lint n;
    cin >> n;
    vector<lint> l(n + 1), r(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> l[i];
    }
    for (int i = 1; i <= n; ++i) {
        cin >> r[i];
    }
    vector<lint> c(n + 1, 0);
    for (int i = 1; i <= n; ++i) {
        cin >> c[i];
    }
    auto d = c;
    for (int i = n; i > 0; --i) {
        d[i] = (d[i] - d[i - 1] + 9);
        if (i != 1)
            d[i] %= 9;
    }
    vector<mint> dp(n + 1, 0);
    dp[0] = 1;
    SquareMatrix<2, mint> mat;
    mat[0][0] = 1;
    mat[0][1] = 1;
    mat[1][0] = 0;
    mat[1][1] = 10;
    bool zero = true;
    for (int i = 1; i <= n; ++i) {
        if (c[i] == 0) {
            if (!zero) {
                cout << 0 << "\n";
                return 0;
            } else {
                dp[i] = 1;
                continue;
            }
        }
        zero    = false;
        int add = d[i];
        auto ml = mat.pow(l[i]);
        auto mr = mat.pow(r[i]);
        mint ls = ml[0][1];
        mint rs = mr[0][1];
        if (add == 0)
            rs += 1;
        dp[i] = dp[i - 1] * (rs - ls);
    }
    cout << dp[n] << "\n";
    return 0;
}
0