結果

問題 No.2381 Gift Exchange Party
ユーザー random contestantrandom contestant
提出日時 2023-07-15 15:35:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 1,506 bytes
コンパイル時間 4,806 ms
コンパイル使用メモリ 263,792 KB
実行使用メモリ 6,280 KB
最終ジャッジ日時 2023-10-17 01:57:39
合計ジャッジ時間 7,968 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
6,280 KB
testcase_01 AC 67 ms
6,280 KB
testcase_02 AC 67 ms
6,280 KB
testcase_03 AC 67 ms
6,280 KB
testcase_04 AC 67 ms
6,280 KB
testcase_05 AC 68 ms
6,280 KB
testcase_06 AC 67 ms
6,280 KB
testcase_07 AC 67 ms
6,280 KB
testcase_08 AC 68 ms
6,280 KB
testcase_09 AC 67 ms
6,280 KB
testcase_10 AC 67 ms
6,280 KB
testcase_11 AC 67 ms
6,280 KB
testcase_12 AC 67 ms
6,280 KB
testcase_13 AC 68 ms
6,280 KB
testcase_14 AC 67 ms
6,280 KB
testcase_15 AC 67 ms
6,280 KB
testcase_16 AC 67 ms
6,280 KB
testcase_17 AC 67 ms
6,280 KB
testcase_18 AC 67 ms
6,280 KB
testcase_19 AC 66 ms
6,280 KB
testcase_20 AC 108 ms
6,280 KB
testcase_21 AC 67 ms
6,280 KB
testcase_22 AC 109 ms
6,280 KB
testcase_23 AC 109 ms
6,280 KB
testcase_24 AC 67 ms
6,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
using ll = long long;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
using vl = vector<ll>;
using vvl = vector<vl>;
using P = pair<ll,ll>;
#define pb push_back
#define int long long
#define double long double
#define INF (ll) 3e18
// Ctrl + Shift + B コンパイル
// Ctrl + C 中断
// ./m 実行


// コンビネーション計算 nCr v3 570ms
constexpr int mod = 998244353;
using mint = modint998244353;
vl kaijo(200010);
vl kaijo_inv(200010);
void kaijo_calc(){
  kaijo[0] = kaijo[1] = 1;
  for(int i = 2; i <= 200000; ++i){
    kaijo[i] = kaijo[i-1] * i;
    kaijo[i] %= mod;
  }
  for(int i = 0; i <= 200000; ++i){
    kaijo_inv[i] = inv_mod(kaijo[i], mod);
  }
}
int nCr(int n, int r){
  if (kaijo[0] == 0) kaijo_calc();
  mint res = 1;
  res *= kaijo[n];
  res *= kaijo_inv[n-r];
  res *= kaijo_inv[r];
  return res.val();
}

signed main(){
  int n; cin >> n;
  int p; cin >> p; // pは素数
  using mint = modint998244353;
  mint ans = 0;
  for(int k = 0; k <= n/p; k++){
    // 長さpのサイクルがk個あり、残りは自己ループとなる。
    mint now = 1;
    now *= nCr(n, k*p); // サイクルとするk*p個の取り出し方
    now *= kaijo[k*p]; // k+pの並べ替え方
    now /= mint(p).pow(k); // サイクルの並べ替えは(P-1)!なのでPで割る
    now /= kaijo[k];
    ans += now;
  }
  ans = kaijo[n] - ans;
  cout << ans.val() << endl;
}
0