結果

問題 No.2670 Sum of Products of Interval Lengths
ユーザー 👑 ygussanyygussany
提出日時 2024-02-25 10:33:09
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 505 ms / 2,000 ms
コード長 5,316 bytes
コンパイル時間 1,677 ms
コンパイル使用メモリ 33,792 KB
実行使用メモリ 25,856 KB
最終ジャッジ日時 2024-09-29 10:48:57
合計ジャッジ時間 7,159 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 503 ms
25,856 KB
testcase_02 AC 115 ms
9,856 KB
testcase_03 AC 48 ms
7,040 KB
testcase_04 AC 115 ms
9,856 KB
testcase_05 AC 241 ms
15,232 KB
testcase_06 AC 249 ms
15,488 KB
testcase_07 AC 114 ms
9,856 KB
testcase_08 AC 48 ms
7,040 KB
testcase_09 AC 114 ms
9,856 KB
testcase_10 AC 244 ms
15,232 KB
testcase_11 AC 250 ms
15,488 KB
testcase_12 AC 501 ms
25,856 KB
testcase_13 AC 505 ms
25,856 KB
testcase_14 AC 503 ms
25,856 KB
testcase_15 AC 504 ms
25,856 KB
testcase_16 AC 505 ms
25,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

#define NTT_MAX 20
#define NTT_d_MAX (1 << NTT_MAX)

const int Mod = 998244353,
	bit[24] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608},
	bit_inv[24] = {1, 499122177, 748683265, 873463809, 935854081, 967049217, 982646785, 990445569, 994344961, 996294657, 997269505, 997756929, 998000641, 998122497, 998183425, 998213889, 998229121, 998236737, 998240545, 998242449, 998243401, 998243877, 998244115, 998244234},
	root[24] = {1, 998244352, 911660635, 372528824, 929031873, 452798380, 922799308, 781712469, 476477967, 166035806, 258648936, 584193783, 63912897, 350007156, 666702199, 968855178, 629671588, 24514907, 996173970, 363395222, 565042129, 733596141, 267099868, 15311432},
	root_inv[24] = {1, 998244352, 86583718, 509520358, 337190230, 87557064, 609441965, 135236158, 304459705, 685443576, 381598368, 335559352, 129292727, 358024708, 814576206, 708402881, 283043518, 3707709, 121392023, 704923114, 950391366, 428961804, 382752275, 469870224};

void NTT_inline(int kk, int a[], int x[])
{
	int h, hh, i, ii, j, jj, k, l, r = bit[kk], d = bit[kk-1], tmpp, cur, prev;
	int *pi, *pii, *pj, *pjj;
	static int y[2][NTT_d_MAX];
	long long tmp;
	for (i = 0; i < r; i++) y[0][i] = a[i];
	for (k = 1, kk--, cur = 1, prev = 0; kk >= 0; k++, kk--, cur ^= 1, prev ^= 1) {
		for (h = 0, tmp = 1; h << (kk + 1) < r; h++, tmp = tmp * root[k] % Mod) {
			for (hh = 0, pi = &(y[cur][h<<kk]), pii = pi + d, pj = &(y[prev][h<<(kk+1)]), pjj = pj + bit[kk]; hh < bit[kk]; hh++, pi++, pii++, pj++, pjj++) {
				tmpp = tmp * (*pjj) % Mod;
				*pi = *pj + tmpp;
				if (*pi >= Mod) *pi -= Mod;
				*pii = *pj - tmpp;
				if (*pii < 0) *pii += Mod;
			}
		}
	}
	for (i = 0; i < r; i++) x[i] = y[prev][i];
}

void NTT_reverse_inline(int kk, int a[], int x[])
{
	int h, hh, i, ii, j, jj, k, l, r = bit[kk], d = bit[kk-1], tmpp, cur, prev;
	int *pi, *pii, *pj, *pjj;
	static int y[2][NTT_d_MAX];
	long long tmp;
	for (i = 0; i < r; i++) y[0][i] = a[i];
	for (k = 1, kk--, cur = 1, prev = 0; kk >= 0; k++, kk--, cur ^= 1, prev ^= 1) {
		for (h = 0, tmp = 1; h << (kk + 1) < r; h++, tmp = tmp * root_inv[k] % Mod) {
			for (hh = 0, pi = &(y[cur][h<<kk]), pii = pi + d, pj = &(y[prev][h<<(kk+1)]), pjj = pj + bit[kk]; hh < bit[kk]; hh++, pi++, pii++, pj++, pjj++) {
				tmpp = tmp * (*pjj) % Mod;
				*pi = *pj + tmpp;
				if (*pi >= Mod) *pi -= Mod;
				*pii = *pj - tmpp;
				if (*pii < 0) *pii += Mod;
			}
		}
	}
	for (i = 0; i < r; i++) x[i] = y[prev][i];
}

