結果
問題 | No.2589 Prepare Integers |
ユーザー | KumaTachiRen |
提出日時 | 2023-12-07 16:54:18 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 3,062 bytes |
コンパイル時間 | 4,748 ms |
コンパイル使用メモリ | 266,108 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-27 07:50:41 |
合計ジャッジ時間 | 7,698 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | WA | - |
testcase_04 | AC | 658 ms
5,376 KB |
testcase_05 | AC | 268 ms
5,376 KB |
testcase_06 | AC | 135 ms
5,376 KB |
testcase_07 | AC | 99 ms
5,376 KB |
testcase_08 | AC | 20 ms
5,376 KB |
testcase_09 | AC | 9 ms
5,376 KB |
testcase_10 | AC | 9 ms
5,376 KB |
testcase_11 | AC | 10 ms
5,376 KB |
testcase_12 | AC | 175 ms
5,376 KB |
testcase_13 | AC | 115 ms
5,376 KB |
testcase_14 | AC | 85 ms
5,376 KB |
testcase_15 | AC | 84 ms
5,376 KB |
testcase_16 | AC | 73 ms
5,376 KB |
testcase_17 | AC | 27 ms
5,376 KB |
testcase_18 | AC | 20 ms
5,376 KB |
testcase_19 | AC | 14 ms
5,376 KB |
testcase_20 | AC | 10 ms
5,376 KB |
testcase_21 | AC | 10 ms
5,376 KB |
testcase_22 | AC | 10 ms
5,376 KB |
testcase_23 | AC | 10 ms
5,376 KB |
testcase_24 | AC | 10 ms
5,376 KB |
testcase_25 | AC | 10 ms
5,376 KB |
testcase_26 | AC | 10 ms
5,376 KB |
testcase_27 | AC | 6 ms
5,376 KB |
ソースコード
#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) { if (basis[i] == 0) continue; ll u = msd[i]; ll v = ans / powk[i]; ans = op(ans, op_pow(basis[i], x / cnt[i] - v / u)); x %= cnt[i]; } cout << ans << "\n"; } void query3(ll x) { x++; ll ans = 0, y = 0; rrep(i, 0, lg) { if (basis[i] == 0) { ll u = x / powk[i] % k, v = y / powk[i] % k; if (u == v) continue; if (u > v) ans += cnt[i]; break; } else { ll d = y / powk[i] % k; ll u = msd[i]; y = op(y, op_pow(basis[i], -(d / u))); d %= u; ll v = x / powk[i] % k - d; ans += (v + u - 1) / u * cnt[i]; if (v % u) break; y = op(y, op_pow(basis[i], v / u)); } } 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); } }