結果
| 問題 | No.444 旨味の相乗効果 |
| コンテスト | |
| ユーザー |
はむこ
|
| 提出日時 | 2016-11-25 18:22:15 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 12,421 bytes |
| 記録 | |
| コンパイル時間 | 1,631 ms |
| コンパイル使用メモリ | 178,216 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-11-27 11:20:17 |
| 合計ジャッジ時間 | 2,302 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 WA * 1 |
| other | AC * 12 WA * 11 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }
using ll = long long; using vll = vector<ll>; using vvll = vector<vll>;
using ld = long double; using vld = vector<ld>;
using vi = vector<int>; using vvi = vector<vi>;
vll conv(vi& v) { vll r(v.size()); rep(i, v.size()) r[i] = v[i]; return r; }
using P = pair<ll, ll>;
template <typename T, typename U> ostream &operator<<(ostream &o, const pair<T, U> &v) { o << "(" << v.first << ", " << v.second << ")"; return o; }
template<size_t...> struct seq{}; template<size_t N, size_t... Is> struct gen_seq : gen_seq<N-1, N-1, Is...>{}; template<size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{};
template<class Ch, class Tr, class Tuple, size_t... Is>
void print_tuple(basic_ostream<Ch,Tr>& os, Tuple const& t, seq<Is...>){ using s = int[]; (void)s{0, (void(os << (Is == 0? "" : ", ") << get<Is>(t)), 0)...}; }
template<class Ch, class Tr, class... Args>
auto operator<<(basic_ostream<Ch, Tr>& os, tuple<Args...> const& t) -> basic_ostream<Ch, Tr>& { os << "("; print_tuple(os, t, gen_seq<sizeof...(Args)>()); return os << ")"; }
ostream &operator<<(ostream &o, const vvll &v) { rep(i, v.size()) { rep(j, v[i].size()) o << v[i][j] << " "; cout << endl; } return o; }
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { o << '['; rep(i, v.size()) o << v[i] << (i != v.size()-1 ? ", " : ""); o << "]"; return o; }
template <typename T> ostream &operator<<(ostream &o, const set<T> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; }
template <typename T, typename U> ostream &operator<<(ostream &o, const map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it << (next(it) != m.end() ? ", " : ""); o << "]"; return o; }
template <typename T, typename U> ostream &operator<<(ostream &o, const unordered_map<T, U> &m) { o << '['; for (auto it = m.begin(); it != m.end(); it++) o << *it; o << "]"; return o; }
string bits_to_string(ll mask, ll n) { string s; rep(i, n) s += '0' + !!(mask & (1ll << i)); return s; }
#define ldout fixed << setprecision(40)
template<int MOD>
struct ModInt {
static const int Mod = MOD;
unsigned x;
ModInt() : x(0) {}
ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt inverse() const {
signed a = x, b = MOD, u = 1, v = 0;
while(b) {
signed t = a / b;
a -= t * b; std::swap(a, b);
u -= t * v; std::swap(u, v);
}
if(u < 0) u += Mod;
ModInt res; res.x = (unsigned)u;
return res;
}
bool operator==(ModInt that) const { return x == that.x; }
bool operator!=(ModInt that) const { return x != that.x; }
ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
ModInt<MOD> r = 1;
while(k) {
if(k & 1) r *= a;
a *= a;
k >>= 1;
}
return r;
}
typedef ModInt<1000000007> mint;
typedef vector<mint> vmint;
struct RandomModInt {
default_random_engine re;
uniform_int_distribution<int> dist;
#ifndef _DEBUG
RandomModInt() : re(random_device{}()), dist(1, mint::Mod - 1) { }
#else
RandomModInt() : re(), dist(1, mint::Mod - 1) { }
#endif
mint operator()() {
mint r;
r.x = dist(re);
return r;
}
} randomModInt;
void randomModIntVector(vector<mint> &v) {
int n = (int)v.size();
for(int i = 0; i < n; ++ i)
v[i] = randomModInt();
}
ostream &operator<<(ostream &o, const mint v) { o << v.x; return o; }
// GF(mo)列sから、それを生成する最小線形漸化式Cを復元する
//
// 入力: 漸化式が生成したGF(mo)列s
// 出力: 漸化式係数C
// 漸化式
// C_0 s_{n} + C_1 s_{n-1} + ... + C_{L} s{n-L} = 0
// がsを生成した時、Cを求める。
//
// O(n^2)
//
// 例:
// s = [1, 2, 4, 8] -> C = [1, 1000000005(-2)] (s[1] - 2 * s[0] = 0)
// s = [1, 1, 1, 1] -> C = [1, 1000000006(-1)] (s[1] - s[0] = 0)
int berlekampMassey(const vector<mint> &s, vector<mint> &C) {
int N = (int)s.size();
C.assign(N + 1, mint());
vector<mint> B(N + 1, mint());
C[0] = B[0] = 1;
int degB = 0;
vector<mint> T;
int L = 0, m = 1;
mint b = 1;
for(int n = 0; n < N; ++ n) {
mint d = s[n];
for(int i = 1; i <= L; ++ i)
d += C[i] * s[n - i];
if(d == mint()) {
++ m;
} else {
if(2 * L <= n)
T.assign(C.begin(), C.begin() + (L + 1));
mint coeff = -d * b.inverse();
for(int i = -1; i <= degB; ++ i)
C[m + i] += coeff * B[i];
if(2 * L <= n) {
L = n + 1 - L;
B.swap(T);
degB = (int)B.size() - 1;
b = d;
m = 1;
} else {
++ m;
}
}
}
C.resize(L + 1);
return L;
}
// GF(mo)列aから、それを生成する最小線形漸化式\phiを復元する
// berlekampMasseyとの違いは、係数の順序が違うのと安全用のassertチェックがあること。
//
// 入力: 漸化式が生成したGF(mo)列a
// 出力: 漸化式係数\phi
// 漸化式
// \phi_0 a_0 + \phi_1 a_1 + ... + \phi_L a_L = 0
// がaを生成した時、\phiを求める。
//
// O(n^2)
//
// 例:
// s = [1, 2, 4, 8] -> C = [1, 1000000005(-2)] (s[1] - 2 * s[0] = 0)
// s = [1, 1, 1, 1] -> C = [1, 1000000006(-1)] (s[1] - s[0] = 0)
void computeMinimumPolynomialForLinearlyRecurrentSequence(const vector<mint> &a, vector<mint> &phi) {
int n2 = (int)a.size();
assert(n2 % 2 == 0);
int L = berlekampMassey(a, phi);
reverse(phi.begin(), phi.begin() + (L + 1));
}
// 第K項をinitValuesの線形結合coeffsで表す。
void linearlyRecurrentSequenceCoeffs(long long K, const vector<mint> &initValues, const vector<mint> &annPoly, vector<mint> &coeffs, int& d) {
d = (int)annPoly.size() - 1;
assert(d >= 0);
assert(annPoly[d].get() == 1);
assert(d <= (int)initValues.size());
coeffs = vector<mint>(d);
vector<mint> square;
coeffs[0] = 1;
int l = 0;
while((K >> l) > 1) ++ l;
for(; l >= 0; -- l) {
square.assign(d * 2 - 1, mint());
for(int i = 0; i < d; ++ i)
for(int j = 0; j < d; ++ j)
square[i + j] += coeffs[i] * coeffs[j];
for(int i = d * 2 - 2; i >= d; -- i) {
mint c = square[i];
if(c.x == 0) continue;
for(int j = 0; j < d; ++ j)
square[i - d + j] -= c * annPoly[j];
}
for(int i = 0; i < d; ++ i)
coeffs[i] = square[i];
if(K >> l & 1) {
mint lc = coeffs[d - 1];
for(int i = d - 1; i >= 1; -- i)
coeffs[i] = coeffs[i - 1] - lc * annPoly[i];
coeffs[0] = mint() - lc * annPoly[0];
}
}
// cout << coeffs << " " << coeffs.size() << endl;
}
// 数列aは、annPolyがinitValuesから生成する数列である。
// この時、a[K]を求める。
//
// また、副産物として、a[K]をinitVectorの線形結合として表す係数coeffが得られる
// a[K] = coeff[0] * initValues[0] + coeff[1] * initValues[1] + ... + coeff[d-1] * initValues[d-1]
//
// O(n^2 log K)
mint linearlyRecurrentSequenceValue(long long K, const vector<mint> &initValues, const vector<mint> &annPoly) {
assert(K >= 0);
if(K < (int)initValues.size())
return initValues[(int)K];
vector<mint> coeffs;
int d;
linearlyRecurrentSequenceCoeffs(K, initValues, annPoly, coeffs, d);
assert(d >= 0);
assert(annPoly[d].get() == 1);
assert(d <= (int)initValues.size());
if(d == 0)
return mint();
mint res;
for(int i = 0; i < d; ++ i)
res += coeffs[i] * initValues[i];
return res;
}
mint linearlyRecurrentSequenceValue(long long K, const pair<vector<mint>, vector<mint> > &seqPair) {
return linearlyRecurrentSequenceValue(K, seqPair.first, seqPair.second);
}
class matrixData {
public:
int n;
vmint data;
vmint phi;
matrixData(int n_arg, vmint &data_arg) { n = n_arg; data = data_arg; }
void init(void) {
computeMinimumPolynomialUsingBlackBoxLinearAlgebra();
}
int size(void) { return n; }
// vec_out is allocated in THIS function.
// u, v: random vector
// dp[i] = u^t A^i v
virtual void productMatrixByVector(vmint& vec_out, vmint& vec_in) = 0;
// Cayley-Hamilton
// O(n M(n)), M(n) is computation time of "matrix by vector"
//
// I never care about "unlucky" situations because I'm enough lucky man.
void computeMinimumPolynomialUsingBlackBoxLinearAlgebra(void) {
vector<mint> dp(n * 2), u(n), v(n);
randomModIntVector(u); randomModIntVector(v);
vector<mint> Aiv = v; // i = 0
// enumerate 2n dp[i]
vector<mint> Aiv_next;
rep(i, n * 2) {
rep(j, n) dp[i] += u[j] * Aiv[j];
productMatrixByVector(Aiv_next, Aiv);
Aiv = Aiv_next;
}
computeMinimumPolynomialForLinearlyRecurrentSequence(dp, phi);
/*
cout << dp << endl;
cout << phi << endl;
*/
}
// vec_out is allocated in THIS function.
void computeMatrixPowerByVector(vmint &res_out, const vmint &v_in, int k) {
res_out.assign(n, mint());
vmint vec = v_in;
vmint vec_next;
vector<mint> coeffs;
int d;
linearlyRecurrentSequenceCoeffs(k, v_in, phi, coeffs, d);
rep(i, d) {
rep(j, n) res_out[j] += coeffs[i] * vec[j];
productMatrixByVector(vec_next, vec);
vec = vec_next;
}
}
virtual ~matrixData() {}
};
class myMatrixData : public matrixData {
public:
myMatrixData(int n_arg, vmint &data_arg) : matrixData(n_arg, data_arg) {}
virtual void productMatrixByVector(vmint& vec_out, vmint& vec_in) {
vec_out.resize(n);
vec_out[0] = vec_in[0] * data[0];
rep(i, n-1)
vec_out[i+1] = vec_out[i] + vec_in[i+1] * data[i+1];
}
virtual ~myMatrixData() {}
};
int main() {
/*
{
vector<mint> s = {1, 2, 4, 8}, C;
berlekampMassey(s, C);
cout << s << endl;
cout << C << endl;
}
{
vector<mint> s = {1, 1, 1, 1, 1, 1}, C;
berlekampMassey(s, C);
cout << s << endl;
cout << C << endl;
}
*/
/*
{
vector<mint> a = {100, 1, 0, 0, 0, 0}, phi;
computeMinimumPolynomialForLinearlyRecurrentSequence(a, phi);
cout << a << endl;
cout << phi << endl;
}
*/
// BLAを介して解く
int n; long long c;
cin >> n >> c;
vmint data; rep(i, n) { int tmp; cin >> tmp; data.push_back(tmp); }
// cout << data << endl;
// cout << n << endl;
// myMatrixData* m = new myMatrixData(n, data);
myMatrixData m = myMatrixData(n, data);
m.init();
vmint res_out;
vmint v_in(n, 1);
m.computeMatrixPowerByVector(res_out, v_in, c);
vmint in = {1, 2, 3};
vmint out;
m.productMatrixByVector(out, in);
// cout << out << endl;
// m->computeMatrixPowerByVector(res_out, v_in, c);
mint ans = res_out[n-1];
rep(i, n)
ans -= mint(data[i]) ^ c;
cout << ans << endl;
return 0;
}
はむこ