結果

問題 No.1504 ヌメロニム
ユーザー 👑 ygussanyygussany
提出日時 2021-05-07 23:31:29
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 470 ms / 2,000 ms
コード長 3,599 bytes
コンパイル時間 1,095 ms
コンパイル使用メモリ 33,068 KB
実行使用メモリ 64,112 KB
最終ジャッジ日時 2023-10-13 14:51:57
合計ジャッジ時間 12,325 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
16,272 KB
testcase_01 AC 5 ms
17,900 KB
testcase_02 AC 5 ms
17,860 KB
testcase_03 AC 5 ms
16,316 KB
testcase_04 AC 6 ms
17,456 KB
testcase_05 AC 6 ms
17,576 KB
testcase_06 AC 4 ms
16,328 KB
testcase_07 AC 5 ms
17,524 KB
testcase_08 AC 5 ms
16,592 KB
testcase_09 AC 5 ms
17,164 KB
testcase_10 AC 6 ms
17,184 KB
testcase_11 AC 4 ms
16,460 KB
testcase_12 AC 6 ms
16,540 KB
testcase_13 AC 6 ms
18,204 KB
testcase_14 AC 6 ms
17,924 KB
testcase_15 AC 6 ms
17,652 KB
testcase_16 AC 5 ms
17,380 KB
testcase_17 AC 6 ms
16,364 KB
testcase_18 AC 5 ms
17,368 KB
testcase_19 AC 6 ms
17,032 KB
testcase_20 AC 8 ms
17,740 KB
testcase_21 AC 7 ms
17,816 KB
testcase_22 AC 8 ms
16,996 KB
testcase_23 AC 5 ms
18,832 KB
testcase_24 AC 460 ms
63,920 KB
testcase_25 AC 222 ms
41,348 KB
testcase_26 AC 457 ms
63,892 KB
testcase_27 AC 225 ms
41,328 KB
testcase_28 AC 222 ms
41,412 KB
testcase_29 AC 456 ms
63,852 KB
testcase_30 AC 460 ms
63,892 KB
testcase_31 AC 226 ms
41,300 KB
testcase_32 AC 227 ms
41,312 KB
testcase_33 AC 466 ms
64,112 KB
testcase_34 AC 55 ms
23,040 KB
testcase_35 AC 54 ms
22,892 KB
testcase_36 AC 228 ms
41,328 KB
testcase_37 AC 30 ms
19,888 KB
testcase_38 AC 464 ms
63,920 KB
testcase_39 AC 462 ms
64,024 KB
testcase_40 AC 464 ms
63,936 KB
testcase_41 AC 470 ms
63,860 KB
testcase_42 AC 465 ms
63,888 KB
testcase_43 AC 468 ms
63,840 KB
testcase_44 AC 461 ms
63,940 KB
testcase_45 AC 461 ms
63,916 KB
testcase_46 AC 468 ms
63,968 KB
testcase_47 AC 455 ms
63,864 KB
testcase_48 AC 218 ms
41,364 KB
testcase_49 AC 221 ms
41,272 KB
testcase_50 AC 7 ms
18,008 KB
testcase_51 AC 6 ms
16,748 KB
testcase_52 AC 4 ms
16,636 KB
testcase_53 AC 6 ms
16,620 KB
testcase_54 AC 5 ms
18,232 KB
testcase_55 AC 54 ms
23,364 KB
testcase_56 AC 5 ms
17,240 KB
testcase_57 AC 4 ms
16,188 KB
testcase_58 AC 5 ms
17,712 KB
testcase_59 AC 4 ms
16,648 KB
testcase_60 AC 4 ms
16,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <stdlib.h>

