This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "lib/debug.h"
#else
#define debug(...) 0
#endif
template<int Rt, int Md>
struct Mi {
static constexpr uint32_t get_r() {
uint32_t iv = Md;
for (uint32_t i = 0; i != 4; ++i)
iv *= 2 - Md * iv;
return iv;
}
static constexpr uint32_t r1 = -get_r(), r2 = -uint64_t(Md) % Md;
uint32_t x;
Mi() = default;
~Mi() = default;
constexpr Mi(uint32_t x) : x(rd(uint64_t(x) * r2)) {}
constexpr Mi(const Mi &r) : x(r.x) {}
static constexpr int root() {
return Rt;
}
static constexpr int mod() {
return Md;
}
static constexpr uint32_t rd(uint64_t x) {
return (x + (uint64_t(uint32_t(x) * r1) * Md)) >> 32;
}
constexpr uint32_t get() const {
uint32_t r = rd(x);
return r - (Md & -(r >= Md));
}
explicit constexpr operator int32_t() const {
return int32_t(get());
}
constexpr Mi &operator=(const Mi &r) {
return x = r.x, *this;
}
constexpr Mi operator-() const {
Mi r;
return r.x = (Md << 1 & -(x != 0)) - x, r;
}
constexpr Mi inv() const {
return bp(-1);
}
constexpr Mi &operator+=(const Mi &r) {
return x += r.x - (Md << 1), x += Md << 1 & -(int32_t(x) < 0), *this;
}
constexpr Mi &operator-=(const Mi &r) {
return x -= r.x, x += Md << 1 & -(int32_t(x) < 0), *this;
}
constexpr Mi &operator*=(const Mi &r) {
return x = rd(uint64_t(x) * r.x), *this;
}
constexpr Mi &operator/=(const Mi &r) {
return this->operator*=(r.inv());
}
friend Mi operator+(const Mi &l, const Mi &r) {
return Mi(l) += r;
}
friend Mi operator-(const Mi &l, const Mi &r) {
return Mi(l) -= r;
}
friend Mi operator*(const Mi &l, const Mi &r) {
return Mi(l) *= r;
}
friend Mi operator/(const Mi &l, const Mi &r) {
return Mi(l) /= r;
}
friend std::istream &operator>>(std::istream &is, Mi &r) {
return is >> r.x, r.x = rd(uint64_t(r.x) * r2), is;
}
friend std::ostream &operator<<(std::ostream &os, const Mi &r) {
return os << r.get();
}
constexpr Mi bp(int64_t k) const {
if ((k %= Md - 1) < 0)
k += Md - 1;
Mi p(1), a(*this);
for ( ; k != 0; k >>= 1, a *= a)
if (k & 1)
p *= a;
return p;
}
};
typedef Mi<3, (int) 1e9 + 7> mi;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int p;
cin >> p;
int t;
cin >> t;
if (p == 1)
while (t--) {
string s;
cin >> s;
int l = 0, r = 0, yes = 1;
for (char c : s)
if (c == '(')
l += 1, r += 2;
else {
l -= 2, r -= 1;
l = max(l, 0);
if (r < 0)
yes = 0;
}
yes &= l <= 0 && r >= 0;
if (!yes) {
cout << "impossible" << '\n';
continue;
}
int n = s.size(), o = 0, i = n - 1;
string t(n, 'G');
for (i = n - 1; r > 0 && i >= 0; i--, r--)
if (s[i] == '(')
t[i] = "RB"[o ^= 1];
o = 0;
while (i >= 0) {
if (s[i] == ')')
t[i] = "RB"[o ^= 1];
i--;
}
cout << t << '\n';
}
else {
const int N = 300, L = 200, R = 400;
static mi f[L + 1][R + 1], g[L + 1][R + 1], h[N + 1];
f[0][0] = 1;
for (int t = 1; t <= N; t++) {
memset(g, 0, sizeof g);
for (int l = 0; l <= L; l++)
for (int r = 0; r <= R; r++) {
if (l + 1 <= L && r + 2 <= R)
g[l + 1][r + 2] += f[l][r];
if (r - 1 >= 0)
g[max(0, l - 2)][r - 1] += f[l][r];
}
swap(f, g);
for (int i = 0; i <= R; i++)
h[t] += f[0][i];
}
while (t--) {
int n;
cin >> n;
cout << h[n] << '\n';
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |