結果

問題 No.444 旨味の相乗効果
ユーザー yosupotyosupot
提出日時 2016-11-09 00:21:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 112 ms / 2,500 ms
コード長 3,554 bytes
コンパイル時間 1,118 ms
コンパイル使用メモリ 81,404 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-07 02:03:35
合計ジャッジ時間 2,569 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); }

template<class T>
T pow(T x, ll n) {
    T r = 1;
    while (n) {
        if (n & 1) r *= x;
        x *= x;
        n >>= 1;
    }
    return r;
}


template<uint MD>
struct ModInt {
    uint v;
    ModInt() : v{0} {}
    ModInt(ll v) : v{normS(v%MD+MD)} {}
    static uint normS(const uint &x) {return (x<MD)?x:x-MD;};
    static ModInt make(const uint &x) {ModInt m; m.v = x; return m;}
    ModInt operator+(const ModInt &r) const {return make(normS(v+r.v));}
    ModInt operator-(const ModInt &r) const {return make(normS(v+MD-r.v));}
    ModInt operator*(const ModInt &r) const {return make((ull)v*r.v%MD);}
    ModInt& operator+=(const ModInt &r) {return *this=*this+r;}
    ModInt& operator-=(const ModInt &r) {return *this=*this-r;}
    ModInt& operator*=(const ModInt &r) {return *this=*this*r;}
    static ModInt inv(const ModInt &x) {
        return pow(ModInt(x), MD-2);
    }
};

/**
 * 行列ライブラリ
 */
template<class D>
struct Matrix {
    vector<vector<D>> d;
    int N, M;
    Matrix(int N, int M) : N(N), M(M) {
        d.resize(N);
        for (int i = 0; i < N; i++) {
            d[i] = vector<D>(M);
        }
    }

    vector<D>& operator[](int p) {return d[p];}
    const vector<D>& operator[](int p) const {return d[p];}

    const Matrix operator+(const Matrix &right) const {
        assert(right.N == N && right.M == M);
        Matrix res(N, M);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                res[i][j] = d[i][j]+right[i][j];
            }
        }
        return res;
    }

    const Matrix operator*(const Matrix &right) const {
        assert(M == right.N);
        Matrix res(N, right.M), r(right.M, right.N);
        for (int i = 0; i < right.M; i++) {
            for (int j = 0; j < right.N; j++) {
                r[i][j] = right[j][i]; 
            }
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < right.M; j++) {
                for (int k = 0; k < M; k++) {
                    res[i][j] += d[i][k]*r[j][k];
                }
            }
        }
        return res;
    }

    const Matrix operator*(const D &x) const {
        Matrix res(N, M);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                res[i][j] = d[i][j]*x;
            }
        }
        return res;
    }
};

template<class T>
Matrix<T> pow(Matrix<T> x, ll p) {
    assert(x.N == x.M);
    int N = x.N;
    Matrix<T> res(N, N), buf = x;
    for (int i = 0; i < N; i++) res[i][i] = T(1);
    while(p != 0) {
        if (p % 2) {
            res = res*buf;
        }
        p /= 2;
        buf = buf*buf;
    }
    return res;
}


using Mint = ModInt<TEN(9)+7>;
using Mat = Matrix<Mint>;

int main() {
    int n; ll c;
    cin >> n >> c;
    
    Mint ans = 0;
    Mat v(n, 1);
    for (int i = 0; i < n; i++) {
        int d;
        cin >> d;
        v[i][0] = d;
        ans -= pow(v[i][0], c);
    }

    Mat m(n, n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j <= i; j++) {
            m[i][j] = v[i][0];
        }
    }

    v = pow(m, c-1)*v;
    
    for (int i = 0; i < n; i++) {
        ans += v[i][0];
    }
    
    /*    for (int i = 0; i < n; i++) {
        cout << v[i][0].v << ", ";
    }
    cout << endl;*/

    cout << ans.v << endl;
    
    return 0;
}

0