Submission #43441423
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
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, long long>::value, Modular>::type& operator*=(const Modular& rhs) {
long long q = static_cast<long long>(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())); }
friend const Type& abs(const Modular& x) { return x.value; }
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 V, typename U>
friend V& operator>>(V& 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, typename U> Modular<T> operator^(const Modular<T>& lhs, U rhs) { return power(lhs, rhs); };
template <typename T>
bool IsZero(const Modular<T>& number) {
return number() == 0;
}
template <typename T>
string to_string(const Modular<T>& number) {
return to_string(number());
}
// U == std::ostream? but done this way because of fastoutput
template <typename U, typename T>
U& operator<<(U& stream, const Modular<T>& number) {
return stream << number();
}
// U == std::istream? but done this way because of fastinput
template <typename U, typename T>
U& operator>>(U& stream, Modular<T>& number) {
typename common_type<typename Modular<T>::Type, long long>::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>>;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string t;
cin >> t;
vector<Mint> f(2 * n + 1), g(2 * n + 1);
f[n] = 1;
for (auto c : t) {
vector<Mint> new_f(2 * n + 1), new_g(2 * n + 1);
for (int i = 0; i <= 2 * n; ++i) {
if (c != '-' && i + 1 <= 2 * n) {
new_f[i + 1] += f[i];
new_g[i + 1] += g[i] + f[i] * abs(n - (i + 1));
}
if (c != '+' && i - 1 >= 0) {
new_f[i - 1] += f[i];
new_g[i - 1] += g[i] + f[i] * abs(n - (i - 1));
}
}
f.swap(new_f);
g.swap(new_g);
}
auto ans = g[n];
cout << ans << "\n";
return 0;
}
Submission Info
| Submission Time |
|
| Task |
D - 1D Coulomb |
| User |
nima_aryan |
| Language |
C++ (GCC 9.2.1) |
| Score |
600 |
| Code Size |
7622 Byte |
| Status |
AC |
| Exec Time |
267 ms |
| Memory |
3704 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
600 / 600 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
sample-01.txt, sample-02.txt |
| All |
in-01.txt, in-02.txt, in-03.txt, in-04.txt, in-05.txt, in-06.txt, in-07.txt, in-08.txt, in-09.txt, in-10.txt, in-11.txt, in-12.txt, in-13.txt, in-14.txt, in-15.txt, in-16.txt, in-17.txt, in-18.txt, in-19.txt, in-20.txt, in-21.txt, in-22.txt, in-23.txt, in-24.txt, in-25.txt, in-26.txt, in-27.txt, in-28.txt, in-29.txt, in-30.txt, in-31.txt, in-32.txt, in-33.txt, in-34.txt, in-35.txt, in-36.txt, in-37.txt, in-38.txt, in-39.txt, in-40.txt, sample-01.txt, sample-02.txt |
| Case Name |
Status |
Exec Time |
Memory |
| in-01.txt |
AC |
8 ms |
3540 KiB |
| in-02.txt |
AC |
3 ms |
3584 KiB |
| in-03.txt |
AC |
2 ms |
3568 KiB |
| in-04.txt |
AC |
2 ms |
3428 KiB |
| in-05.txt |
AC |
2 ms |
3428 KiB |
| in-06.txt |
AC |
2 ms |
3584 KiB |
| in-07.txt |
AC |
2 ms |
3592 KiB |
| in-08.txt |
AC |
2 ms |
3568 KiB |
| in-09.txt |
AC |
267 ms |
3688 KiB |
| in-10.txt |
AC |
253 ms |
3592 KiB |
| in-11.txt |
AC |
255 ms |
3624 KiB |
| in-12.txt |
AC |
253 ms |
3692 KiB |
| in-13.txt |
AC |
212 ms |
3676 KiB |
| in-14.txt |
AC |
213 ms |
3692 KiB |
| in-15.txt |
AC |
214 ms |
3644 KiB |
| in-16.txt |
AC |
174 ms |
3692 KiB |
| in-17.txt |
AC |
177 ms |
3692 KiB |
| in-18.txt |
AC |
175 ms |
3528 KiB |
| in-19.txt |
AC |
250 ms |
3568 KiB |
| in-20.txt |
AC |
249 ms |
3624 KiB |
| in-21.txt |
AC |
249 ms |
3620 KiB |
| in-22.txt |
AC |
176 ms |
3572 KiB |
| in-23.txt |
AC |
213 ms |
3560 KiB |
| in-24.txt |
AC |
172 ms |
3636 KiB |
| in-25.txt |
AC |
171 ms |
3688 KiB |
| in-26.txt |
AC |
154 ms |
3616 KiB |
| in-27.txt |
AC |
118 ms |
3696 KiB |
| in-28.txt |
AC |
2 ms |
3588 KiB |
| in-29.txt |
AC |
2 ms |
3488 KiB |
| in-30.txt |
AC |
1 ms |
3624 KiB |
| in-31.txt |
AC |
2 ms |
3468 KiB |
| in-32.txt |
AC |
1 ms |
3572 KiB |
| in-33.txt |
AC |
2 ms |
3592 KiB |
| in-34.txt |
AC |
238 ms |
3640 KiB |
| in-35.txt |
AC |
220 ms |
3668 KiB |
| in-36.txt |
AC |
176 ms |
3676 KiB |
| in-37.txt |
AC |
230 ms |
3704 KiB |
| in-38.txt |
AC |
137 ms |
3684 KiB |
| in-39.txt |
AC |
82 ms |
3608 KiB |
| in-40.txt |
AC |
94 ms |
3568 KiB |
| sample-01.txt |
AC |
2 ms |
3584 KiB |
| sample-02.txt |
AC |
2 ms |
3544 KiB |