const int Mod = 998244353;
int bit[21], bit_inv[21], root[21], root_inv[21];

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, int 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;
}

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;
	static int *b[21] = {}, *c[21] = {}, *x[21] = {}, *y[21] = {};
	if (b[k] == NULL) {
		b[k] = (int*)malloc(sizeof(int) * d);
		c[k] = (int*)malloc(sizeof(int) * d);
		x[k] = (int*)malloc(sizeof(int) * d);
		y[k] = (int*)malloc(sizeof(int) * d);
	}
	for (i = 0; i < d; i++) {
		b[k][i] = a[i*2];
		c[k][i] = a[i*2+1];
	}
	NTT(k - 1, b[k], x[k]);
	NTT(k - 1, c[k], y[k]);
	for (i = 0, tmp = 1; i < d; i++, tmp = tmp * root[k] % Mod) {
		tmpp = tmp * y[k][i] % Mod;
		z[i] = x[k][i] + tmpp;
		if (z[i] >= Mod) z[i] -= Mod;
		z[i+d] = 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;
	static int *b[21] = {}, *c[21] = {}, *x[21] = {}, *y[21] = {};
	if (b[k] == NULL) {
		b[k] = (int*)malloc(sizeof(int) * d);
		c[k] = (int*)malloc(sizeof(int) * d);
		x[k] = (int*)malloc(sizeof(int) * d);
		y[k] = (int*)malloc(sizeof(int) * d);
	}
	for (i = 0; i < d; i++) {
		x[k][i] = z[i*2];
		y[k][i] = z[i*2+1];
	}
	NTT_reverse(k - 1, x[k], b[k]);
	NTT_reverse(k - 1, y[k], c[k]);
	for (i = 0, tmp = 1; i < d; i++, tmp = tmp * root_inv[k] % Mod) {
		tmpp = tmp * c[k][i] % Mod;
		a[i] = b[k][i] + tmpp;
		if (a[i] >= Mod) a[i] -= Mod;
		a[i+d] = b[k][i] - tmpp;
		if (a[i+d] < 0) a[i+d] += Mod;
	}
}

void prod_poly_NTT(int da, int db, int a[], int b[], int c[])
{
	int i, k;
	for (k = 0, bit[0] = 1; bit[k] < da + db - 1; k++) bit[k+1] = bit[k] * 2;
	for (i = k - 1, bit_inv[k] = div_mod(1, bit[k], Mod); i >= 0; i--) bit_inv[i] = bit_inv[i+1] * 2 % Mod;
	for (i = k - 1, root[k] = pow_mod(3, (Mod - 1) / bit[k]), root_inv[k] = pow_mod(3, Mod - 1 - (Mod - 1) / bit[k]); i >= 0; i--) {
		root[i] = (long long)root[i+1] * root[i+1] % Mod;
		root_inv[i] = (long long)root_inv[i+1] * root_inv[i+1] % Mod;
	}
	
	int *x = (int*)malloc(sizeof(int) * bit[k]), *y = (int*)malloc(sizeof(int) * bit[k]), *z = (int*)malloc(sizeof(int) * bit[k]);
	NTT(k, a, x);
	NTT(k, b, y);
	for (i = 0; i < bit[k]; i++) z[i] = (long long)x[i] * y[i] % Mod;
	NTT_reverse(k, z, c);
	for (i = 0; i < da + db - 1; i++) c[i] = (long long)c[i] * bit_inv[k] % Mod;
	
	free(x);
	free(y);
	free(z);
}

int main()
{
	int i, N;
	char S[300001];
	scanf("%d", &N);
	scanf("%s", S);
	
	int a[1048576] = {}, b[1048576] = {}, c[1048576] = {};
	for (i = 0; i < N; i++) {
		if (S[i] == 'i') a[N-i-1] = 1;
		else b[i] = 1;
	}
	prod_poly_NTT(N, N, a, b, c);
	
	long long ans = 0, fact[300001], fact_inv[300001];
	for (i = 1, fact[0] = 1; i <= N; i++) fact[i] = fact[i-1] * i % Mod;
	for (i = N - 1, fact_inv[N] = div_mod(1, fact[N], Mod); i >= 0; i--) fact_inv[i] = fact_inv[i+1] * (i + 1) % Mod;
	for (i = N; i < N * 2 - 1; i++) a[i-N] = c[i] * fact[i-N] % Mod;
	for (i = N - 1; i < 1048576; i++) a[i] = 0;
	for (i = 0; i < N - 1; i++) b[i] = fact_inv[N-2-i];
	for (i = N - 1; i < 1048576; i++) b[i] = 0;
	prod_poly_NTT(N - 1, N - 1, a, b, c);
	for (i = 0; i < N - 1; i++) ans ^= c[i+N-2] * fact_inv[i] % Mod;
	printf("%lld\n", ans);
	fflush(stdout);
	return 0;
}
0