結果

問題 No.3396 ChRisTmas memory
コンテスト
ユーザー lif4635
提出日時 2025-12-03 06:12:50
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 3,045 bytes
コンパイル時間 12,057 ms
コンパイル使用メモリ 526,140 KB
実行使用メモリ 375,668 KB
最終ジャッジ日時 2025-12-03 06:13:40
合計ジャッジ時間 44,166 ms
ジャッジサーバーID
(参考情報)
judge1 / 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) {
    if (b == 0) {
        x = 1; y = 0;
        return a;
    }
    Int d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
    return d;
}

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 m2_div_g = m2 / g;
    
    Int tmp = (r2 - r1) / g;
    Int k = (tmp * p) % m2_div_g;
    
    Int r = (r1 + m1 * k) % lcm;

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

int main() {

    int Q; cin >> Q;
    vector<Int> M(Q + 5, 1);
    vector<Int> R(Q + 5, 0);

    M[0] = 1; 
    R[0] = 0;
    
    int idx = 1;

    for (int i = 0; i < Q; ++i) {
        int t; cin >> t;
        if (t == 1) {
            ll m, r; cin >> m >> r;
            if (M[idx - 1] != 0) {
                pair<Int, Int> res = crt(R[idx - 1], M[idx - 1], r, m);
                R[idx] = res.first;
                M[idx] = res.second;
            } else {
                M[idx] = 0;
            }
            idx++;
        }
        else if (t == 2) {
            int k; cin >> k;
            idx -= k;
        }
        else {
            ll m; cin >> m;
            if (M[idx - 1] != 0) {
                cout << (R[idx - 1] % m) << "\n";
            } else {
                cout << "-1\n";
            }
        }
    }

    return 0;
}
0