結果

問題 No.3396 ChRisTmas memory
コンテスト
ユーザー lif4635
提出日時 2025-12-03 09:19:38
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 3,165 bytes
コンパイル時間 9,572 ms
コンパイル使用メモリ 529,436 KB
実行使用メモリ 365,624 KB
最終ジャッジ日時 2025-12-03 09:20:32
合計ジャッジ時間 48,609 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30 MLE * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
// using mint = modint1000000007;
// using mint = double;
#endif

//define
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long , long long>;
using vi = vector<int>;
using vll = vector<long long>;

#define rep(i,l,r) for (int i = (int)(l); i < (int)(r); i++)
#define rrep(i,l,r) for (int i = (int)(r-1); i >= (int)(l); i--)
#define len(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define elif else if
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
const int inf = 1e9;
const long long infl = 1LL<<60;
const int mod = 998244353;
ll pow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T, class U> bool chmin(T& a, const U& b){ if(a > T(b)){ a = b; return 1; } return 0; }
template<class T, class U> bool chmax(T& a, const U& b){ if(a < T(b)){ a = b; return 1; } return 0; }
template <class T>
using spq = priority_queue<T, vector<T>, greater<T>>;

template<class T>istream& operator>>(istream& i, vector<T>& v) {for(int j = 0; j < (int)(v).size(); j++) i >> v[j]; return i;}
struct IoSetup {
    IoSetup() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
        cerr << fixed << setprecision(15);
    }
} iosetup;

#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using namespace boost::multiprecision;

using Int = cpp_int;

Int extgcd(Int a, Int b, Int &x, Int &y) {
    Int r0 = a, r1 = b;
    Int x0 = 1, x1 = 0;
    Int y0 = 0, y1 = 1;

    while (r1 != 0) {
        Int q = r0 / r1;
        
        Int r_next = r0 % r1;
        Int x_next = x0 - q * x1;
        Int y_next = y0 - q * y1;

        r0 = r1; r1 = r_next;
        x0 = x1; x1 = x_next;
        y0 = y1; y1 = y_next;
    }

    x = x0;
    y = y0;
    return r0;
}

pair<Int, Int> crt(Int r1, Int m1, Int r2, Int m2) {
    Int p, q;
    Int g = extgcd(m1, m2, p, q);

    if ((r2 - r1) % g != 0) {
        return {0, 0};
    }

    Int lcm = (m1 / g) * m2;
    Int k = (((r2 - r1) / g) * p) % (m2 / g);
    Int r = (r1 + m1 * k) % lcm;

    if (r < 0) r += lcm;
    
    return {r, lcm};
}

int main() {
    int Q; cin >> Q;
    vector<Int> R, M;
    R.pb(0); M.pb(1); 
    rep(i, 0, Q) {
        int t; cin >> t;
        if (t == 1) {
            ll m, r; cin >> m >> r;
            const auto& pr = R.back();
            const auto& pm = M.back();
            if (pm != 0) {
                auto [nr, nm] = crt(pr, pm, r, m);
                R.pb(nr); M.pb(nm);
            } else {
                R.pb(0); M.pb(0);
            }
        } else if (t == 2) {
            int k; cin >> k;
            R.resize(R.size() - k);
            M.resize(M.size() - k);
        } else {
            ll m; cin >> m;
            if (M.back() != 0) {
                cout << (R.back() % m) << "\n";
            } else {
                cout << "-1\n";
            }
        }
    }
    return 0;
}
0