Submission #63604107


Source Code Expand

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

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef double db;

const int N = 5047;

const int mod = 998244353;

int add(int a, int b)
{
	return a + b < mod ? a + b : a + b - mod;
}

void updAdd(int& a, int b)
{
	a += b;
	if (a >= mod)
		a -= mod;
}

int sub(int a, int b)
{
	return a - b >= 0 ? a - b : a - b + mod;
}

void updSub(int& a, int b)
{
	a -= b;
	if (a < 0)
		a += mod;
}

int mult(int a, int b)
{
	return (LL)a * b % mod;
}

int binpow(int a, LL n)
{
	int res = 1;
	while (n)
	{
		if (n & 1)
			res = mult(res, a);
		a = mult(a, a);
		n /= 2;
	}
	return res;
}

int inv[N], fact[N], ifact[N];

void init()
{
	inv[1] = 1;
	FOR(i, 2, N)
	{
		inv[i] = mult(mod - mod / i, inv[mod % i]);
	}
	fact[0] = ifact[0] = 1;
	FOR(i, 1, N)
	{
		fact[i] = mult(fact[i - 1], i);
		ifact[i] = mult(ifact[i - 1], inv[i]);
	}
}

int C(int n, int k)
{
	if (k < 0 || k > n)
		return 0;
	return mult(fact[n], mult(ifact[n - k], ifact[k]));
}

const int mod1 = 1e9 + 7;
const int mod2 = 1e9 + 9;
const int P = 4747;
const LL modH = (LL)mod1 * mod2;
LL pwH[N];

LL addH(LL a, LL b)
{
	return a + b < modH ? a + b : a + b - modH;
}

LL multH(LL a, LL b)
{
	return (__int128)a * b % modH;
}

VI poses[N];
int mn[N][N];
LL h1[N][N], h2[N][N];

int main()
{
	ios::sync_with_stdio(0); 
	cin.tie(0);
	init();
	pwH[0] = 1;
	FOR(i, 1, P)
	{
		pwH[i] = multH(pwH[i - 1], P);
	}
	
	int n;
	string s;
	cin >> n >> s;
	VI a(n + 1);
	FOR(i, 0, n)
	{
		if (s[i] == '(')
			a[i + 1] = a[i] + 1;
		else
			a[i + 1] = a[i] - 1;
	}
	FOR(i, 0, n + 1)
	{
		assert(a[i] >= 0);
		poses[a[i]].PB(i);
	}
	RFOR(i, n + 1, 0)
	{
		FOR(j, i, n + 1)
		{
			if (i == j)
			{
				mn[i][i] = a[i];
			}
			else
			{
				mn[i][j] = min(mn[i][j - 1], a[j]);
				h1[i][j] = addH(s[i], multH(P, h1[i + 1][j]));
				h2[i][j] = addH(multH(P, h2[i][j - 1]), s[j - 1] ^ 1);
			}
		}
	}
	unordered_map<LL, int> cntHashes;
	int ans = 1;
	FOR(bal, 0, n + 1)
	{
		int j;
		for (int i = 0; i < SZ(poses[bal]); i = j)
		{
			cntHashes.clear();
			j = i + 1;
			while (j < SZ(poses[bal]) && mn[poses[bal][i]][poses[bal][j]] == bal)
			{
				int p1 = poses[bal][j - 1], p2 = poses[bal][j];
				LL hDir = h1[p1][p2], hRev = h2[p1][p2];
				if (hDir != hRev)
				{
					ans = mult(2, ans);
					if (hDir > hRev)
					{
						swap(hDir, hRev);
					}
				}
				cntHashes[hDir]++;
				j++;
			}
			ans = mult(ans, fact[j - i - 1]);
			for (auto [hsh, cnt] : cntHashes)
			{
				ans = mult(ans, ifact[cnt]);
			}
		}
	}
	cout << ans << "\n";
	return 0;
}

Submission Info

Submission Time
Task D - Reverse Brackets
User mshcherba
Language C++ 20 (gcc 12.2)
Score 0
Code Size 3051 Byte
Status WA
Exec Time 248 ms
Memory 304260 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 2
AC × 9
WA × 20
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, test_00.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3792 KiB
example_01.txt AC 1 ms 3672 KiB
test_00.txt WA 32 ms 49472 KiB
test_01.txt WA 19 ms 31304 KiB
test_02.txt WA 136 ms 182192 KiB
test_03.txt WA 7 ms 12696 KiB
test_04.txt WA 228 ms 298984 KiB
test_05.txt WA 199 ms 260516 KiB
test_06.txt WA 4 ms 8332 KiB
test_07.txt WA 9 ms 17560 KiB
test_08.txt WA 79 ms 111032 KiB
test_09.txt WA 2 ms 5980 KiB
test_10.txt WA 235 ms 303972 KiB
test_11.txt WA 233 ms 304132 KiB
test_12.txt WA 234 ms 304104 KiB
test_13.txt WA 234 ms 304108 KiB
test_14.txt WA 233 ms 304116 KiB
test_15.txt WA 233 ms 304124 KiB
test_16.txt WA 233 ms 304132 KiB
test_17.txt WA 232 ms 304068 KiB
test_18.txt WA 232 ms 304136 KiB
test_19.txt WA 248 ms 304040 KiB
test_20.txt AC 231 ms 304260 KiB
test_21.txt AC 233 ms 304140 KiB
test_22.txt AC 161 ms 216256 KiB
test_23.txt AC 161 ms 216208 KiB
test_24.txt AC 162 ms 216200 KiB
test_25.txt AC 23 ms 38112 KiB
test_26.txt AC 77 ms 109020 KiB