結果

問題 No.2589 Prepare Integers
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-12-17 00:42:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 11,098 bytes
コンパイル時間 1,321 ms
コンパイル使用メモリ 121,504 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-17 00:42:08
合計ジャッジ時間 4,052 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 2 ms
6,676 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 8 ms
6,676 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 7 ms
6,676 KB
testcase_24 AC 7 ms
6,676 KB
testcase_25 AC 7 ms
6,676 KB
testcase_26 AC 8 ms
6,676 KB
testcase_27 AC 7 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;

#define ModInt ModIntRunTime
////////////////////////////////////////////////////////////////////////////////
struct ModInt {
  static unsigned M;
  unsigned x;
  ModInt() : x(0U) {}
  ModInt(unsigned x_) : x(x_ % M) {}
  ModInt(unsigned long long x_) : x(x_ % M) {}
  ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
unsigned ModInt::M;
////////////////////////////////////////////////////////////////////////////////
#undef ModInt


// a x + b y = g
int gojo(int a, int b, int &x, int &y) {
  if (b) {
    const int g = gojo(b, a % b, y, x);
    y -= (a / b) * x;
    return g;
  } else {
    x = 1;
    y = 0;
    return a;
  }
}


int D, M;

// generating <a[0], ..., a[D - 1]>
// a: upper triangular
// a[i][i] = 0  or  a[i][i] | M
constexpr int MAX_D = 110;
ModIntRunTime a[MAX_D][MAX_D];
void init() {
  for (int i = 0; i < D; ++i) {
    fill(a[i], a[i] + D, 0);
  }
}
void add(vector<ModIntRunTime> bs) {  // call by value
  for (int i = 0; i < D; ++i) if (bs[i].x) {
    const int a0 = a[i][i].x ? a[i][i].x : M;
    int x, y;
    const int g = gojo(a0, bs[i].x, x, y);
    const int z = -(int)bs[i].x / g, w = a0 / g;
    // [ g ] = [ x  y ] [ a ]
    // [ 0 ]   [ z  w ] [ b ]
    a[i][i] = g;
    for (int j = i + 1; j < D; ++j) {
      const ModIntRunTime t = x * a[i][j] + y * bs[j];
      bs[j] = z * a[i][j] + w * bs[j];
      a[i][j] = t;
    }
  }
}
bool check(vector<ModIntRunTime> bs) {  // call by value
  for (int i = 0; i < D; ++i) if (bs[i]) {
    if (!a[i][i].x || bs[i].x % a[i][i].x != 0) {
      return false;
    }
    const int x = bs[i].x / a[i][i].x;
    for (int j = i + 1; j < D; ++j) {
      bs[j] -= x * a[i][j];
    }
  }
  return true;
}
vector<ModIntRunTime> lexMax() {
  vector<ModIntRunTime> bs(D, 0);
  for (int i = 0; i < D; ++i) if (a[i][i].x) {
    const int x = (M - 1 - bs[i].x) / a[i][i].x;
    for (int j = i; j < D; ++j) {
      bs[j] += x * a[i][j];
    }
  }
  return bs;
}
template <class T> T count() {
  T prod = 1;
  for (int i = 0; i < D; ++i) if (a[i][i].x) {
    prod *= (M / a[i][i].x);
  }
  return prod;
}

int main() {
  int Q;
  for (; ~scanf("%d%d", &M, &Q); ) {
assert(M>=1LL<<24);
    ModIntRunTime::M = M;
    D = 0;
    for (Int x = 1001001001LL; x; x /= M) ++D;
cerr<<"M = "<<M<<", D = "<<D<<endl;
    init();
    for (int q = 0; q < Q; ++q) {
// for(int i=0;i<D;++i)if(a[i][i].x){cerr<<i<<": ";pv(a[i],a[i]+D);}
      int typ;
      scanf("%d", &typ);
      switch (typ) {
        case 1: {
          Int X;
          scanf("%lld", &X);
          vector<ModIntRunTime> V(D);
          for (int i = 0; i < D; ++i) {
            V[i] = X;
            X /= M;
          }
          assert(!X);
          reverse(V.begin(), V.end());
// cerr<<COLOR("33")<<"add "<<V<<COLOR()<<endl;
          add(V);
        } break;
        case 2: {
          Int K;
          scanf("%lld", &K);
          --K;
// cerr<<COLOR("33")<<"kth "<<K<<COLOR()<<endl;
          vector<Int> prods(D + 1);
          prods[D] = 1;
          for (int i = D; --i >= 0; ) {
            prods[i] = (a[i][i].x ? (M / a[i][i].x) : 1) * prods[i + 1];
          }
          if (K < prods[0]) {
            vector<ModIntRunTime> V(D, 0);
            Int k = K;
            for (int i = 0; i < D; ++i) if (a[i][i].x) {
              const Int r = k / prods[i + 1];
              k %= prods[i + 1];
              const ModIntRunTime s = r - ((Int)V[i].x / (Int)a[i][i].x);
              for (int j = i; j < D; ++j) {
                V[j] += s * a[i][j];
              }
            }
            Int ans = 0;
            for (int i = 0; i < D; ++i) {
              (ans *= M) += V[i].x;
            }
            printf("%lld\n", ans);
          } else {
            puts("-1");
          }
        } break;
        case 3: {
          Int X;
          scanf("%lld", &X);
          ++X;
          vector<ModIntRunTime> V(D);
          for (int i = 0; i < D; ++i) {
            V[i] = X;
            X /= M;
          }
          reverse(V.begin(), V.end());
// cerr<<COLOR("33")<<"rank "<<X<<" "<<V<<COLOR()<<endl;
          vector<__int128> prods(D + 1);
          prods[D] = 1;
          for (int i = D; --i >= 0; ) {
            prods[i] = (a[i][i].x ? (M / a[i][i].x) : 1) * prods[i + 1];
          }
          Int ans = 0;
          if (X) {
            ans += prods[0];
          } else {
            vector<ModIntRunTime> W(D, 0);
            for (int i = 0; i < D; ++i) {
              if (a[i][i].x) {
                const Int w = (Int)W[i].x % (Int)a[i][i].x;
                const Int q = (Int)(V[i].x - w) / (Int)a[i][i].x;
                const Int r = (Int)(V[i].x - w) % (Int)a[i][i].x;
// cerr<<"  W = "<<W<<", i = "<<i<<", w = "<<w<<", q = "<<q<<", r = "<<r<<endl;
                if (r) {
                  ans += (q + 1) * prods[i + 1];
                  break;
                } else {
                  ans += q * prods[i + 1];
                  for (int j = i; j < D; ++j) {
                    W[j] += q * a[i][j];
                  }
                }
              } else {
                if (V[i]) {
                  ans += prods[i + 1];
                  break;
                }
              }
            }
          }
          printf("%lld\n", ans);
        } break;
        default: assert(false);
      }
    }
cerr<<string(80,'=')<<endl;
  }
  return 0;
}
0