QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#796108 | #9551. The Emperor | KKT89 | RE | 1ms | 3688kb | C++20 | 3.3kb | 2024-12-01 11:37:15 | 2024-12-01 11:37:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <int mod> struct static_modint {
using mint = static_modint;
int x;
static_modint() : x(0) {}
static_modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
mint &operator+=(const mint &rhs) {
if ((x += rhs.x) >= mod) x -= mod;
return *this;
}
mint &operator-=(const mint &rhs) {
if ((x += mod - rhs.x) >= mod) x -= mod;
return *this;
}
mint &operator*=(const mint &rhs) {
x = (int)(1LL * x * rhs.x % mod);
return *this;
}
mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); }
mint pow(long long n) const {
mint _x = *this, r = 1;
while (n) {
if (n & 1) r *= _x;
_x *= _x;
n >>= 1;
}
return r;
}
mint inv() const { return pow(mod - 2); }
mint operator+() const { return *this; }
mint operator-() const { return mint() - *this; }
friend mint operator+(const mint &lhs, const mint &rhs) { return mint(lhs) += rhs; }
friend mint operator-(const mint &lhs, const mint &rhs) { return mint(lhs) -= rhs; }
friend mint operator*(const mint &lhs, const mint &rhs) { return mint(lhs) *= rhs; }
friend mint operator/(const mint &lhs, const mint &rhs) { return mint(lhs) /= rhs; }
friend bool operator==(const mint &lhs, const mint &rhs) { return lhs.x == rhs.x; }
friend bool operator!=(const mint &lhs, const mint &rhs) { return lhs.x != rhs.x; }
friend ostream &operator<<(ostream &os, const mint &p) { return os << p.x; }
friend istream &operator>>(istream &is, mint &a) {
int64_t t;
is >> t;
a = static_modint<mod>(t);
return (is);
}
};
const unsigned int mod = 998244353;
using modint = static_modint<mod>;
modint mod_pow(ll n, ll x) { return modint(n).pow(x); }
modint mod_pow(modint n, ll x) { return n.pow(x); }
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<pair<int, int>> op1(n + 1), op2(n + 1);
for (int i = 1; i <= n; ++i) {
string str;
cin >> str;
if (str == "POP") {
cin >> op1[i].first;
cin >> str >> op1[i].second >> str;
} else {
op1[i] = {0, 0};
}
cin >> str >> op2[i].first >> str >> op2[i].second;
}
using P = pair<modint, int>;
vector<vector<P>> dp(n + 1, vector<P>(n + 1));
vector<vector<int>> used(n + 1, vector<int>(n + 1));
auto calc = [&](auto calc, int a, int x) -> P {
if (a == 0 and x == 0) {
return {0, 0};
}
if (used[a][x] & 2) {
return dp[a][x];
}
if (used[a][x] & 1) {
cout << -1 << '\n';
exit(0);
}
used[a][x] |= 1;
P res;
if (op1[x].first == a) {
res = {1, op1[x].second};
} else {
auto r1 = calc(calc, op2[x].first, op2[x].second);
auto r2 = calc(calc, a, r1.second);
res = {r1.first + r2.first + 1, r2.second};
}
used[a][x] |= 2;
return dp[a][x] = res;
};
auto res = calc(calc, 0, 1);
cout << res.first << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3640kb
input:
1 HALT; PUSH 1 GOTO 1
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3576kb
input:
5 POP 1 GOTO 2; PUSH 1 GOTO 2 HALT; PUSH 1 GOTO 3 POP 1 GOTO 4; PUSH 2 GOTO 4 POP 1 GOTO 2; PUSH 2 GOTO 4 HALT; PUSH 99 GOTO 4
output:
5
result:
ok 1 number(s): "5"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
1 POP 1 GOTO 1; PUSH 1 GOTO 1
output:
-1
result:
ok 1 number(s): "-1"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3688kb
input:
61 POP 62 GOTO 61; PUSH 30 GOTO 60 POP 1 GOTO 3; PUSH 62 GOTO 61 POP 2 GOTO 61; PUSH 62 GOTO 61 POP 4 GOTO 7; PUSH 2 GOTO 61 POP 62 GOTO 61; PUSH 3 GOTO 4 POP 62 GOTO 61; PUSH 3 GOTO 5 POP 5 GOTO 10; PUSH 3 GOTO 6 POP 62 GOTO 61; PUSH 4 GOTO 7 POP 62 GOTO 61; PUSH 4 GOTO 8 POP 6 GOTO 12; PUSH 4 GOTO...
output:
150994941
result:
ok 1 number(s): "150994941"
Test #5:
score: -100
Runtime Error
input:
60 POP 1 GOTO 2; PUSH 1 GOTO 1 POP 51 GOTO 3; PUSH 51 GOTO 2 POP 2 GOTO 4; PUSH 2 GOTO 1 POP 52 GOTO 5; PUSH 52 GOTO 4 POP 3 GOTO 6; PUSH 3 GOTO 1 POP 53 GOTO 7; PUSH 53 GOTO 6 POP 4 GOTO 8; PUSH 4 GOTO 1 POP 54 GOTO 9; PUSH 54 GOTO 8 POP 5 GOTO 10; PUSH 5 GOTO 1 POP 55 GOTO 11; PUSH 55 GOTO 10 POP ...