結果

問題 No.2589 Prepare Integers
ユーザー KumaTachiRenKumaTachiRen
提出日時 2023-12-16 19:35:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,767 bytes
コンパイル時間 4,457 ms
コンパイル使用メモリ 267,132 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-16 23:30:25
合計ジャッジ時間 6,910 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;

struct Fast {
  Fast() {
    std::cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(10);
  }
} fast;

#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int(i) = (a); (i) < (int)(b); (i)++)
#define rrep(i, a, b) for (int(i) = (int)(b)-1; (i) >= (a); (i)--)
using vi = vector<int>;
using ll = long long;
using vl = vector<ll>;

ll safe_mod(ll a, ll b) {
  ll c = a % b;
  return c < 0 ? c + b : c;
}

ll ext_gcd(ll a, ll b, ll& x, ll& y) {
  for (ll u = y = 1, v = x = 0; a;) {
    ll q = b / a;
    swap(x -= q * u, u);
    swap(y -= q * v, v);
    swap(b -= q * a, a);
  }
  return b;
}

const ll A_MAX = 1e9;
int lg;
ll k;
vl powk, basis, cnt, msd;

ll op(ll a, ll b) {
  ll ret = 0, pow = 1;
  while (a | b) {
    ret += (a + b) % k * pow;
    a /= k;
    b /= k;
    pow *= k;
  }
  return ret;
}

ll op_pow(ll a, ll n) {
  if (n == 0) return 0;
  n = safe_mod(n, k);
  ll ret = 0, pow = 1;
  while (a) {
    ret += safe_mod(a % k * n, k) * pow;
    a /= k;
    pow *= k;
  }
  return ret;
}

void query1(ll x) {
  vl a{x};
  rrep(i, 0, lg) {
    ll g = msd[i];
    vl v(a.size()), prod(a.size() + 1);
    rep(j, 0, a.size()) {
      ll u = a[j] / powk[i];
      ext_gcd(g, u, prod[j], v[j]);
      g = g * prod[j] + u * v[j];
      prod[j] = safe_mod(prod[j], k);
      v[j] = safe_mod(v[j], k);
    }
    rrep(j, 1, a.size()) prod[j - 1] = safe_mod(prod[j - 1] * prod[j], k);
    ll t = op_pow(basis[i], prod[0]);
    prod[a.size()] = 1;
    rep(j, 0, a.size()) t = op(t, op_pow(a[j], v[j] * prod[j + 1] % k));

    rep(j, 0, a.size()) a[j] = op(a[j], op_pow(t, -a[j] / powk[i] / g));
    a.push_back(op(basis[i], op_pow(t, -msd[i] / g)));

    if (g != k) basis[i] = t, msd[i] = g;
  }
  rep(i, 0, lg) cnt[i + 1] = cnt[i] * (k / msd[i]);
}

void query2(ll x) {
  if (x > cnt[lg]) {
    cout << -1 << "\n";
    return;
  }
  x--;
  ll ans = 0;
  rrep(i, 0, lg) {
    ans = op(ans, op_pow(basis[i], x / cnt[i] - ans / powk[i] / msd[i]));
    x %= cnt[i];
  }
  cout << ans << "\n";
}

void query3(ll x) {
  x++;
  ll ans = 0, y = 0;
  rrep(i, 0, lg) {
    ll r = y / powk[i] % msd[i], d = x / powk[i] % k;
    ans += (d - r + msd[i] - 1) / msd[i] * cnt[i];
    if ((d - r) % msd[i]) break;
    y = op(y, op_pow(basis[i], (d - y / powk[i] % k) / msd[i]));
  }
  cout << ans << "\n";
}

int main() {
  int q;
  cin >> k >> q;

  powk = vl();
  for (ll p = 1; p <= A_MAX; p *= k) powk.push_back(p);
  lg = (int)powk.size();
  basis = vl(lg, 0);
  msd = vl(lg, k);
  cnt = vl(lg + 1, 1);

  while (q--) {
    int t;
    ll x;
    cin >> t >> x;
    if (t == 1) query1(x);
    if (t == 2) query2(x);
    if (t == 3) query3(x);
  }
}
0