結果
問題 | No.444 旨味の相乗効果 |
ユーザー |
![]() |
提出日時 | 2016-11-09 00:21:00 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 111 ms / 2,500 ms |
コード長 | 3,554 bytes |
コンパイル時間 | 1,001 ms |
コンパイル使用メモリ | 81,380 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-24 20:40:48 |
合計ジャッジ時間 | 2,267 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 23 |
ソースコード
#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;}