結果

問題 No.1035 Color Box
ユーザー monkukui2monkukui2
提出日時 2020-04-24 21:42:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 1,341 bytes
コンパイル時間 557 ms
コンパイル使用メモリ 67,028 KB
実行使用メモリ 27,128 KB
最終ジャッジ日時 2024-04-23 03:05:49
合計ジャッジ時間 3,097 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
26,996 KB
testcase_01 AC 35 ms
27,060 KB
testcase_02 AC 36 ms
27,044 KB
testcase_03 AC 41 ms
26,948 KB
testcase_04 AC 37 ms
27,080 KB
testcase_05 AC 37 ms
27,036 KB
testcase_06 AC 35 ms
27,000 KB
testcase_07 AC 39 ms
27,120 KB
testcase_08 AC 41 ms
26,912 KB
testcase_09 AC 38 ms
26,996 KB
testcase_10 AC 35 ms
26,980 KB
testcase_11 AC 38 ms
27,076 KB
testcase_12 AC 37 ms
26,968 KB
testcase_13 AC 46 ms
27,032 KB
testcase_14 AC 43 ms
27,028 KB
testcase_15 AC 43 ms
27,048 KB
testcase_16 AC 39 ms
27,008 KB
testcase_17 AC 37 ms
27,040 KB
testcase_18 AC 37 ms
27,012 KB
testcase_19 AC 49 ms
27,124 KB
testcase_20 AC 39 ms
26,992 KB
testcase_21 AC 39 ms
26,928 KB
testcase_22 AC 38 ms
26,940 KB
testcase_23 AC 37 ms
26,884 KB
testcase_24 AC 36 ms
27,052 KB
testcase_25 AC 40 ms
26,912 KB
testcase_26 AC 36 ms
27,000 KB
testcase_27 AC 37 ms
27,012 KB
testcase_28 AC 37 ms
26,816 KB
testcase_29 AC 34 ms
26,948 KB
testcase_30 AC 41 ms
27,108 KB
testcase_31 AC 38 ms
27,044 KB
testcase_32 AC 38 ms
27,128 KB
testcase_33 AC 37 ms
27,104 KB
testcase_34 AC 38 ms
26,944 KB
testcase_35 AC 35 ms
27,004 KB
testcase_36 AC 36 ms
27,076 KB
testcase_37 AC 38 ms
27,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ボール: 区別あり, 箱:区別あり, 入れ方: 1 つ以上
#include <iostream>
using namespace std;
const int MOD = 1000000007;
const long long MAXN = 1001024;

// 繰り返し 2 乗法
long long mod_pow(long long a, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % MOD;
    a = a * a % MOD;
    n >>= 1;
  }
  return res;
}

long long fac[MAXN], finv[MAXN], inv[MAXN];

// 前処理 O(n)
void comb_init(){
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for(long long i = 2; i < MAXN; i++){
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算 O(1)
long long mod_comb(long long n, long long k){
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  
  long long n, k; cin >> n >> k;
  long long ans = 0;
  comb_init();
  for(int i = 0; i <= k; i++) {
    // i 個の箱を選んで, 絶対ボールを入れないことにする
    // 残りの箱に自由に入れる
    int rest = k - i;
    long long add = mod_comb(k, i) * mod_pow(rest, n) % MOD;
    
    if(i % 2 == 0) ans = (ans + add      ) % MOD;
    else           ans = (ans - add + MOD) % MOD;
  }

  cout << ans << endl;
  return 0;
}
0