結果

問題 No.3396 ChRisTmas memory
コンテスト
ユーザー GOTKAKO
提出日時 2025-12-03 09:33:36
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,830 ms / 4,000 ms
コード長 7,149 bytes
コンパイル時間 2,911 ms
コンパイル使用メモリ 227,152 KB
実行使用メモリ 13,184 KB
最終ジャッジ日時 2025-12-03 09:34:06
合計ジャッジ時間 29,918 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

template<typename T>
bool chmin(T &a,T b){ //a>bならa=bに更新してtrue.
    if(a > b){a = b; return true;}
    else return false;
}
template<typename T> 
bool chmax(T &a,T b){ //a<bならa=bに更新してtrue.
    if(a < b){a = b; return true;}
    else return false;
}
template<typename T> 
T safemod(T a,T m){a %= m,a += m;return a>=m?a-m:a;} //return x = a mod m. 
template<typename T> 
T floor(T a,T b){ //return a/b切り下げ.
    if(b < 0) a *= -1,b *= -1;
    return a<0?(a+1)/b-1:a/b; 
}
template<typename T> 
T ceil(T a,T b){ //return a/b切り上げ.
    if(b < 0) a *= -1,b *= -1;
    return a>0?(a-1)/b+1:a/b; 
}
template<typename T> 
pair<T,T> invgcd(T a,T b){
    //return {gcd(a,b),x} (xa≡g(mod b))
    a = safemod(a,b);
    if(a == 0) return {b,0};
    T x = 0,y = 1,memob = b;
    while(a){
        T q = b/a;
        b -= a*q;
        swap(x,y); y -= q*x;
        swap(a,b);
    }
    if(x < 0) x += memob/b;
    return {b,x};
}
template<typename T>
bool isABmoreC(T a,T b,T c){return a>ceil(c,b);} //a*b=cはfalse a*b>c -> a>ceil(c/b)?.
template<typename T>
bool isABmoreC2(T a,T b,T c){return a>=ceil(c,b);} //a*b=cはtrue.
template<typename T>
bool isABlessC(T a,T b,T c){return a<floor(c,b);} //a*b=cはfalse.
template<typename T>
bool isABlessC2(T a,T b,T c){return a<=floor(c,b);} //a*b=cはtrue.
template<typename T>
T Kthpower(T a,T k){ //return a^k オーバーフローは考慮しない.
    T ret = 1;
    while(k){
        if(k&1) ret *= a;
        a *= a; k >>= 1;
    }
    return ret;
}
template<typename T>
pair<T,bool> Kthpower2(T a,T k){ //return {a^k,オーバーした?} オーバーフローは考慮する.
    T ret = 1,maxv = numeric_limits<T>::max();
    while(k){
        if(k&1){
            if(isABmoreC(ret,a,maxv)) return {-1,true};
            ret *= a;
        }
        if(k == 1) break;
        if(isABmoreC(a,a,maxv)) return {-1,true};
        a *= a; k >>= 1;
    }
    return {ret,false};
}
template<typename T>
T Kthroot(T a,T k){ //return floor(a^(1/k));
    assert(k > 0 && a >= 0);
    if(k == 1 || a <= 1) return a;
    T ret = pow(a,1.0/k);
    while(true){
        auto [check,over] = Kthpower2(ret+1,k);
        if(over || check > a) break;
        ret++;
    }    
    while(true){
        auto [check,over] = Kthpower2(ret,k);
        if(!over && check <= a) break;
        ret--;
    }    
    return ret;
}
template<typename T>
T powmod(T a,T b,T m){//a^b(mod m)を返す.
    assert(b >= 0);
    __int128_t ret = 1,p = a;
    while(b){
        if(b&1) ret = ret*p%m;
        p = p*p%m; b >>= 1;
    }
    return T(ret);
}
template<typename T>
T divmod(T a,T b,T m){//a/b(mod m)を返す 素数mod必須.
    return (T)((__int128_t)a*powmod(b,m-2,m)%m); 
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int Q; cin >> Q;
    vector<vector<pair<int,int>>> Query(Q+1);
    vector<vector<tuple<int,long long,long long>>> Graph(Q+1);
    vector<long long> answer(Q,-2);

    vector<int> V = {0},L = {1};
    V.reserve(100000),L.reserve(100000);
    const int B = 1'000'000'000;
    auto mul = [&](vector<int> &A,const long long x) -> void {
        if(x == B){A.insert(A.begin(),0); return;}
        long long bring = 0;
        for(auto &v : A){
            bring += v*x;
            v = bring%B,bring /= B;
        }
        if(bring) A.push_back(bring);
    };
    auto mul2 = [&](vector<int> A,const long long x) -> vector<int> {
        if(x == B){A.insert(A.begin(),0); return A;}
        long long bring = 0;
        for(auto &v : A){
            bring += v*x;
            v = bring%B,bring /= B;
        }
        if(bring) A.push_back(bring);
        return A;
    };
    auto div = [&](vector<int> &A,const long long x) -> void {
        if(x == B){A.erase(A.begin()); return;}
        long long bring = 0;
        for(int i=A.size(); i--;){
            bring += A.at(i);
            A.at(i) = bring/x,bring %= x,bring *= B;
        }
        if(A.back() == 0) A.pop_back();
    };
    auto mod = [&](const vector<int> &A,const int x) -> int {
        long long pb = 1,ret = 0;
        for(auto v : A) ret += v*pb%x,pb = pb*B%x;
        return ret%x;
    };
    auto add = [&](vector<int> &A,const vector<int> &A2) -> void {
        A.resize(max(A.size(),A2.size()));
        for(int i=0; i<A2.size(); i++) A.at(i) += A2.at(i);
        int bring = 0;
        for(auto &a : A){
            a += bring,bring = 0;
            if(a >= B) a -= B,bring = 1;
        }
        if(bring) A.push_back(bring);
    };


    {  
        int pos = 1;
        stack<int> st; st.push(0);
        for(int i=0; i<Q; i++){
            int t; cin >> t;
            if(t == 1){
                int m,r; cin >> m >> r;
                int par = st.top();
                Graph.at(par).push_back({pos,m,r});
                st.push(pos++);
            }
            else if(t == 2){
                int k; cin >> k;
                while(k--) st.pop();
            }
            else{
                int m; cin >> m;
                Query.at(st.top()).push_back({m,i});
            }
        }
    }
    {
        auto dfs = [&](auto dfs,int pos) -> bool {
            bool ret = Query.at(pos).size()>0;
            vector<tuple<int,long long,long long>> remake;
            for(auto [to,ig,ign] : Graph.at(pos)) if(dfs(dfs,to)) remake.push_back({to,ig,ign});
            Graph.at(pos) = remake;
            ret |= Graph.at(pos).size();
            return ret;
        };
        dfs(dfs,0);
    }
    {
        vector<int> siz(Q+1);
        auto dfs = [&](auto dfs,int pos) -> int {
            int ret = 1;
            for(auto [to,ig,ign] : Graph.at(pos)) ret += dfs(dfs,to);
            siz.at(pos) = ret; return ret;
        };
        for(auto &g : Graph) sort(g.begin(),g.end(),[&](auto &a,auto &b){return siz.at(get<0>(a))<siz.at(get<0>(b));});
    }
    {
        bool ok = true;
        auto dfs = [&](auto dfs,int pos,bool back = false) -> void {
            for(auto [m,qpos] : Query.at(pos)){
                if(ok) answer.at(qpos) = mod(V,m);
                else answer.at(qpos) = -1;
            }
            for(int i=0; i<Graph.at(pos).size(); i++){
                auto [to,m,r] = Graph.at(pos).at(i);
                bool last = i+1==Graph.at(pos).size();
                if(ok == false) dfs(dfs,to,back|(!last));
                else{
                    long long a = mod(L,m),D = mod(V,m);
                    D = (r-D+m)%m;
                    auto [g,x] = invgcd(a,m);
                    if(D%g) ok = false,dfs(dfs,to,back|(!last)),ok = true;
                    else{
                        D /= g,m /= g;
                        x = x*D%m;
                        vector<int> memo;
                        if(back || !last) memo = V;
                        add(V,mul2(L,x)),mul(L,m);
                        dfs(dfs,to,back|(!last));
                        if(back || !last) div(L,m),V = memo;
                    }
                }
            }
        };
        dfs(dfs,0);
    }
    for(auto a : answer){
        if(a == -2) continue;
        cout << a << "\n";
    }
}
0