結果

問題 No.1062 素敵なスコア
ユーザー chocoruskchocorusk
提出日時 2020-05-23 02:31:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 4,554 bytes
コンパイル時間 1,883 ms
コンパイル使用メモリ 139,692 KB
実行使用メモリ 10,280 KB
最終ジャッジ日時 2024-04-16 00:16:31
合計ジャッジ時間 3,107 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 15 ms
5,632 KB
testcase_19 AC 27 ms
7,040 KB
testcase_20 AC 19 ms
6,320 KB
testcase_21 AC 19 ms
6,020 KB
testcase_22 AC 25 ms
7,708 KB
testcase_23 AC 19 ms
6,092 KB
testcase_24 AC 15 ms
5,888 KB
testcase_25 AC 16 ms
6,528 KB
testcase_26 AC 19 ms
5,816 KB
testcase_27 AC 7 ms
5,376 KB
testcase_28 AC 28 ms
7,396 KB
testcase_29 AC 12 ms
5,760 KB
testcase_30 AC 29 ms
7,488 KB
testcase_31 AC 43 ms
10,280 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 4 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=998244353;
ll powmod(ll a, ll k){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=MOD;
        }
        ap=ap*ap;
        ap%=MOD;
        k>>=1;
    }
    return ans;
}
ll inv(ll a){
    return powmod(a, MOD-2);
}
ll f[200020], invf[200020];
void fac(int n){
    f[0]=1;
    for(ll i=1; i<=n; i++) f[i]=f[i-1]*i%MOD;
    invf[n]=inv(f[n]);
    for(ll i=n-1; i>=0; i--) invf[i]=invf[i+1]*(i+1)%MOD;
}
ll comb(int x, int y){
    if(!(0<=y && y<=x)) return 0;
    return f[x]*invf[y]%MOD*invf[x-y]%MOD;
}
template< int mod >
struct NumberTheoreticTransform {

  vector< int > rev, rts;
  int base, max_base, root;

  NumberTheoreticTransform() : base(1), rev{0, 1}, rts{0, 1} {
    assert(mod >= 3 && mod % 2 == 1);
    auto tmp = mod - 1;
    max_base = 0;
    while(tmp % 2 == 0) tmp >>= 1, max_base++;
    root = 2;
    while(mod_pow(root, (mod - 1) >> 1) == 1) ++root;
    assert(mod_pow(root, mod - 1) == 1);
    root = mod_pow(root, (mod - 1) >> max_base);
  }

  inline int mod_pow(int x, int n) {
    int ret = 1;
    while(n > 0) {
      if(n & 1) ret = mul(ret, x);
      x = mul(x, x);
      n >>= 1;
    }
    return ret;
  }

  inline int inverse(int x) {
    return mod_pow(x, mod - 2);
  }

  inline unsigned add(unsigned x, unsigned y) {
    x += y;
    if(x >= mod) x -= mod;
    return x;
  }

  inline unsigned mul(unsigned a, unsigned b) {
    return 1ull * a * b % (unsigned long long) mod;
  }

  void ensure_base(int nbase) {
    if(nbase <= base) return;
    rev.resize(1 << nbase);
    rts.resize(1 << nbase);
    for(int i = 0; i < (1 << nbase); i++) {
      rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
    }
    assert(nbase <= max_base);
    while(base < nbase) {
      int z = mod_pow(root, 1 << (max_base - 1 - base));
      for(int i = 1 << (base - 1); i < (1 << base); i++) {
        rts[i << 1] = rts[i];
        rts[(i << 1) + 1] = mul(rts[i], z);
      }
      ++base;
    }
  }


  void ntt(vector< int > &a) {
    const int n = (int) a.size();
    assert((n & (n - 1)) == 0);
    int zeros = __builtin_ctz(n);
    ensure_base(zeros);
    int shift = base - zeros;
    for(int i = 0; i < n; i++) {
      if(i < (rev[i] >> shift)) {
        swap(a[i], a[rev[i] >> shift]);
      }
    }
    for(int k = 1; k < n; k <<= 1) {
      for(int i = 0; i < n; i += 2 * k) {
        for(int j = 0; j < k; j++) {
          int z = mul(a[i + j + k], rts[j + k]);
          a[i + j + k] = add(a[i + j], mod - z);
          a[i + j] = add(a[i + j], z);
        }
      }
    }
  }


  vector< int > multiply(vector< int > a, vector< int > b) {
    int need = a.size() + b.size() - 1;
    int nbase = 1;
    while((1 << nbase) < need) nbase++;
    ensure_base(nbase);
    int sz = 1 << nbase;
    a.resize(sz, 0);
    b.resize(sz, 0);
    ntt(a);
    ntt(b);
    int inv_sz = inverse(sz);
    for(int i = 0; i < sz; i++) {
      a[i] = mul(a[i], mul(b[i], inv_sz));
    }
    reverse(a.begin() + 1, a.end());
    ntt(a);
    a.resize(need);
    return a;
  }
};

int main()
{
	int n, a, b;
	cin>>n>>a>>b;
	fac(n);
	if(a>b) swap(a, b);
	if(a==b){
		cout<<f[n]*n%MOD<<endl;
		return 0;
	}
	int x=a, z=n-b, y=n-x-z;
	ll ans=f[n-1]*x%MOD*x%MOD;
	(ans+=f[n-1]*z%MOD*z)%=MOD;
	(ans+=f[n-1]*y%MOD*y)%=MOD;
	ll u=f[x]*f[y]%MOD*f[z]%MOD;
	NumberTheoreticTransform<MOD> ntt;
	vector<int> v1(x), v2(x);
	for(int i=0; i<x; i++){
		v1[i]=invf[i]*((z-i-1<0)?0:invf[z-i-1])%MOD;
	}
	for(int i=0; i<x; i++){
		v2[i]=f[x-i-1]*f[y+i]%MOD*f[y+z+i]%MOD*invf[i]%MOD;
	}
	auto w=ntt.multiply(v1, v2);
	for(int i=0; i<x; i++){
		(ans+=u*w[i]%MOD*invf[y]%MOD*invf[x-i-1]%MOD*invf[y+1+i])%=MOD;
	}
	v1.resize(z), v2.resize(z);
	for(int i=0; i<z; i++){
		v1[i]=invf[i]*((x-i-1<0)?0:invf[x-i-1])%MOD;
	}
	for(int i=0; i<z; i++){
		v2[i]=f[z-i-1]*f[y+i]%MOD*f[y+x+i]%MOD*invf[i]%MOD;
	}
	w=ntt.multiply(v1, v2);
	for(int i=0; i<z; i++){
		(ans+=u*w[i]%MOD*invf[y]%MOD*invf[z-i-1]%MOD*invf[y+1+i])%=MOD;
	}
	cout<<ans<<endl;
    return 0;
}
0