結果

問題 No.995 タピオカオイシクナーレ
ユーザー kibunakibuna
提出日時 2020-02-21 21:54:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 4,009 bytes
コンパイル時間 1,543 ms
コンパイル使用メモリ 168,456 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 08:05:14
合計ジャッジ時間 2,618 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 15 ms
5,376 KB
testcase_17 AC 14 ms
5,376 KB
testcase_18 AC 15 ms
5,376 KB
testcase_19 AC 15 ms
5,376 KB
testcase_20 AC 16 ms
5,376 KB
testcase_21 AC 15 ms
5,376 KB
testcase_22 AC 14 ms
5,376 KB
testcase_23 AC 15 ms
5,376 KB
testcase_24 AC 15 ms
5,376 KB
testcase_25 AC 15 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct mint {
    lint v;
    lint _mod;
    mint() : v(0) {}
    mint(signed v, lint _mod = mod) : v(v), _mod(_mod) {}
    mint(lint t, lint _mod = mod) : _mod(_mod) {
        v = t % _mod;
        if (v < 0)
            v += _mod;
    }

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

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

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

    mint operator-() const { return v ? mint(_mod - v) : mint(v); }

    bool operator==(const mint a) const { return v == a.v; }
    bool operator!=(const mint a) const { return v != a.v; }
    bool operator<(const mint a) const { return v < a.v; }
};
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, m, k, p, q;
    cin >> n >> m >> k >> p >> q;
    vector<lint> b(n);
    for (int i = 0; i < n; ++i) {
        cin >> b[i];
    }
    mint in = 0, out = 0;
    for (int i = 0; i < n; ++i) {
        if (i < m) {
            in += b[i];
        } else {
            out += b[i];
        }
    }
    SquareMatrix<2, mint> mat;
    mat[0][0] = mint(q - p) / mint(q);
    mat[0][1] = mint(p) / mint(q);
    mat[1][0] = mint(p) / mint(q);
    mat[1][1] = mint(q - p) / mint(q);
    mat       = mat.pow(k);
    mint ret  = (mat[0][0]) * in + (mat[0][1]) * out;
    cout << ret << "\n";
    return 0;
}
0