結果

問題 No.1073 無限すごろく
ユーザー KoDKoD
提出日時 2020-06-05 21:48:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 7,174 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 79,436 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-12-17 14:14:36
合計ジャッジ時間 1,697 ms
ジャッジサーバーID
(参考情報)
judge2 / 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,824 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 2 ms
6,816 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 1 ms
6,820 KB
testcase_08 AC 2 ms
6,820 KB
testcase_09 AC 2 ms
6,816 KB
testcase_10 AC 2 ms
6,820 KB
testcase_11 AC 2 ms
6,820 KB
testcase_12 AC 2 ms
6,816 KB
testcase_13 AC 2 ms
6,820 KB
testcase_14 AC 1 ms
6,816 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,816 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,820 KB
testcase_25 AC 2 ms
6,816 KB
testcase_26 AC 2 ms
6,816 KB
testcase_27 AC 2 ms
6,820 KB
testcase_28 AC 2 ms
6,816 KB
testcase_29 AC 1 ms
6,820 KB
testcase_30 AC 2 ms
6,816 KB
testcase_31 AC 2 ms
6,820 KB
testcase_32 AC 2 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>

template <class T, class U>
inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { lhs = rhs; return true; }
  return false;
}

template <class T, class U>
inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { lhs = rhs; return true; }
  return false;
}

struct range {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr itr operator * () const { return i; }
    constexpr bool operator != (iterator x) const { return i != x.i; }
  };
  const iterator l, r;
  constexpr range(itr l_, itr r_): l(l_), r(std::max(l_, r_)) { }
  constexpr iterator begin() const { return l; }
  constexpr iterator end() const { return r; }
};

struct revrange {
  using itr = int64_t;
  struct iterator {
    itr i;
    constexpr iterator(itr i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr itr operator * () const { return i; }
    constexpr bool operator != (iterator x) const { return i != x.i; }
  };
  const iterator l, r;
  constexpr revrange(itr l_, itr r_): l(l_ - 1), r(std::max(l_, r_) - 1) { }
  constexpr iterator begin() const { return r; }
  constexpr iterator end() const { return l; }
};

template <uint32_t Modulus>
class modular {
public:
  using value_type = uint32_t;
  using max_type = uint64_t;

  static constexpr value_type mod = Modulus;
  static_assert(mod >= 2, "invalid mod :: smaller than 2");
  static_assert(mod < (value_type(1) << 31), "invalid mod :: over 2^31");

  template <class T>
  static constexpr value_type normalize(T value_) {
    if (value_ < 0) {
      value_ = -value_;
      value_ %= mod;
      if (value_ == 0) return 0;
      return mod - value_;
    }
    return value_ % mod;
  }

private:
  value_type value;

public:
  constexpr modular(): value(0) { }
  template <class T>
  explicit constexpr modular(T value_): value(normalize(value_)) { }
  template <class T>
  explicit constexpr operator T() { return static_cast<T>(value); }

  constexpr value_type get() const { return value; }
  constexpr modular operator - () const { return modular(mod - value); }
  constexpr modular operator ~ () const { return inverse(); }

  constexpr value_type &extract() { return value; }
  constexpr modular inverse() const { return power(mod - 2); }
  constexpr modular power(max_type exp) const {
    modular res(1), mult(*this);
    while (exp > 0) {
      if (exp & 1) res *= mult;
      mult *= mult;
      exp >>= 1;
    }
    return res;
  }

  constexpr modular operator + (const modular &rhs) const { return modular(*this) += rhs; }
  constexpr modular& operator += (const modular &rhs) { 
    if ((value += rhs.value) >= mod) value -= mod; 
    return *this; 
  }

  constexpr modular operator - (const modular &rhs) const { return modular(*this) -= rhs; }
  constexpr modular& operator -= (const modular &rhs) { 
    if ((value += mod - rhs.value) >= mod) value -= mod; 
    return *this; 
  }

  constexpr modular operator * (const modular &rhs) const { return modular(*this) *= rhs; }
  constexpr modular& operator *= (const modular &rhs) { 
    value = (max_type) value * rhs.value % mod;
    return *this;
  }

