結果

問題 No.3160 Party Game
ユーザー butsurizuki
提出日時 2025-05-23 17:34:24
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,455 bytes
コンパイル時間 3,234 ms
コンパイル使用メモリ 276,132 KB
実行使用メモリ 36,424 KB
最終ジャッジ日時 2025-05-27 21:56:44
合計ジャッジ時間 4,719 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

// Counting Template
#include<bits/stdc++.h>

using namespace std;

#define mod 998244353
#define FACSIZE 2097152

long long power(long long a,long long b){
  long long x=1,y=a;
  while(b>0){
    if(b&1ll){
      x=(x*y)%mod;
    }
    y=(y*y)%mod;
    b>>=1;
  }
  return x%mod;
}

long long modular_inverse(long long n){
  return power(n,mod-2);
}

long long factorial[FACSIZE];
long long invfact[FACSIZE];

void cfact(){
  long long i;
  factorial[0]=1;
  factorial[1]=1;
  for(i=2;i<FACSIZE;i++){
    factorial[i]=factorial[i-1]*i;
    factorial[i]%=mod;
  }
  invfact[FACSIZE-1]=modular_inverse(factorial[FACSIZE-1]);
  for(i=FACSIZE-2;i>=0;i--){
    invfact[i]=invfact[i+1]*(i+1);
    invfact[i]%=mod;
  }
}

long long calcnCr(long long n,long long k){
  if(k<0 || n<k){return 0;}
  return (factorial[n]*((invfact[k]*invfact[n-k])%mod))%mod;
}

long long solve(){
  // write your code here, and return the answer
  long long n,m;
  cin >> n >> m;
  if(n==1){
    long long v;
    v=(m-1); v%=mod;
    v*=modular_inverse(2); v%=mod;
    return (v%mod);
  }
  long long p=0,q=calcnCr(m+n,n);
  q+=(mod-n); q%=mod;
  for(long long l=1;l<=m;l++){
    if(m-n*l<0){break;}
    long long del=calcnCr(m+n-n*l,n);
    p+=del;
    p%=mod;
  }
  return (p*(modular_inverse(q)))%mod;
}

int main(){
  cfact();
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t=1;
  // cin >> t;
  while(t>0){
    t--;
    cout << solve()%mod << "\n";
  }
  return 0;
}
0