提出 #64379717


ソースコード 拡げる

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

#define ll long long
#define all(c) ((c).begin()), ((c).end())
#define sz(x) ((int)(x).size())

#ifdef LOCAL
#include <print.h>
#else
#define trace(...)
#define endl '\n' // remove in interactive
#endif

// ModInt source: https://github.com/the-tourist/algo/blob/master/numeric/mint.cpp
template <typename T>
T inverse(T a, T m) {
  T u = 0, v = 1;
  while (a != 0) {
	T t = m / a;
	m -= t * a; swap(a, m);
	u -= t * v; swap(u, v);
  }
  assert(m == 1);
  return u;
}

template <typename T>
class Modular {
 public:
  using Type = typename decay<decltype(T::value)>::type;

  constexpr Modular() : value() {}
  template <typename U>
  Modular(const U& x) {
	value = normalize(x);
  }

  template <typename U>
  static Type normalize(const U& x) {
	Type v;
	if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
	else v = static_cast<Type>(x % mod());
	if (v < 0) v += mod();
	return v;
  }

  const Type& operator()() const { return value; }
  template <typename U>
  explicit operator U() const { return static_cast<U>(value); }
  constexpr static Type mod() { return T::value; }

  Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; }
  Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; }
  template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); }
  template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); }
  Modular& operator++() { return *this += 1; }
  Modular& operator--() { return *this -= 1; }
  Modular operator++(int) { Modular result(*this); *this += 1; return result; }
  Modular operator--(int) { Modular result(*this); *this -= 1; return result; }
  Modular operator-() const { return Modular(-value); }

  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) {
#ifdef _WIN32
uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value);
	uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m;
	asm(
	  "divl %4; \n\t"
	  : "=a" (d), "=d" (m)
	  : "d" (xh), "a" (xl), "r" (mod())
	);
	value = m;
#else
	value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value));
#endif
	return *this;
  }
  template <typename U = T>
  typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) {
	int64_t q = static_cast<int64_t>(static_cast<long double>(value) * rhs.value / mod());
	value = normalize(value * rhs.value - q * mod());
	return *this;
  }
  template <typename U = T>
  typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) {
	value = normalize(value * rhs.value);
	return *this;
  }

  Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); }

  template <typename U>
  friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);

  template <typename U>
  friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);

  template <typename U>
  friend std::istream& operator>>(std::istream& stream, Modular<U>& number);

 private:
  Type value;
};

template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; }
template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); }
template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; }

template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); }
template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); }

template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; }

template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; }
template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; }

template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; }
template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; }

template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; }
template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; }

template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; }
template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; }

template<typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
  assert(b >= 0);
  Modular<T> x = a, res = 1;
  U p = b;
  while (p > 0) {
	if (p & 1) res *= x;
	x *= x;
	p >>= 1;
  }
  return res;
}

template <typename T>
string to_string(const Modular<T>& number) {
  return to_string(number());
}

template <typename T>
std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) {
  return stream << number();
}

template <typename T>
std::istream& operator>>(std::istream& stream, Modular<T>& number) {
  typename common_type<typename Modular<T>::Type, int64_t>::type x;
  stream >> x;
  number.value = Modular<T>::normalize(x);
  return stream;
}

/*
using ModType = int;

struct VarMod { static ModType value; };
ModType VarMod::value;
ModType& md = VarMod::value;
using Mint = Modular<VarMod>;
*/

constexpr int md = 998244353;
using mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>;
const int N = 10000006;
mint fact[N], invfact[N];
mint p2[N];

void pre(){
	fact[0] = 1;
	p2[0] = 1;
	for(int i = 1; i < N; i++){
		fact[i] = i * fact[i - 1];
		p2[i] = p2[i - 1] * 2;
	}
	invfact[N - 1] = 1 / fact[N - 1];
	for(int i = N - 2; i >= 0; i--){
		invfact[i] = (i + 1) * invfact[i + 1];
	}
	assert(invfact[0] == 1);
}

