結果

問題 No.2917 二重木
ユーザー Kude
提出日時 2024-10-05 00:08:00
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 7 ms / 3,000 ms
コード長 1,555 bytes
コンパイル時間 2,724 ms
コンパイル使用メモリ 272,368 KB
実行使用メモリ 7,444 KB
最終ジャッジ日時 2024-10-05 00:08:05
合計ジャッジ時間 3,780 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint;

mint comb[1010][1010];

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, p;
  cin >> n >> p;
  mint::set_mod(p);
  comb[0][0] = 1;
  rep(n, 1009) rep(k, n + 1) comb[n+1][k] += comb[n][k], comb[n+1][k+1] += comb[n][k];
  if (n == 1) {
    cout << 1 << '\n';
    return 0;
  }
  mint ans = mint(n).pow(n - 2);
  for (int s = 1; s <= n - 1; s++) {
    int rest = n - s;
    // int N = rest + 1;
    mint cs = s == 1 ? mint(1) : mint(s).pow(s - 2);
    // for (int i = 0; i <= N - 2; i++) {
    //   ans += comb[N - 2][i] * mint(N - 1).pow(N - 2 - i) * mint(s).pow(i + 1) * cs * comb[n][s];
    //   // N=n-s+1
    //   // 0<=i<=n-s-1
    //   // (n-s)^(n-s-1-i)*s^(i+1)comb(n-s-1,i)
    // }
    ans += s * mint(n).pow(n - s - 1) * cs * comb[n][s];
  }
  cout << ans.val() << '\n';
}
0