結果

問題 No.997 Jumping Kangaroo
ユーザー MayimgMayimg
提出日時 2020-02-22 20:53:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 15,049 bytes
コンパイル時間 2,926 ms
コンパイル使用メモリ 213,904 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-09 20:49:02
合計ジャッジ時間 3,471 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
template<typename T> class Modular {
private:
  long long value;
  constexpr static int MOD() { return static_cast<int>(T::value); }
public:
  constexpr Modular() : value() {};
  constexpr Modular(const Modular& other) : value(other.value) {}
  template <typename U> constexpr Modular(const U& x) { value = normalize(x); }

  template <typename U> static long long normalize(const U& x) {
    long long v;
    if (-MOD() <= x && x < MOD()) v = static_cast<long long>(x);
    else v = static_cast<long long>(x % MOD());
    if (v < 0) v += MOD();
    return v;
  }

  constexpr static long long inverse(long long x) {
    x = (x % MOD() + MOD()) % MOD();
    long long y = MOD(), u = 1, v = 0;
    while(y) {
      long long t = x / y;
      x -= t * y; swap(x, y);
      u -= t * v; swap(u, v);
    }
    return (u % MOD() + MOD()) % MOD();
  }
  
  static long long mul(const long long& a, const long long& b) {
    long long res;
    #ifdef _WIN32
    unsigned long long x = a * b;
    unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m;
    asm(
      "divl %4; \n\t"
      : "=a" (d), "=d" (m)
      : "d" (xh), "a" (xl), "r" (MOD)
    );
    res = m;
    #else
    res = a * b % MOD();
    #endif
    return res;
  }

  explicit operator long long() const noexcept { return value;}
  template <typename U> explicit operator U() const noexcept { return static_cast<U>(value); }

  constexpr Modular& operator=(const Modular& other) & noexcept { value = other.value; return *this; }
  template <typename U> constexpr Modular& operator=(const U& other) & noexcept { return *this = Modular(other); }

  constexpr Modular& operator+=(const Modular& other) noexcept { if ((value += other.value) >= MOD()) value -= MOD(); return *this; }
  template <typename U> constexpr Modular& operator+=(const U& other) noexcept { return *this += Modular(other); }

  constexpr Modular& operator-=(const Modular& other) noexcept { if ((value -= other.value) < 0) value += MOD(); return *this; }
  template <typename U> constexpr Modular& operator-=(const U& other) noexcept { return *this -= Modular(other); }

  constexpr Modular& operator*=(const Modular& other) noexcept { this->value = mul(this->value, other.value); return *this; }
  template <typename U> constexpr Modular& operator*=(const U& other) noexcept {return *this *= Modular(other); }

  constexpr Modular& operator/=(const Modular& other) noexcept { return *this *= Modular(inverse(other.value)); }
  template <typename U> constexpr Modular& operator/=(const U& other) noexcept { return *this *= Modular(inverse(normalize(other))); }

  constexpr Modular& operator++() noexcept {return *this += 1; }
  constexpr Modular operator++(int) noexcept { Modular ret(*this); *this += 1; return ret; }

  constexpr Modular& operator--() noexcept {return *this -= 1; }
  constexpr Modular operator--(int) noexcept { Modular ret(*this); *this += 1; return ret; }

  constexpr Modular operator-() const { return Modular(-value); }

  friend constexpr bool operator==(const Modular& lhs, const Modular<T>& rhs) noexcept { return lhs.value == rhs.value; }
  template <typename U> friend constexpr bool operator==(const Modular<T>& lhs, U rhs) noexcept { return lhs == Modular<T>(rhs); }
  template <typename U> friend constexpr bool operator==(U lhs, const Modular<T>& rhs) noexcept { return Modular<T>(lhs) == rhs; }

  friend constexpr bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return !(lhs == rhs); }
  template <typename U> friend constexpr bool operator!=(const Modular<T>& lhs, U rhs) noexcept { return !(lhs == rhs); }
  template <typename U> friend constexpr bool operator!=(U lhs, const Modular<T> rhs) noexcept { return !(lhs == rhs); }

  friend constexpr bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return lhs.value < rhs.value; }
  template <typename U> friend constexpr bool operator<(const Modular<T> &lhs, U rhs) noexcept { return lhs.value < rhs; }
  template <typename U> friend constexpr bool operator<(U lhs, const Modular<T> &rhs) noexcept { return lhs < rhs.value; }

  friend constexpr bool operator>(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return rhs.value < lhs.value; }
  template <typename U> friend constexpr bool operator>(const Modular<T> &lhs, U rhs) noexcept { return rhs.value < lhs; }
  template <typename U> friend constexpr bool operator>(U lhs, const Modular<T> &rhs) noexcept { return rhs < lhs.value; }

  friend constexpr bool operator<=(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return !(lhs.value > rhs.value); }
  template <typename U> friend constexpr bool operator<=(const Modular<T> &lhs, U rhs) noexcept { return !(lhs.value > rhs); }
  template <typename U> friend constexpr bool operator<=(U lhs, const Modular<T> &rhs) noexcept { return !(lhs < rhs.value); }

  friend constexpr bool operator>=(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return !(lhs.value < rhs.value); }
  template <typename U> friend constexpr bool operator>=(const Modular<T> &lhs, U rhs) noexcept { return !(lhs.value < rhs); }
  template <typename U> friend constexpr bool operator>=(U lhs, const Modular<T> &rhs) noexcept { return !(lhs < rhs.value); }

  friend constexpr Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return Modular<T>(lhs) += rhs; }
  template <typename U> friend constexpr Modular<T> operator+(const Modular<T>& lhs, U rhs) noexcept { return Modular<T>(lhs) += rhs; }
  template <typename U> friend constexpr Modular<T> operator+(U lhs, const Modular<T> &rhs) noexcept { return Modular<T>(lhs) += rhs; }

  friend constexpr Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return Modular<T>(lhs) -= rhs; }
  template <typename U> friend constexpr Modular<T> operator-(const Modular<T>& lhs, U rhs) noexcept { return Modular<T>(lhs) -= rhs; }
  template <typename U> friend constexpr Modular<T> operator-(U lhs, const Modular<T> &rhs) noexcept { return Modular<T>(lhs) -= rhs; }

  friend constexpr Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return Modular<T>(lhs) *= rhs; }
  template <typename U> friend constexpr Modular<T> operator*(const Modular<T>& lhs, U rhs) noexcept { return Modular<T>(lhs) *= rhs; }
  template <typename U> friend constexpr Modular<T> operator*(U lhs, const Modular<T> &rhs) noexcept { return Modular<T>(lhs) *= rhs; }

  friend constexpr Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) noexcept { return Modular<T>(lhs) /= rhs; }
  template <typename U> friend constexpr Modular<T> operator/(const Modular<T>& lhs, U rhs) noexcept { return Modular<T>(lhs) /= rhs; }
  template <typename U> friend constexpr Modular<T> operator/(U lhs, const Modular<T> &rhs) noexcept { return Modular<T>(lhs) /= rhs; }

  friend std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) noexcept { return stream << number.value; }
  friend std::istream& operator>>(std::istream& stream, Modular<T>& number) { long long in; stream >> in; number.value = Modular<T>::normalize(in); return stream; }
  
  constexpr int getmod() const { return MOD(); }
};