mint C(int n, int k){
	if(n < k || k < 0) return 0;
	return fact[n] * invfact[k] * invfact[n - k];
};
mint Ways(int n, int r){
	if(r < 0) return 0;
	if(r == 0) return n == 0;
	return C(n + r - 1, r - 1);
}
void solve(){
	int n, k;
	cin >> n >> k;
	string s; cin >> s;
	vector<int> pos = {-1};
	stack<int> st;
	int m = 0;
	int q = 0;
	for(int i = 0; i < k; i++){
		if(s[i] == '('){
			st.push(i);
		}
		else{
			st.pop();
			if(st.empty()){
				int delta = i -pos.back();
				if(delta == 2 && q == m) q++;
				m++;
				pos.push_back(i);
			}
		}
	}
	int place = n - k;
	if(q == m){
		mint ans = 0;
		for(int t = 0; t <= place; t++) ans += Ways(t, 2 * m) * p2[place - t];
		cout << ans << endl;
		return;
	}
	mint ans = Ways(place, m + 1);// no ) added
	for(int i = m; i >= 0; i--){
		// x_{2i} > 0,x_{2j} = 0 for j in [i + 1, m]
		int vars = 2 * m + 2;
		// x_{2r - 1} = 0, for r in [q + 2, i]
		vars -= m - i;
		vars -= max(0, i - q - 1);
		ans += Ways(place - 1, vars);
	}

	cout << ans << endl;
}

int main(){
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); // Remove in interactive problems
	int t = 1;
	pre();
	// cin >> t;
	while(t--){
		solve();
	}
}

提出情報

提出日時
問題 B - Maximum Bracket Subsequence
ユーザ jtnydv25
言語 C++ 20 (gcc 12.2)
得点 900
コード長 8274 Byte
結果 AC
実行時間 205 ms
メモリ 122116 KiB

コンパイルエラー

Main.cpp: In instantiation of ‘static Modular<T>::Type Modular<T>::normalize(const U&) [with U = bool; T = std::integral_constant<int, 998244353>; Type = int]’:
Main.cpp:36:19:   required from ‘Modular<T>::Modular(const U&) [with U = bool; T = std::integral_constant<int, 998244353>]’
Main.cpp:198:22:   required from here
Main.cpp:42:20: warning: comparison of constant ‘-998244353’ with boolean expression is always true [-Wbool-compare]
   42 |         if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
      |             ~~~~~~~^~~~
Main.cpp:42:30: warning: comparison of constant ‘998244353’ with boolean expression is always true [-Wbool-compare]
   42 |         if (-mod() <= x && x < mod()) v = static_cast<Type>(x);
      |                            ~~^~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 900 / 900
