結果
問題 | No.995 タピオカオイシクナーレ |
ユーザー |
![]() |
提出日時 | 2020-02-21 21:54:18 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 13 ms / 2,000 ms |
コード長 | 4,009 bytes |
コンパイル時間 | 1,905 ms |
コンパイル使用メモリ | 172,880 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-10-08 22:42:00 |
合計ジャッジ時間 | 2,516 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 23 |
ソースコード
#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;}