#define NTT_THR 70

// Compute c[0-dc] = a[0-da] * b[0-db] (naive)
void FPS_prod_naive(int da, int db, int dc, int a[], int b[], int c[])
{
	int i, j, sa, sb;
	static int supp_a[NTT_d_MAX], supp_b[NTT_d_MAX];
	static long long tmp[NTT_d_MAX];
	for (i = 0, sa = 0; i <= da; i++) if (a[i] != 0) supp_a[sa++] = i;
	for (i = 0, sb = 0; i <= db; i++) if (b[i] != 0) supp_b[sb++] = i;
	for (i = 0; i <= dc; i++) tmp[i] = 0;
	for (i = 0; i < sa; i++) for (j = 0; j < sb && supp_a[i] + supp_b[j] <= dc; j++) tmp[supp_a[i] + supp_b[j]] += (long long)a[supp_a[i]] * b[supp_b[j]] % Mod;
	for (i = 0; i <= dc; i++) c[i] = tmp[i] % Mod;
}

// Compute c[0-dc] = a[0-da] * b[0-db] (NTT)
void FPS_prod_NTT(int da, int db, int dc, int a[], int b[], int c[])
{
	int i, k;
	static int aa[NTT_d_MAX], bb[NTT_d_MAX], cc[NTT_d_MAX];
	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[NTT_d_MAX], y[NTT_d_MAX], z[NTT_d_MAX];
	NTT_inline(k, aa, x);
	if (db == da) {
		for (i = 0; i <= da; i++) if (a[i] != b[i]) break;
		if (i <= da) NTT_inline(k, bb, y);
		else for (i = 0; i < bit[k]; i++) y[i] = x[i];
	} else NTT_inline(k, bb, y);
	for (i = 0; i < bit[k]; i++) z[i] = (long long)x[i] * y[i] % Mod;
	NTT_reverse_inline(k, z, cc);
	for (i = 0; i <= dc; i++) c[i] = (long long)cc[i] * bit_inv[k] % Mod;
}

// Compute c[0-dc] = a[0-da] * b[0-db]
void FPS_prod(int da, int db, int dc, int a[], int b[], int c[])
{
	int i, sa, sb;
	if (da > dc) da = dc;
	if (db > dc) db = dc;
	for (i = 0, sa = 0; i <= da && sa <= NTT_THR; i++) if (a[i] != 0) sa++;
	for (i = 0, sb = 0; i <= db && sb <= NTT_THR; i++) if (b[i] != 0) sb++;
	if (sa <= NTT_THR || sb <= NTT_THR) FPS_prod_naive(da, db, dc, a, b, c);
	else FPS_prod_NTT(da, db, dc, a, b, c);
}



void solve_recursion(int l, int r, long long ans[], int coeff[])
{
	if (l == r) {
		ans[l] %= Mod;
		return;
	}
	
	int i, m = (l + r) / 2;
	static int a[200001], b[200001];
	solve_recursion(l, m, ans, coeff);
	for (i = 0; i <= m - l; i++) a[i] = ans[i+l];
	FPS_prod(m - l, r - l, r - l, a, coeff, b);
	for (i = m - l; i < r - l; i++) ans[i+l+1] += b[i];
	solve_recursion(m + 1, r, ans, coeff);
}

long long solve(int n, long long m)
{
	int i, coeff[200001] = {};
	long long ans[200001] = {1};
	for (i = 0; i <= n && i < m; i++) {
		switch (i % 6) {
		case 0:
		case 1:
			coeff[i] = (m - i) % Mod;
			break;
		case 3:
		case 4:
			coeff[i] = (Mod - (m - i) % Mod) % Mod;
			break;
		}
	}
	solve_recursion(0, n, ans, coeff);
	return ans[n];
}

int main()
{
	int n;
	long long m;
	scanf("%d %lld", &n, &m);
	printf("%lld\n", solve(n, m));
	fflush(stdout);
	return 0;
}
0