  constexpr modular operator / (const modular &rhs) const { return modular(*this) /= rhs; }
  constexpr modular& operator /= (const modular &rhs) { return (*this) *= rhs.inverse(); }

  constexpr bool zero() const { return value == 0; }
  constexpr bool operator == (const modular &rhs) const { return value == rhs.value; }
  constexpr bool operator != (const modular &rhs) const { return value != rhs.value; }
  friend std::ostream& operator << (std::ostream &stream, const modular &rhs) {
    return stream << rhs.value;
  }

};

using m32 = modular<1000000007>;


template <class T, size_t H, size_t W>
class matrix {
public:
  using value_type = typename T::value_type;
  using size_type = size_t;

  static inline auto add = typename T::addition();
  static inline auto mult = typename T::multiplication();

  static constexpr size_type height = H;
  static constexpr size_type width = W;

private:
  std::array<std::array<value_type, W>, H> data;

public:
  matrix(const value_type &value_ = add.identity) { fill(value_); }
  matrix(const std::array<std::array<value_type, W>, H> &data_): data(data_) { }

  std::array<value_type, W> &operator [] (size_type idx) { return data[idx]; }
  const std::array<value_type, W> &operator [] (size_type idx) const { return data[idx]; }

  matrix operator + (const matrix &rhs) const { return matrix(*this) += rhs; }
  matrix &operator += (const matrix &rhs) {
    for (size_type i = 0; i < H; ++i) {
      for (size_type j = 0; j < W; ++j) {
        data[i][j] = add(data[i][j], rhs[i][j]);
      }
    }
    return *this;
  }

  matrix &operator *= (const matrix<T, W, W> &rhs) { return (*this) = (*this) * rhs; }
  template <size_t K>
  matrix<T, H, K> operator * (const matrix<T, W, K> &rhs) const {
    matrix<T, H, K> res;
    for (size_type i = 0; i < H; ++i) {
      for (size_type j = 0; j < K; ++j) {
        for (size_type k = 0; k < W; ++k) {
          res[i][j] = add(res[i][j], mult(data[i][k], rhs[k][j]));
        }
      }
    }
    return res;
  }

  matrix operator * (const value_type &rhs) const { return matrix(*this) *= rhs; }
  matrix &operator *= (const value_type &rhs) {
    for (size_type i = 0; i < H; ++i) {
      for (size_type j = 0; j < W; ++j) {
        data[i][j] = mult(data[i][j], rhs);
      }
    }
    return *this;
  }

  void fill(const value_type &val) {
    for (auto &arr: data) { arr.fill(val); }
  }
  /* typename std::enable_if<H == W, matrix>::type */matrix power(uint64_t exp) const {
    matrix res, use(*this);
    for (size_type i = 0; i < H; ++i) {
      res[i][i] = mult.identity;
    }
    while (exp > 0) {
      if (exp & 1) res *= use;
      use *= use;
      exp >>= 1;
    }
    return res;
  }

};

template <class T>
struct number {
  using value_type = T;
  struct addition {
    value_type identity = value_type(0);
    value_type operator () (const value_type &x, const value_type &y) const {
      return x + y;
    }
  };
  struct multiplication {
    value_type identity = value_type(1);
    value_type operator () (const value_type &x, const value_type &y) const {
      return x * y;
    }
  };
};

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;

int main() {
  u64 N;
  std::cin >> N;
  matrix<number<m32>, 1, 6> start;
  start[0][0] = m32(6).inverse();
  for (auto i: range(0, 6)) {
    start[0][i] = m32(6).inverse();
    for (auto j: range(0, i)) {
      start[0][i] += start[0][j] / m32(6);
    }
  }
  if (N <= 6) {
    std::cout << start[0][N - 1] << '\n';
    return 0;
  }
  matrix<number<m32>, 6, 6> power;
  for (auto i: range(0, 6)) {
    power[i][5] = m32(6).inverse();
  }
  for (auto i: range(0, 5)) {
    power[i + 1][i] = m32(1);
  }
  start *= power.power(N - 6);
  std::cout << start[0][5] << '\n';
  return 0;
}
0