template<typename T, typename U> Modular<T> power(const Modular<T>& x, const U& y) {
  assert(y >= 0);
  Modular<T> k = x, result = 1;
  U p = y;
  while (p > 0) {
    if (p & 1) result *= k;
    k *= k;
    p >>= 1;
  }
  return result;
}

template<typename T> class BinaryCoefficients {
private:
  vector<Modular<T>> fact_, inv_, finv_;
  long long MOD = static_cast<long long>(T::value);
public:
  constexpr BinaryCoefficients(int n = 2020200) : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
    for (int i = 2; i < n; i++) {
      fact_[i] = fact_[i - 1] * i;
      inv_[i] = -inv_[MOD % i] * (MOD / i);
      finv_[i] = finv_[i - 1] * inv_[i];
    }
  }
  constexpr Modular<T> comb(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; }
  constexpr Modular<T> fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; }
  constexpr Modular<T> inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; }
  constexpr Modular<T> finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; }
};

constexpr int mod = 1e9 + 7;
//constexpr int mod = 998244353;
using mint = Modular<std::integral_constant<decay<decltype(mod)>::type, mod>>;
using bicoef = BinaryCoefficients<std::integral_constant<decay<decltype(mod)>::type, mod>>;

// struct modValue { static int value; };
// int modValue::value;
// int& mod = modValue::value;
// using mint = Modular<modValue>;
// using bicoef = BinaryCoefficients<modValue>;

