Submission #10068801


Source Code Expand

#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>
#include <functional>
#include <tuple>
#include <type_traits>

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;
}

// [l, r) from l to r
struct range {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { ++i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr l, r;
  constexpr range(int l_, int r_): l(std::min<int>(l_, r_)), r(r_) {}
  constexpr itr begin() const { return l; }
  constexpr itr end() const { return r; }
};

// [l, r) from r to l
struct revrange {
  struct itr {
    int i;
    constexpr itr(int i_): i(i_) { }
    constexpr void operator ++ () { --i; }
    constexpr int operator * () const { return i; }
    constexpr bool operator != (itr x) const { return i != x.i; }
  };
  const itr r, l;
  constexpr revrange(int l_, int r_): r(std::max<int>(l_, r_) - 1), l(l_ - 1) {}
  constexpr itr begin() const { return r; }
  constexpr itr end() const { return l; }
};

template <class T>
inline T scan() {
  T res;
  std::cin >> res;
  return res;
}

template <class T>
class modulo_int {
public:
  static constexpr int mod = T::value;
  static_assert(mod > 0, "mod must be positive");
private:
  long long value;
  constexpr void normalize() {
    value %= mod;
    if (value < 0) value += mod;
  }
public:
  constexpr modulo_int(long long value_ = 0): value(value_) { normalize(); }
  constexpr modulo_int operator - () const { return modulo_int(mod - value); }
  constexpr modulo_int operator ~ () const { return power(mod - 2); }
  constexpr long long operator () () const { return value; }
  constexpr modulo_int operator + (const modulo_int& rhs) const { return modulo_int(*this) += rhs; }
  constexpr modulo_int& operator += (const modulo_int& rhs) {
    if ((value += rhs.value) >= mod) value -= mod;
    return (*this);
  }
  constexpr modulo_int operator - (const modulo_int& rhs) const { return modulo_int(*this) -= rhs; }
  constexpr modulo_int& operator -= (const modulo_int& rhs) {
    if ((value += mod - rhs.value) >= mod) value -= mod;
    return (*this);
  }
  constexpr modulo_int operator * (const modulo_int& rhs) const { return modulo_int(*this) *= rhs; }
  constexpr modulo_int& operator *= (const modulo_int& rhs) {
    (value *= rhs.value) %= mod;
    return (*this);
  }
  constexpr modulo_int operator / (const modulo_int& rhs) const { return modulo_int(*this) /= rhs; }
  constexpr modulo_int& operator /= (const modulo_int& rhs) {
    return (*this) *= ~rhs;
  }
  constexpr modulo_int power (unsigned long long pow) const {
    modulo_int result(1), mult(*this);
    while (pow > 0) {
      if (pow & 1) result *= mult;
      mult *= mult;
      pow >>= 1;
    }
    return result;
  }
  friend std::istream& operator >> (std::istream& stream, modulo_int& lhs) {
    stream >> lhs.value;
    lhs.normalize();
    return stream;
  }
  friend std::ostream& operator << (std::ostream& stream, const modulo_int& rhs) {
    return stream << rhs.value;
  }
};

using modint = modulo_int<std::integral_constant<int, 1000000007>>;
modint dp[301][301][301];

int main() {
  int N, M;
  std::cin >> N >> M;
  dp[0][1][1] = 1;
  for (int i: range(0, M)) {
    for (int j: range(1, N + 1)) {
      for (int k: range(1, N + 1)) {
        dp[i + 1][j][j] += dp[i][j][k] * k;
        dp[i + 1][j][k] += dp[i][j][k] * (j - k);
        if (j + 1 <= N) {
          dp[i + 1][j + 1][k] += dp[i][j][k] * (N - j);
        }
      }
    }
  }
  std::cout << dp[M][N][N] << '\n';
  return 0;
}

Submission Info

Submission Time
Task F - Road of the King
User KoD
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 4023 Byte
Status AC
Exec Time 358 ms
Memory 212608 KB

Judge Result

Set Name sample all
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 3
AC × 16
Set Name Test Cases
sample sample-01.txt, sample-02.txt, sample-03.txt
all sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, sample-01.txt, sample-02.txt, sample-03.txt
Case Name Status Exec Time Memory
01-01.txt AC 1 ms 256 KB
01-02.txt AC 1 ms 256 KB
01-03.txt AC 42 ms 211200 KB
01-04.txt AC 2 ms 896 KB
01-05.txt AC 42 ms 211200 KB
01-06.txt AC 77 ms 211584 KB
01-07.txt AC 339 ms 212480 KB
01-08.txt AC 354 ms 212608 KB
01-09.txt AC 356 ms 212608 KB
01-10.txt AC 358 ms 212608 KB
sample-01.txt AC 2 ms 2304 KB
sample-02.txt AC 135 ms 211840 KB
sample-03.txt AC 161 ms 108032 KB