結果

問題 No.2084 Mex Subset For All Sequences
ユーザー ygussanyygussany
提出日時 2022-09-17 15:39:01
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 4,494 bytes
コンパイル時間 507 ms
コンパイル使用メモリ 33,536 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2024-06-01 15:20:13
合計ジャッジ時間 2,631 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 20 ms
5,632 KB
testcase_09 AC 38 ms
8,576 KB
testcase_10 AC 42 ms
8,704 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 19 ms
5,376 KB
testcase_13 AC 19 ms
5,376 KB
testcase_14 AC 10 ms
5,376 KB
testcase_15 AC 40 ms
9,216 KB
testcase_16 AC 37 ms
8,576 KB
testcase_17 AC 19 ms
5,376 KB
testcase_18 AC 79 ms
15,360 KB
testcase_19 AC 79 ms
15,488 KB
testcase_20 AC 79 ms
15,488 KB
testcase_21 AC 82 ms
15,488 KB
testcase_22 AC 79 ms
15,488 KB
testcase_23 AC 78 ms
15,488 KB
testcase_24 AC 79 ms
15,488 KB
testcase_25 AC 78 ms
15,488 KB
testcase_26 AC 80 ms
15,488 KB
testcase_27 AC 78 ms
15,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

const int Mod = 998244353,
	bit[21] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576},
	bit_inv[21] = {1, 499122177, 748683265, 873463809, 935854081, 967049217, 982646785, 990445569, 994344961, 996294657, 997269505, 997756929, 998000641, 998122497, 998183425, 998213889, 998229121, 998236737, 998240545, 998242449, 998243401},
	root[21] = {1, 998244352, 911660635, 372528824, 929031873, 452798380, 922799308, 781712469, 476477967, 166035806, 258648936, 584193783, 63912897, 350007156, 666702199, 968855178, 629671588, 24514907, 996173970, 363395222, 565042129},
	root_inv[21] = {1, 998244352, 86583718, 509520358, 337190230, 87557064, 609441965, 135236158, 304459705, 685443576, 381598368, 335559352, 129292727, 358024708, 814576206, 708402881, 283043518, 3707709, 121392023, 704923114, 950391366};
int ntt_b[21][1048576], ntt_c[21][1048576], ntt_x[21][1048576], ntt_y[21][1048576];
	
void NTT(int k, int a[], int z[])
{
	if (k == 0) {
		z[0] = a[0];
		return;
	}
	
	int i, d = bit[k-1], tmpp;
	long long tmp;
	for (i = 0; i < d; i++) {
		ntt_b[k][i] = a[i*2];
		ntt_c[k][i] = a[i*2+1];
	}
	NTT(k - 1, ntt_b[k], ntt_x[k]);
	NTT(k - 1, ntt_c[k], ntt_y[k]);
	for (i = 0, tmp = 1; i < d; i++, tmp = tmp * root[k] % Mod) {
		tmpp = tmp * ntt_y[k][i] % Mod;
		z[i] = ntt_x[k][i] + tmpp;
		if (z[i] >= Mod) z[i] -= Mod;
		z[i+d] = ntt_x[k][i] - tmpp;
		if (z[i+d] < 0) z[i+d] += Mod;
	}
}

