結果

問題 No.391 CODING WAR
ユーザー masutech16masutech16
提出日時 2021-09-08 00:59:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 7,019 bytes
コンパイル時間 1,884 ms
コンパイル使用メモリ 201,536 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-25 19:48:54
合計ジャッジ時間 3,276 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 33 ms
4,376 KB
testcase_10 AC 31 ms
4,380 KB
testcase_11 AC 22 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 29 ms
4,376 KB
testcase_14 AC 24 ms
4,380 KB
testcase_15 AC 27 ms
4,380 KB
testcase_16 AC 17 ms
4,376 KB
testcase_17 AC 20 ms
4,380 KB
testcase_18 AC 14 ms
4,376 KB
testcase_19 AC 14 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "/workspaces/compro/lib/Assert/Assert.hpp"
#include <cassert>
#include <iostream>
#include <string>
#ifndef MY_ASSERT
#define MY_ASSERT

#ifdef LOCAL

#define ASSERT_MSG(expr, msg)                                                                                          \
  do {                                                                                                                 \
    if (!(expr)) {                                                                                                     \
      std::cerr << msg << std::endl;                                                                                   \
      assert(expr);                                                                                                    \
    }                                                                                                                  \
  } while (0)

#define FAIL ASSERT_MSG(false, "到達しない想定の場所に到達しました")

#else

#define ASSERT_MSG(...) (void)0
#define FAIL (void)0

#endif

#endif
#line 3 "/workspaces/compro/lib/math/modint.hpp"
#include <cstdint>
#line 5 "/workspaces/compro/lib/math/modint.hpp"
#include <vector>
#ifndef MOD_INT
#define MOD_INT

template <std::uint_fast64_t MOD> class ModInt {
  using u64 = std::uint_fast64_t;

public:
  ModInt(const u64 val = 0) { value = (val + MOD) % MOD; }

  ModInt operator+(const ModInt rhs) const { return ModInt(*this) += rhs; }
  ModInt operator-(const ModInt rhs) const { return ModInt(*this) -= rhs; }
  ModInt operator*(const ModInt rhs) const { return ModInt(*this) *= rhs; }
  ModInt operator/(const ModInt rhs) const { return ModInt(*this) /= rhs; }

  ModInt &operator+=(const ModInt rhs) {
    value += rhs.value;
    if (value >= MOD) {
      value -= MOD;
    }
    return *this;
  }

  ModInt &operator-=(const ModInt rhs) {
    if (value < rhs.value) {
      value += MOD;
    }
    value -= rhs.value;
    return *this;
  }

  ModInt &operator*=(const ModInt rhs) {
    value = value * rhs.value % MOD;
    return *this;
  }

  ModInt &operator/=(ModInt rhs) {
    *this *= rhs.inv();
    return *this;
  }

  ModInt &operator++(int n) {
    value++;
    if (value >= MOD) {
      value -= MOD;
    }
    return *this;
  }

  ModInt &operator--(int n) {
    if (value == 0) {
      value += MOD;
    }
    value--;
    return *this;
  }

  ModInt inv() { return ModInt::pow(*this, MOD - 2); }

  static ModInt pow(ModInt base, long long int n) {
    ModInt res = ModInt(1);
    while (n) {
      if (n & 1) {
        res *= base;
      }
      base *= base;
      n /= 2;
    }
    return res;
  }

  static ModInt comb(ModInt n, ModInt r) { return comb(n.value, r.value); }

  static ModInt comb(int n, int r) {
    if (n < r)
      return ModInt(0);
    ModInt res = ModInt(1);
    for (int i = 0; i < r; i++) {
      res *= ModInt(n - i);
    }

    ModInt inv = ModInt(1);
    for (int i = 0; i < r; i++) {
      inv *= ModInt(r - i);
    }
    return res / inv;
  }

  // nC0からnCnまでを計算する。計算量はO(n)
  // @param n     要素数
  // @param vec   結果を格納する配列
  static std::vector<ModInt> combination_table(int n) {
    std::vector<ModInt> vec(n + 1);
    vec[0] = ModInt(1);
    for (int r = 1; r < n + 1; r++) {
      vec[r] = vec[r - 1] * ModInt(n - r + 1) / ModInt(r);
    }
    return vec;
  }

  // n!までを計算する
  // @param n     要素数
  // @param vec   結果を格納する配列
  static std::vector<ModInt> factorial_table(int n) {
    std::vector<ModInt> vec(n + 1);
    vec[0] = ModInt(1);
    for (int r = 1; r <= n; r++) {
      vec[r] = vec[r - 1] * ModInt(r);
    }
  }

  u64 getValue() const { return value; }

private:
  u64 value;

  friend std::ostream &operator<<(std::ostream &out, const ModInt<MOD> &m) {
    out << m.value;
    return out;
  }

  friend std::istream &operator>>(std::istream &in, ModInt &m) {
    uint_fast64_t i;
    in >> i;
    m = ModInt(i);
    return in;
  }
};

#endif
#line 1 "/workspaces/compro/lib/template.hpp"


#line 4 "/workspaces/compro/lib/Assert/Assert.hpp"
#ifndef MY_ASSERT
#define MY_ASSERT

#ifdef LOCAL

#define ASSERT_MSG(expr, msg)                                                                                          \
  do {                                                                                                                 \
    if (!(expr)) {                                                                                                     \
      std::cerr << msg << std::endl;                                                                                   \
      assert(expr);                                                                                                    \
    }                                                                                                                  \
  } while (0)

#define FAIL ASSERT_MSG(false, "到達しない想定の場所に到達しました")

#else

#define ASSERT_MSG(...) (void)0
#define FAIL (void)0

#endif

#endif
#line 3 "/workspaces/compro/lib/io/vector.hpp"

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T> std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 5 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define RFOR(i, m, n) for (int i = (n - 1); i >= (m); i--)
#define ALL(v) std::begin(v), std::end(v)
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

struct P {
  ll x;
  ll y;
};

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}


#line 3 "main.cpp"

constexpr long long MOD = 1000000007;
using mint = ModInt<MOD>;
mint solve(long long N, long long M) {
  auto table = mint::combination_table(M);

  mint ans = mint::pow(M, N);
  ll sign = -1;
  FOR(i, 1, M) {
    mint val = table[i] * mint::pow(M - i, N);
    sign == 1 ? ans += val : ans -= val;
    sign *= -1;
  }
  return ans;
}

// generated by oj-template v4.8.1 (https://github.com/online-judge-tools/template-generator)
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long N, M;
  std::cin >> N >> M;
  auto ans = solve(N, M);
  std::cout << ans << '\n';
  return 0;
}
0