結果
AC × 4
AC × 63
セット名 テストケース
Sample 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 00_sample_04.txt
All 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 00_sample_04.txt, 01_random_case_01.txt, 01_random_case_02.txt, 01_random_case_03.txt, 01_random_case_04.txt, 01_random_case_05.txt, 01_random_case_06.txt, 01_random_case_07.txt, 01_random_case_08.txt, 01_random_case_09.txt, 01_random_case_10.txt, 01_random_case_11.txt, 01_random_case_12.txt, 01_random_case_13.txt, 01_random_case_14.txt, 01_random_case_15.txt, 01_random_case_16.txt, 02_simple_case_01.txt, 02_simple_case_02.txt, 02_simple_case_03.txt, 02_simple_case_04.txt, 02_simple_case_05.txt, 02_simple_case_06.txt, 02_simple_case_07.txt, 02_simple_case_08.txt, 02_simple_case_09.txt, 02_simple_case_10.txt, 02_simple_case_11.txt, 02_simple_case_12.txt, 03_max_case_01.txt, 03_max_case_02.txt, 03_max_case_03.txt, 03_max_case_04.txt, 03_max_case_05.txt, 03_max_case_06.txt, 03_max_case_07.txt, 03_max_case_08.txt, 03_max_case_09.txt, 03_max_case_10.txt, 03_max_case_11.txt, 03_max_case_12.txt, 03_max_case_13.txt, 03_max_case_14.txt, 03_max_case_15.txt, 03_max_case_16.txt, 03_max_case_17.txt, 03_max_case_18.txt, 03_max_case_19.txt, 03_max_case_20.txt, 04_corner_01.txt, 04_corner_02.txt, 04_corner_03.txt, 04_corner_04.txt, 04_corner_05.txt, 04_corner_06.txt, 05_handmade_01.txt, 05_handmade_02.txt, 05_handmade_03.txt, 05_handmade_04.txt, 05_handmade_05.txt
ケース名 結果 実行時間 メモリ
00_sample_01.txt AC 131 ms 120792 KiB
00_sample_02.txt AC 132 ms 120720 KiB
00_sample_03.txt AC 132 ms 120608 KiB
00_sample_04.txt AC 132 ms 120720 KiB
01_random_case_01.txt AC 133 ms 121204 KiB
01_random_case_02.txt AC 135 ms 122000 KiB
01_random_case_03.txt AC 133 ms 121228 KiB
01_random_case_04.txt AC 135 ms 121600 KiB
01_random_case_05.txt AC 134 ms 121832 KiB
01_random_case_06.txt AC 134 ms 121620 KiB
01_random_case_07.txt AC 134 ms 121828 KiB
01_random_case_08.txt AC 134 ms 121928 KiB
01_random_case_09.txt AC 134 ms 121332 KiB
01_random_case_10.txt AC 134 ms 121556 KiB
01_random_case_11.txt AC 134 ms 121808 KiB
01_random_case_12.txt AC 136 ms 121960 KiB
01_random_case_13.txt AC 137 ms 121832 KiB
01_random_case_14.txt AC 136 ms 121508 KiB
01_random_case_15.txt AC 134 ms 121752 KiB
01_random_case_16.txt AC 134 ms 121316 KiB
02_simple_case_01.txt AC 134 ms 121840 KiB
02_simple_case_02.txt AC 135 ms 121904 KiB
02_simple_case_03.txt AC 135 ms 122032 KiB
02_simple_case_04.txt AC 135 ms 121980 KiB
02_simple_case_05.txt AC 135 ms 121952 KiB
02_simple_case_06.txt AC 135 ms 121988 KiB
02_simple_case_07.txt AC 135 ms 122116 KiB
02_simple_case_08.txt AC 135 ms 121884 KiB
02_simple_case_09.txt AC 135 ms 122004 KiB
02_simple_case_10.txt AC 135 ms 121956 KiB
02_simple_case_11.txt AC 135 ms 121952 KiB
02_simple_case_12.txt AC 135 ms 121956 KiB
03_max_case_01.txt AC 137 ms 121888 KiB
03_max_case_02.txt AC 135 ms 121956 KiB
03_max_case_03.txt AC 135 ms 121928 KiB
03_max_case_04.txt AC 135 ms 121932 KiB
03_max_case_05.txt AC 135 ms 122004 KiB
03_max_case_06.txt AC 135 ms 121856 KiB
03_max_case_07.txt AC 135 ms 122044 KiB
03_max_case_08.txt AC 135 ms 121956 KiB
03_max_case_09.txt AC 135 ms 122000 KiB
03_max_case_10.txt AC 135 ms 121856 KiB
03_max_case_11.txt AC 135 ms 122032 KiB
03_max_case_12.txt AC 135 ms 122092 KiB
03_max_case_13.txt AC 135 ms 121976 KiB
03_max_case_14.txt AC 135 ms 121992 KiB
03_max_case_15.txt AC 135 ms 121956 KiB
03_max_case_16.txt AC 135 ms 122028 KiB
03_max_case_17.txt AC 135 ms 122004 KiB
03_max_case_18.txt AC 135 ms 122004 KiB
03_max_case_19.txt AC 135 ms 122032 KiB
03_max_case_20.txt AC 135 ms 121940 KiB
04_corner_01.txt AC 195 ms 121252 KiB
04_corner_02.txt AC 196 ms 120972 KiB
04_corner_03.txt AC 154 ms 121916 KiB
04_corner_04.txt AC 201 ms 121968 KiB
04_corner_05.txt AC 151 ms 121252 KiB
04_corner_06.txt AC 139 ms 121180 KiB
05_handmade_01.txt AC 132 ms 120716 KiB
05_handmade_02.txt AC 132 ms 120720 KiB
05_handmade_03.txt AC 132 ms 120844 KiB
05_handmade_04.txt AC 205 ms 122048 KiB
05_handmade_05.txt AC 135 ms 121888 KiB