QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#761641#9551. The Emperorucup-team5243TL 0ms3592kbC++174.5kb2024-11-19 04:38:242024-11-19 04:38:26

Judging History

你现在查看的是最新测评结果

  • [2024-11-19 04:38:26]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3592kb
  • [2024-11-19 04:38:24]
  • 提交

answer

#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
const i64 INF = 1001001001001001001;
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
using namespace std;

#include <utility>

#include <cassert>
namespace nachia{

// ax + by = gcd(a,b)
// return ( x, - )
std::pair<long long, long long> ExtGcd(long long a, long long b){
    long long x = 1, y = 0;
    while(b){
        long long u = a / b;
        std::swap(a-=b*u, b);
        std::swap(x-=y*u, y);
    }
    return std::make_pair(x, a);
}

} // namespace nachia

namespace nachia{

template<unsigned int MOD>
struct StaticModint{
private:
    using u64 = unsigned long long;
    unsigned int x;
public:

    using my_type = StaticModint;
    template< class Elem >
    static Elem safe_mod(Elem x){
        if(x < 0){
            if(0 <= x+MOD) return x + MOD;
            return MOD - ((-(x+MOD)-1) % MOD + 1);
        }
        return x % MOD;
    }

    StaticModint() : x(0){}
    StaticModint(const my_type& a) : x(a.x){}
    StaticModint& operator=(const my_type&) = default;
    template< class Elem >
    StaticModint(Elem v) : x(safe_mod(v)){}
    unsigned int operator*() const noexcept { return x; }
    my_type& operator+=(const my_type& r) noexcept { auto t = x + r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
    my_type operator+(const my_type& r) const noexcept { my_type res = *this; return res += r; }
    my_type& operator-=(const my_type& r) noexcept { auto t = x + MOD - r.x; if(t >= MOD) t -= MOD; x = t; return *this; }
    my_type operator-(const my_type& r) const noexcept { my_type res = *this; return res -= r; }
    my_type operator-() const noexcept { my_type res = *this; res.x = ((res.x == 0) ? 0 : (MOD - res.x)); return res; }
    my_type& operator*=(const my_type& r)noexcept { x = (u64)x * r.x % MOD; return *this; }
    my_type operator*(const my_type& r) const noexcept { my_type res = *this; return res *= r; }
    my_type pow(unsigned long long i) const noexcept {
        my_type a = *this, res = 1;
        while(i){ if(i & 1){ res *= a; } a *= a; i >>= 1; }
        return res;
    }
    my_type inv() const { return my_type(ExtGcd(x, MOD).first); }
    unsigned int val() const noexcept { return x; }
    static constexpr unsigned int mod() { return MOD; }
    static my_type raw(unsigned int val) noexcept { auto res = my_type(); res.x = val; return res; }
    my_type& operator/=(const my_type& r){ return operator*=(r.inv()); }
    my_type operator/(const my_type& r) const { return operator*(r.inv()); }
};

} // namespace nachia

void testcase(){
    using Modint = nachia::StaticModint<998244353>;
    i64 N; cin >> N;
    vector<i64> A(N+1), X(N+1), B(N+1), Y(N+1);
    rep(i,N){
        string s; cin >> s;
        if(s == "HALT;"){
            A[i] = -1;
            X[i] = i;
        } else {
            i64 a; cin >> a;
            cin >> s;
            i64 b; cin >> b;
            cin >> s;
            A[i] = a - 1;
            X[i] = b - 1;
        }
        {
            cin >> s;
            i64 a; cin >> a;
            cin >> s;
            i64 b; cin >> b;
            B[i] = a - 1;
            Y[i] = b - 1;
        }
    }
    B[N] = -1;
    Y[N] = 0;
    vector<i64> F = Y;
    vector<Modint> len(N+1, 1);
    vector<i64> Z(N+1, -1);
    vector<vector<i64>> que(N+1);
    vector<i64> st;
    rep(i,N+1) st.push_back(i);
    while(st.size()){
        i64 s = st.back(); st.pop_back();
        //cout << "check " << s << endl;
        while(true){
            if(A[F[s]] == B[s]){
                //cout << "determine " << s << endl;
                len[s] += 1;
                Z[s] = X[F[s]];
                for(auto a : que[s]) st.push_back(a);
                que[s].clear();
                break;
            } else if(Z[F[s]] >= 0){
                len[s] += len[F[s]];
                F[s] = Z[F[s]];
            } else {
                que[F[s]].push_back(s);
                break;
            }
        }
    }

    if(Z[N] == -1){ cout << "-1\n"; return; }
    Modint ans = len[N] - 1;
    cout << ans.val() << '\n';
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    testcase();
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3592kb

input:

1
HALT; PUSH 1 GOTO 1

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3568kb

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: -100
Time Limit Exceeded

input:

1
POP 1 GOTO 1; PUSH 1 GOTO 1

output:


result: