結果

問題 No.3396 ChRisTmas memory
コンテスト
ユーザー KumaTachiRen
提出日時 2025-11-12 23:29:48
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,126 ms / 4,000 ms
コード長 1,138 bytes
コンパイル時間 1,195 ms
コンパイル使用メモリ 127,292 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-02 23:33:08
合計ジャッジ時間 14,257 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <ranges>
using namespace std;
#include <atcoder/modint>
using mint = atcoder::modint;

int main() {
  int q;
  cin >> q;
  vector<int> M{1}, Y{0};
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int m, r;
      cin >> m >> r;
      if (Y.back() < 0)
        M.push_back(m), Y.push_back(-1);
      else {
    	mint::set_mod(m);
        mint p = 1, y = r;
        for (auto [m_, y_] : views::zip(M, Y)) y -= y_ * p, p *= m_;
        auto [g, x] = atcoder::internal::inv_gcd(p.val(), m);
        if (y.val() % g != 0) {
          M.push_back(1), Y.push_back(-1);
        } else {
          m /= g;
          M.push_back(m);
          Y.push_back((y.val() / g) * x % m);
        }
      }
    } else if (t == 2) {
      int k;
      cin >> k;
      while (k--) M.pop_back(), Y.pop_back();
    } else {
      int m;
      cin >> m;
      if (Y.back() < 0) {
      	cout << -1 << "\n";
      } else {
        mint::set_mod(m);
        mint x = 0;
        for (auto [m_, y_] : views::zip(M, Y) | views::reverse) x = x * m_ + y_;
        cout << x.val() << "\n";
      }
    }
  }
}
0