結果

問題 No.2589 Prepare Integers
ユーザー fdironiafdironia
提出日時 2023-12-26 03:19:09
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,338 ms / 2,000 ms
コード長 2,641 bytes
コンパイル時間 1,416 ms
コンパイル使用メモリ 121,320 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-12-26 03:19:17
合計ジャッジ時間 7,637 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 AC 2 ms
6,676 KB
testcase_04 AC 1,338 ms
6,676 KB
testcase_05 AC 637 ms
6,676 KB
testcase_06 AC 325 ms
6,676 KB
testcase_07 AC 246 ms
6,676 KB
testcase_08 AC 79 ms
6,676 KB
testcase_09 AC 41 ms
6,676 KB
testcase_10 AC 50 ms
6,676 KB
testcase_11 AC 57 ms
6,676 KB
testcase_12 AC 434 ms
6,676 KB
testcase_13 AC 289 ms
6,676 KB
testcase_14 AC 215 ms
6,676 KB
testcase_15 AC 221 ms
6,676 KB
testcase_16 AC 183 ms
6,676 KB
testcase_17 AC 94 ms
6,676 KB
testcase_18 AC 70 ms
6,676 KB
testcase_19 AC 64 ms
6,676 KB
testcase_20 AC 41 ms
6,676 KB
testcase_21 AC 47 ms
6,676 KB
testcase_22 AC 50 ms
6,676 KB
testcase_23 AC 56 ms
6,676 KB
testcase_24 AC 58 ms
6,676 KB
testcase_25 AC 59 ms
6,676 KB
testcase_26 AC 58 ms
6,676 KB
testcase_27 AC 33 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <vector>
#include <tuple>
#include <algorithm>

using namespace std;

using ll = long long;
using vi = vector<int>;
using v2i = vector<vi>;
using t4i = tuple<int, int, int, int>;

constexpr int X = 1e9;

t4i exgcd(int a, int b) {
    if (b == 0) {
        return {1, 0, 0, 1};
    }

    auto [k1, k2, k3, k4] = exgcd(b, a % b);
    return {k2, k1 - a / b * k2, k4, k3 - a / b * k4};
}

ll solve(const v2i &base, int k, ll x) {
    x--;
    int n = base.size();

    int xd[n];
    for (int i = 0; i < n; i++) {
        if (!base[i].empty()) {
            xd[i] = x % (k / base[i][i]);
            x /= k / base[i][i];
        }
    }

    if (x > 0) {
        return -1;
    }

    int ans[n];
    fill(ans, ans + n, 0);
    for (int i = n - 1; i >= 0; i--) {
        if (!base[i].empty()) {
            int t = ((xd[i] - ans[i] / base[i][i]) % k + k) % k;
            for (int j = 0; j < n; j++) {
                ans[j] = (ans[j] + 1ll * t * base[i][j]) % k;
            }
        }
    }

    ll t = 0;
    for (int i = n - 1; i >= 0; i--) {
        t = t * k + ans[i];
    }

    return t;
}

int main() {
    int k, q;
    cin >> k >> q;
    int n = log(X) / log(k) + 1;

    v2i base(n);

    while (q--) {
        int op;
        ll x;
        cin >> op >> x;

        if (op == 1) {
            int xd[n];
            for (int i = 0; i < n; i++) {
                xd[i] = x % k;
                x /= k;
            }

            for (int i = n - 1; i >= 0; i--) {
                if (xd[i] == 0) {
                    continue;
                }

                auto [k1, k2, k3, k4] = exgcd(xd[i], base[i].empty() ? k : base[i][i]);

                if (base[i].empty()) {
                    base[i] = vi(n);
                }

                int t[n];
                copy(base[i].begin(), base[i].end(), t);

                for (int j = 0; j < n; j++) {
                    base[i][j] = ((1ll * k1 * xd[j] + 1ll * k2 * t[j]) % k + k) % k;
                }
                for (int j = 0; j < n; j++) {
                    xd[j] = ((1ll * k3 * xd[j] + 1ll * k4 * t[j]) % k + k) % k;
                }
            }
        } else if (op == 2) {
            cout << solve(base, k, x) << endl;
        } else {
            ll l = 0, r = x + 2;
            while (r - l > 1) {
                ll mid = (l + r) / 2;

                ll t = solve(base, k, mid);
                if (t <= x && t != -1) {
                    l = mid;
                } else {
                    r = mid;
                }
            }

            cout << l << endl;
        }
    }

    return 0;
}
0