結果
| 問題 |
No.997 Jumping Kangaroo
|
| コンテスト | |
| ユーザー |
kibuna
|
| 提出日時 | 2020-02-21 22:27:39 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 4,005 bytes |
| コンパイル時間 | 1,693 ms |
| コンパイル使用メモリ | 172,712 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-08 21:58:29 |
| 合計ジャッジ時間 | 2,579 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
#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, w, k;
cin >> n >> w >> k;
vector<lint> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<mint> dp(2 * w + 1, 0);
dp[0] = 1;
for (int i = 0; i < 2 * w; ++i) {
for (int j = 0; j < n; ++j) {
if (i + a[j] <= 2 * w)
dp[i + a[j]] += dp[i];
}
}
mint nn = dp[w];
mint nm = dp[2 * w];
SquareMatrix<2, mint> mat;
mat[0][0] = 0;
mat[0][1] = 1;
mat[1][0] = nm - nn * nn;
mat[1][1] = nn;
mat = mat.pow(k - 1);
cout << mat[1][0] + mat[1][1] * nn << "\n";
return 0;
}
kibuna