結果

問題 No.2243 Coaching Schedule
ユーザー rabimearabimea
提出日時 2023-03-10 22:24:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 776 ms / 4,000 ms
コード長 2,325 bytes
コンパイル時間 2,418 ms
コンパイル使用メモリ 215,724 KB
実行使用メモリ 16,960 KB
最終ジャッジ日時 2023-10-18 08:06:41
合計ジャッジ時間 8,714 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
6,844 KB
testcase_01 AC 6 ms
6,844 KB
testcase_02 AC 6 ms
6,844 KB
testcase_03 AC 6 ms
6,844 KB
testcase_04 AC 6 ms
6,844 KB
testcase_05 AC 81 ms
16,960 KB
testcase_06 AC 117 ms
16,960 KB
testcase_07 AC 115 ms
16,960 KB
testcase_08 AC 114 ms
16,960 KB
testcase_09 AC 148 ms
16,668 KB
testcase_10 AC 149 ms
16,668 KB
testcase_11 AC 150 ms
16,668 KB
testcase_12 AC 776 ms
16,572 KB
testcase_13 AC 595 ms
16,960 KB
testcase_14 AC 606 ms
16,960 KB
testcase_15 AC 596 ms
16,952 KB
testcase_16 AC 72 ms
16,572 KB
testcase_17 AC 35 ms
9,304 KB
testcase_18 AC 96 ms
15,904 KB
testcase_19 AC 136 ms
16,432 KB
testcase_20 AC 108 ms
12,208 KB
testcase_21 AC 54 ms
11,680 KB
testcase_22 AC 50 ms
11,416 KB
testcase_23 AC 11 ms
7,512 KB
testcase_24 AC 68 ms
12,208 KB
testcase_25 AC 14 ms
7,984 KB
testcase_26 AC 28 ms
9,304 KB
testcase_27 AC 73 ms
12,208 KB
testcase_28 AC 16 ms
7,984 KB
testcase_29 AC 110 ms
15,640 KB
testcase_30 AC 109 ms
16,432 KB
testcase_31 AC 111 ms
16,696 KB
testcase_32 AC 50 ms
11,152 KB
testcase_33 AC 28 ms
9,568 KB
testcase_34 AC 96 ms
15,640 KB
testcase_35 AC 113 ms
16,168 KB
testcase_36 AC 104 ms
16,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vl = vector <ll>;

const ll P = 998244353, G = 3;
ll qpow(ll a, ll b) {
	ll r = 1, t = a;
	for(; b; b /= 2) {
		if(b & 1)
			r = r * t % P;
		t = t * t % P;
	}
	return r;
}
ll INV(ll x) { return qpow(x, P - 2); }
void ntt(vl &a, int op = 1) { // P: 模数, G: 原根
	int n = a.size();
	for (int i = 1, j = 0; i < n - 1; ++i) {
		for (int s = n; j ^= s >>= 1, ~j & s;) {}
		if (i < j) swap(a[i], a[j]);
	}
	for (int i = 1; i < n; i *= 2) {
		ll u = qpow(G, (P - 1) / (i * 2));
		if (op == -1) u = INV(u);
		for (int j = 0; j < n; j += i * 2) {
			ll w = 1;
			for (int k = 0; k < i; ++k, w = w * u % P) {
				int x = a[j + k], y = w * a[j + k + i] % P;
				#define modto(x) ({if ((x) >= P) (x) -= P;})
				a[j + k] = x + y; modto(a[j + k]);
				a[j + k + i] = x - y + P; modto(a[j + k + i]);
				#undef modto
	}}}
	if (op == -1) { ll inv = INV(n);
		for (int i = 0; i < n; ++i) a[i] = a[i] * inv % P; }
}
vl multi(vl a, vl b) {
	int m = a.size() + b.size() - 1;
	int n = 1; while (n < m) n *= 2;
	vl fa(n), fb(n), fc(n);
	copy(a.begin(), a.end(), fa.begin()); ntt(fa);
	copy(b.begin(), b.end(), fb.begin()); ntt(fb);
	for (int i = 0; i < n; ++i) fc[i] = fa[i] * fb[i] % P;
	ntt(fc, -1); fc.resize(m);
	return fc;
}

const int N = 1e5 + 11;
int b[N];
ll fac[N], inv[N], ifac[N];
ll binom(int n, int m) {
	if(n < m) return 0;
	return fac[n] * ifac[m] % P * ifac[n - m] % P;
}

ll f[N], g[N];

int main() {
	ios::sync_with_stdio(0);
	
	inv[1] = 1;
	for(int i = 2; i < N; i ++)
		inv[i] = (P - P / i) * inv[P % i] % P;
	fac[0] = ifac[0] = 1;
	for(int i = 1; i < N; i ++) {
		fac[i] = i * fac[i - 1] % P;
		ifac[i] = inv[i] * ifac[i - 1] % P;
	}
	
	int m, n; cin >> m >> n;
	for(int i = 1; i <= n; i ++) {
		int a; cin >> a;
		b[a] ++;
	}
	map <int, int> mp;
	for(int i = 1; i <= m; i ++)
		if(b[i]) mp[b[i]] ++;

	for(int k = 1; k <= n; k ++) {
		f[k] = 1;
		for(auto [b, c] : mp) {
			f[k] *= qpow(binom(k, b) * fac[b] % P, c);
			f[k] %= P;
		}
		f[k] = f[k] * ifac[k] % P;
	}
	
	for(int i = 0; i <= n; i ++) {
		int sgn = (i & 1) ? P - 1 : 1;
		g[i] = sgn * ifac[i] % P;
	}
	
	vl F(f, f + n + 1);
	vl G(g, g + n + 1);
	
	vl H = multi(F, G);
	
	ll ans = 0;
	for(int i = 0; i <= n; i ++) {
		ans += H[i] * fac[i];
		ans %= P;
	}
	cout << ans << '\n';
}
0