template <typename T> class Matrix {
private:
  vector<vector<T>> matrix;
  template<typename U> using ET = enable_if<is_floating_point<U>::value>;
  template<typename U> using EF = enable_if<!is_floating_point<U>::value>;
  template<typename U, typename ET<U>::type* = nullptr> static constexpr bool is_zero (const U& x) { return abs(x) < 1e-8; }
  template<typename U, typename EF<U>::type* = nullptr> static constexpr bool is_zero (const U& x) { return x == U(0); }
public:
  constexpr Matrix () {}
  constexpr Matrix (int h, int w) : matrix(vector<vector<T>>(h, vector<T>(w))) {}
  constexpr Matrix (const vector<vector<T>>& mat) : matrix(mat) {}
  constexpr Matrix (const Matrix<T>& mat) : matrix(mat.matrix) {}

  constexpr size_t height () { return (int) matrix.size(); }
  constexpr size_t width () { assert(!empty()); return (int) matrix[0].size(); }
  constexpr bool empty() { return matrix.empty(); }
  
  vector<T>& operator[] (size_t row) { assert(matrix.size() > row); return matrix[row]; }
  const vector<T>& operator[] (size_t row) const { assert(matrix.size() > row); return matrix[row]; }

  static Matrix identity (int n) {
    Matrix res(n, n);
    for (int i = 0; i < n; ++i) res[i][i] = T(1);
    return res;
  }
  Matrix identity () { assert(height() == width()); return identity(height()); };

  constexpr Matrix<T>& operator= (const Matrix<T>& lhs) & noexcept {
    int h = lhs.matrix.size();
    assert(!lhs.matrix.empty());
    int w = lhs.matrix[0].size();
    matrix = vector<vector<T>>(h, vector<T>(w, T()));
    for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) matrix[i][j] = lhs.matrix[i][j];
    return *this; 
  }
  constexpr Matrix<T>& operator= (const vector<vector<T>>& lhs) & noexcept { return *this = Matrix(lhs); }

  constexpr Matrix<T>& operator+= (const Matrix<T>& lhs) noexcept {
    assert(height() == lhs.matrix.size());
    assert(width() == lhs.matrix[0].sizse());
    for (int i = 0; i < height(); ++i) for (int j = 0; j < width(); ++j) matrix[i][j] += lhs[i][j];
    return (*this);
  }
  constexpr Matrix<T>& operator+= (const vector<vector<T>>& lhs) noexcept { return *this += Matrix(lhs); }

  constexpr Matrix<T>& operator-= (const Matrix<T>& lhs) noexcept {
    assert(height() == lhs.matrix.size());
    assert(width() == lhs.matrix[0].sizse());
    for (int i = 0; i < height(); ++i) for (int j = 0; j < width(); ++j) matrix[i][j] -= lhs[i][j];
    return (*this);
  }
  constexpr Matrix<T>& operator-= (const vector<vector<T>>& lhs) noexcept { return *this -= Matrix(lhs); }

  constexpr Matrix<T>& operator*= (const Matrix<T>& lhs) noexcept {
    Matrix res(height(), lhs[0].size());
    for (int i = 0; i < (int) height(); ++i) for (int j = 0; j < (int) lhs[0].size(); ++j) for (int k = 0; k < (int) width(); k++) {
      res[i][j] += matrix[i][k] * lhs[k][j];
    }
    matrix.swap(res.matrix);
    return (*this);
  }
  constexpr Matrix<T>& operator*= (const vector<vector<T>>& lhs) noexcept { return *this *= Matrix(lhs); }

  friend constexpr bool operator==(const Matrix<T>& lhs, const Matrix<T>& rhs) noexcept { return lhs.matrix == rhs.matrix; }
  friend constexpr bool operator==(const Matrix<T>& lhs, const vector<vector<T>>& rhs) noexcept { return lhs == Matrix(rhs); }
  friend constexpr bool operator==(const vector<vector<T>>& lhs, const Matrix<T>& rhs) noexcept { return Matrix(lhs) == rhs; }

  friend constexpr Matrix<T> operator+(const Matrix<T>& lhs, const Matrix<T>& rhs) noexcept { return Matrix(lhs) += rhs; }
  friend constexpr Matrix<T> operator+(const Matrix<T>& lhs, const vector<vector<T>>& rhs) noexcept { return Matrix(lhs) += rhs; }
  friend constexpr Matrix<T> operator+(const vector<vector<T>>& lhs, const Matrix<T>& rhs) noexcept { return Matrix(lhs) += rhs; }

  friend constexpr Matrix<T> operator-(const Matrix<T>& lhs, const Matrix<T>& rhs) noexcept { return Matrix(lhs) -= rhs; }
  friend constexpr Matrix<T> operator-(const Matrix<T>& lhs, const vector<vector<T>>& rhs) noexcept { return Matrix(lhs) -= rhs; }
  friend constexpr Matrix<T> operator-(const vector<vector<T>>& lhs, const Matrix<T>& rhs) noexcept { return Matrix(lhs) -= rhs; }

  friend constexpr Matrix<T> operator*(const Matrix<T>& lhs, const Matrix<T>& rhs) noexcept { return Matrix(lhs) *= rhs; }
  friend constexpr Matrix<T> operator*(const Matrix<T>& lhs, const vector<vector<T>>& rhs) noexcept { return Matrix(lhs) *= rhs; }
  friend constexpr Matrix<T> operator*(const vector<vector<T>>& lhs, const Matrix<T>& rhs) noexcept { return Matrix(lhs) *= rhs; }

  template <typename U> Matrix<T> power(U n) {
    Matrix x(*this), res = identity(height());
    while (n) {
      if (n & 1) res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  T determinant() {
    Matrix a(*this);
    assert(a.height() == a.width());
    T res = 1;
    for (int i = 0; i < width(); ++i) {
      int pivot = -1;
      for (int j = i; j < width(); ++j) if (!is_zero(a[j][i])) pivot = j;
      if (pivot == -1) return T(0);
      if (i != pivot) {
        res *= -1;
        swap(a[i], a[pivot]);
      }
      res *= a[i][i];
      T div = a[i][i];
      for (int j = 0; j < width(); ++j) a[i][j] /= div;
      for (int j = i + 1; j < width(); ++j) {
        T b = a[j][i];
        for (int k = 0; k < width(); ++k) {
          a[j][k] -= a[i][k] * b;
        }
      }
    }
    return res;
  }
};

signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  long long n, w, k;
  cin >> n >> w >> k;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<mint> dp1(w + 1);
  dp1[0] = 1;
  for (int i = 0; i < w; i++) {
    for (auto j : a) {
      if (i + j <= w) dp1[i + j] += dp1[i];
    }
  }
  vector<mint> dp2(w + w + 1);
  dp2[0] = 1;
  for (int i = 0; i < w + w + 1; i++) {
    for (auto j : a) {
      if (j + i == w) continue;
      else if (j + i <= w + w) dp2[i + j] += dp2[i];
    }
  }
  mint t1 = dp1[w];
  mint t2 = dp2[w + w];
  //cerr << t1 << " " << t2 << endl;
  if (k == 1) {
    cout << t1 << endl;
    return 0;
  }
  Matrix<mint> mat1(2, 2);
  mat1[0][0] = t1; mat1[0][1] = t2;
  mat1[1][0] = 1;  mat1[1][1] = 0;
  Matrix<mint> mat2(2, 1);
  mat2[0][0] = t1;
  mat2[1][0] = 1;
  mat1 = mat1.power(k - 1);
  auto res = mat1 * mat2;
  cout << res[0][0] << endl;
  return 0;
}
0