void NTT_reverse(int k, int z[], int a[])
{
	if (k == 0) {
		a[0] = z[0];
		return;
	}
	
	int i, d = bit[k-1], tmpp;
	long long tmp;
	for (i = 0; i < d; i++) {
		ntt_x[k][i] = z[i*2];
		ntt_y[k][i] = z[i*2+1];
	}
	NTT_reverse(k - 1, ntt_x[k], ntt_b[k]);
	NTT_reverse(k - 1, ntt_y[k], ntt_c[k]);
	for (i = 0, tmp = 1; i < d; i++, tmp = tmp * root_inv[k] % Mod) {
		tmpp = tmp * ntt_c[k][i] % Mod;
		a[i] = ntt_b[k][i] + tmpp;
		if (a[i] >= Mod) a[i] -= Mod;
		a[i+d] = ntt_b[k][i] - tmpp;
		if (a[i+d] < 0) a[i+d] += Mod;
	}
}
// Compute the product of two polynomials a[0-da] and b[0-db] using NTT in O(d * log d) time
void prod_poly_NTT(int da, int db, int a[], int b[], int c[])
{
	int i, k;
	static int aa[1048576], bb[1048576], cc[1048576];
	for (k = 0; bit[k] <= da + db; k++);
	for (i = 0; i <= da; i++) aa[i] = a[i];
	for (i = da + 1; i < bit[k]; i++) aa[i] = 0;
	for (i = 0; i <= db; i++) bb[i] = b[i];
	for (i = db + 1; i < bit[k]; i++) bb[i] = 0;
	
	static int x[1048576], y[1048576], z[1048576];
	NTT(k, aa, x);
	if (db == da) {
		for (i = 0; i <= da; i++) if (a[i] != b[i]) break;
		if (i <= da) NTT(k, bb, y);
		else for (i = 0; i < bit[k]; i++) y[i] = x[i];
	} else NTT(k, bb, y);
	for (i = 0; i < bit[k]; i++) z[i] = (long long)x[i] * y[i] % Mod;
	NTT_reverse(k, z, cc);
	for (i = 0; i <= da + db; i++) c[i] = (long long)cc[i] * bit_inv[k] % Mod;
}

// Compute the product of two polynomials a[0-da] and b[0-db] naively in O(da * db) time
void prod_poly_naive(int da, int db, int a[], int b[], int c[])
{
	int i, j;
	for (i = 0; i <= da + db; i++) c[i] = 0;
	for (i = 0; i <= da; i++) {
		for (j = 0; j <= db; j++) {
			c[i+j] += (long long)a[i] * b[j] % Mod;
			if (c[i+j] >= Mod) c[i+j] -= Mod;
		}
	}
}

// Compute the product of two polynomials a[0-da] and b[0-db] in an appropriate way
void prod_polynomial(int da, int db, int a[], int b[], int c[])
{
	const int THR = 250000;
	if (THR / (da + 1) >= db + 1) prod_poly_naive(da, db, a, b, c);
	else prod_poly_NTT(da, db, a, b, c);
}

long long fact[100003], fact_inv[100003];

long long div_mod(long long x, long long y, long long z)
{
	if (x % y == 0) return x / y;
	else return (div_mod((1 + x / y) * y - x, (z % y), y) * z + x) / y;
}

long long pow_mod(int n, long long k)
{
	long long N, ans = 1;
	for (N = n; k > 0; k >>= 1, N = N * N % Mod) if (k & 1) ans = ans * N % Mod;
	return ans;
}

int main()
{
	int N, M;
	scanf("%d %d", &N, &M);
	
	int i, d = (N <= M)? N: M;
	for (i = 1, fact[0] = 1; i <= d + 1; i++) fact[i] = fact[i-1] * i % Mod;
	for (i = d, fact_inv[d+1] = div_mod(1, fact[d+1], Mod); i >= 0; i--) fact_inv[i] = fact_inv[i+1] * (i + 1) % Mod;

	int a[262144], b[262144], c[262144];
	for (i = 0; i <= d; i++) {
		if (i % 2 == 0) a[i] = pow_mod(M * 2 - i, N);
		else a[i] = Mod - pow_mod(M * 2 - i, N);
		a[i] = a[i] * fact_inv[i] % Mod;
		b[i] = fact_inv[i];
	}
	prod_polynomial(d, d, a, b, c);
	
	long long ans = 0;
	for (i = 1; i <= d; i++) ans += fact[i] * c[i] % Mod;
	printf("%lld\n", ans % Mod);
	fflush(stdout);
	return 0;
}
0