結果

問題 No.391 CODING WAR
ユーザー bokusunnybokusunny
提出日時 2022-02-11 23:21:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 3,515 bytes
コンパイル時間 2,131 ms
コンパイル使用メモリ 204,928 KB
実行使用メモリ 15,552 KB
最終ジャッジ日時 2024-06-27 21:43:07
合計ジャッジ時間 3,634 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
15,508 KB
testcase_01 AC 28 ms
15,488 KB
testcase_02 AC 28 ms
15,360 KB
testcase_03 AC 29 ms
15,488 KB
testcase_04 AC 28 ms
15,488 KB
testcase_05 AC 29 ms
15,488 KB
testcase_06 AC 27 ms
15,552 KB
testcase_07 AC 28 ms
15,548 KB
testcase_08 AC 28 ms
15,552 KB
testcase_09 AC 46 ms
15,416 KB
testcase_10 AC 44 ms
15,488 KB
testcase_11 AC 35 ms
15,488 KB
testcase_12 AC 30 ms
15,488 KB
testcase_13 AC 44 ms
15,488 KB
testcase_14 AC 40 ms
15,536 KB
testcase_15 AC 42 ms
15,488 KB
testcase_16 AC 37 ms
15,352 KB
testcase_17 AC 40 ms
15,476 KB
testcase_18 AC 36 ms
15,488 KB
testcase_19 AC 36 ms
15,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define bokusunny ios::sync_with_stdio(false), cin.tie(nullptr);

template <int mod>
struct ModInt {
  int x;

  ModInt() : x(0) {}

  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }

  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }

  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }

  ModInt &operator++() {
    x++;
    if (x == mod) x = 0;
    return *this;
  }

  ModInt &operator--() {
    if (x == 0) x = mod;
    x--;
    return *this;
  }

  ModInt operator++(int) {
    ModInt res = *this;
    ++*this;
    return res;
  }

  ModInt operator--(int) {
    ModInt res = *this;
    --*this;
    return res;
  }

  ModInt operator-() const { return ModInt(-x); }

  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }

  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }

  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }

  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }

  bool operator==(const ModInt &p) const { return x == p.x; }

  bool operator!=(const ModInt &p) const { return x != p.x; }

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while (n > 0) {
      if (n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }

  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }

  static int get_mod() { return mod; }
};

const int MOD = 1e9 + 7;
using mint = ModInt<MOD>;

template <class T>
struct ModComb {
 private:
  vector<T> Fac, Finv, Inv;
  int MAX_N;

 public:
  ModComb(int max_n = 1 << 20) {
    MAX_N = max_n;
    Fac.resize(MAX_N + 1), Finv.resize(MAX_N + 1), Inv.resize(MAX_N + 1);
    Fac[0] = Fac[1] = 1;
    Finv[0] = Finv[1] = 1;
    Inv[1] = 1;

    for (int i = 2; i <= MAX_N; i++) {
      Fac[i] = Fac[i - 1] * i;
      Inv[i] = (mint)0 + MOD - Inv[MOD % i] * (MOD / i);
      Finv[i] = Finv[i - 1] * Inv[i];
    }
  }

  T nCk(int n, int k) {
    assert(n <= MAX_N);
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;

    return Fac[n] * Finv[k] * Finv[n - k];
  }

  T nHr(int n, int r) { return nCk(n + r - 1, r); }

  T nPr(int n, int r) {
    assert(n <= MAX_N);
    assert(0 <= r && r <= n);
    return Fac[n] / Fac[n - r];
  }
};

long long modpow(long long a, long long n, int mod = 1000000007) {
  assert(mod != 0);
  if (mod == 1) return 0LL;
  a %= mod;
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

void solve() {
  long long N;
  int K;
  cin >> N >> K;

  ModComb<mint> Comb;

  mint ans = 0;
  for (int vacant = 0; vacant <= K; vacant++) {
    mint tmp = modpow(K - vacant, N);
    tmp *= Comb.nCk(K, vacant);
    if (vacant & 1) tmp *= -1;
    ans += tmp;
  }

  cout << ans << endl;
}

int main() {
  bokusunny;
  solve();

  return 0;
}
0