結果

問題 No.1697 Deque House
ユーザー 👑 ygussanyygussany
提出日時 2021-09-29 09:41:29
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 824 ms / 3,500 ms
コード長 3,815 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 34,688 KB
実行使用メモリ 36,116 KB
最終ジャッジ日時 2024-07-19 10:18:13
合計ジャッジ時間 6,369 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int Mod = 998244353,
	bit[19] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144},
	bit_inv[19] = {1, 499122177, 748683265, 873463809, 935854081, 967049217, 982646785, 990445569, 994344961, 996294657, 997269505, 997756929, 998000641, 998122497, 998183425, 998213889, 998229121, 998236737, 998240545},
	root[19] = {1, 998244352, 911660635, 372528824, 929031873, 452798380, 922799308, 781712469, 476477967, 166035806, 258648936, 584193783, 63912897, 350007156, 666702199, 968855178, 629671588, 24514907, 996173970},
	root_inv[19] = {1, 998244352, 86583718, 509520358, 337190230, 87557064, 609441965, 135236158, 304459705, 685443576, 381598368, 335559352, 129292727, 358024708, 814576206, 708402881, 283043518, 3707709, 121392023};
int ntt_b[19][262144], ntt_c[19][262144], ntt_x[19][262144], ntt_y[19][262144];
	
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;
	}
}

void prod_poly_NTT(int da, int db, int a[], int b[], int c[])
{
	int i, k;
	for (k = 0; bit[k] < da + db - 1; k++);
	for (i = da; i < bit[k]; i++) a[i] = 0;
	for (i = db; i < bit[k]; i++) b[i] = 0;
	
	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, K, A[100001];
	scanf("%d %d", &N, &K);
	for (i = 1; i <= N; i++) scanf("%d", &(A[i]));
	
	int j, k, dp[2][15][100001];
	long long pow[1400001];
	for (i = 1, pow[0] = 1; i <= N * K; i++) pow[i] = pow[i-1] * 2 % Mod;
	for (j = 1; j <= N; j++) {
		dp[0][0][j] = 1;
		dp[1][0][j] = (j == 1)? 1: 0;
	}
	for (k = 1; k <= K; k++) {
		dp[0][k][1] = 1;
		dp[1][k][1] = 1;
		for (j = 2; j <= N; j++) {
			dp[0][k][j] = dp[0][k][j-1] + dp[0][k-1][j] * pow[j-1] % Mod;
			if (dp[0][k][j] >= Mod) dp[0][k][j] -= Mod;
			dp[1][k][j] = dp[1][k][j-1] + dp[1][k-1][j] * pow[j-1] % Mod;
			if (dp[1][k][j] >= Mod) dp[1][k][j] -= Mod;
		}
	}
	
	int a[262144], b[262144], c[262144];
	long long ans = 0, tmp[100001];
	for (j = 1; j <= N; j++) tmp[j] = 1;
	for (k = 0; k < K; k++) {
		for (j = 1, a[0] = 0, b[0] = 0; j <= N; j++) {
			a[j] = dp[0][K][j];
			b[j] = dp[1][K-k][j] * pow[k*j] % Mod;
		}
		prod_poly_NTT(N + 1, N + 1, a, b, c);
		for (j = 1; j <= N; j++) {
			ans += tmp[j] * (((long long)dp[0][k][j] * c[N-j] + (long long)dp[0][k][N-j+1] * c[j-1]) % Mod) % Mod;
			tmp[j] = tmp[j] * A[j] % Mod;
		}
	}
	for (j = 1; j <= N; j++) ans += tmp[j] * (((long long)dp[0][K][j-1] * dp[0][K][N-j+1] + (long long)dp[1][K][j] * dp[0][K][N-j]) % Mod) % Mod;
	printf("%lld\n", ans % Mod);
	fflush(stdout);
	return 0;
}
0