結果

問題 No.551 夏休みの思い出(2)
ユーザー pekempeypekempey
提出日時 2017-07-29 01:14:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,557 ms / 4,000 ms
コード長 1,759 bytes
コンパイル時間 837 ms
コンパイル使用メモリ 85,104 KB
実行使用メモリ 206,372 KB
最終ジャッジ日時 2024-04-18 18:06:34
合計ジャッジ時間 70,446 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
6,812 KB
testcase_01 AC 96 ms
6,812 KB
testcase_02 AC 93 ms
6,944 KB
testcase_03 AC 93 ms
6,940 KB
testcase_04 AC 95 ms
6,944 KB
testcase_05 AC 96 ms
6,940 KB
testcase_06 AC 96 ms
6,940 KB
testcase_07 AC 96 ms
6,940 KB
testcase_08 AC 94 ms
6,944 KB
testcase_09 AC 95 ms
6,940 KB
testcase_10 AC 96 ms
6,940 KB
testcase_11 AC 93 ms
6,944 KB
testcase_12 AC 93 ms
6,940 KB
testcase_13 AC 95 ms
6,944 KB
testcase_14 AC 94 ms
6,940 KB
testcase_15 AC 94 ms
6,940 KB
testcase_16 AC 93 ms
6,944 KB
testcase_17 AC 222 ms
14,108 KB
testcase_18 AC 403 ms
31,080 KB
testcase_19 AC 395 ms
29,540 KB
testcase_20 AC 439 ms
30,944 KB
testcase_21 AC 659 ms
44,788 KB
testcase_22 AC 147 ms
6,944 KB
testcase_23 AC 173 ms
10,056 KB
testcase_24 AC 373 ms
23,904 KB
testcase_25 AC 210 ms
13,724 KB
testcase_26 AC 292 ms
23,272 KB
testcase_27 AC 3,144 ms
206,368 KB
testcase_28 AC 3,070 ms
206,368 KB
testcase_29 AC 2,529 ms
206,236 KB
testcase_30 AC 3,184 ms
206,368 KB
testcase_31 AC 2,572 ms
206,368 KB
testcase_32 AC 3,115 ms
206,368 KB
testcase_33 AC 3,098 ms
206,368 KB
testcase_34 AC 3,127 ms
206,320 KB
testcase_35 AC 2,697 ms
206,368 KB
testcase_36 AC 3,068 ms
206,236 KB
testcase_37 AC 3,557 ms
206,372 KB
testcase_38 AC 3,425 ms
206,372 KB
testcase_39 AC 3,140 ms
206,368 KB
testcase_40 AC 3,251 ms
206,364 KB
testcase_41 AC 3,014 ms
206,240 KB
testcase_42 AC 3,355 ms
206,244 KB
testcase_43 AC 3,107 ms
206,372 KB
testcase_44 AC 3,130 ms
206,368 KB
testcase_45 AC 3,226 ms
206,372 KB
testcase_46 AC 3,431 ms
206,236 KB
testcase_47 AC 94 ms
6,940 KB
testcase_48 AC 103 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>

int64_t modpow(int64_t a, int64_t b, int64_t m) {
    int64_t ret = 1;
    for (; b > 0; b >>= 1) {
        if (b & 1) ret = ret * a % m;
        a = a * a % m;
    }
    return ret;
}

int64_t modinv(int64_t a, int64_t p) {
    return modpow(a, p - 2, p);
}

int main() {
    int64_t p, r;
    std::cin >> p >> r;

    auto modulo = [&](int64_t a) {
        a %= p;
        if (a < 0) a += p;
        return a;
    };

    constexpr int S = 5000000;
    constexpr int T = (1000000000 + S - 1) / S;
    int64_t rs = 1;
    std::unordered_map<int64_t, int> baby;
    for (int i = 0; i < S; i++) {
        if (!baby.count(rs)) baby[rs] = i;
        rs = rs * r % p;
    }
    rs = modinv(rs, p);

    auto modlog = [&](int64_t a) -> int64_t {
        if (a == 0) return 1000000000;
        int64_t giant = 1;
        for (int i = 0; i < T; i++) {
            if (baby.count(a * giant % p)) return baby[a * giant % p] + i * S;
            giant = giant * rs % p;
        }
        return -1;
    };

    int q;
    std::cin >> q;
    while (q--) {
        int64_t a, b, c;
        scanf("%lld %lld %lld", &a, &b, &c);
        int64_t d = modulo(b * b - 4 * a * c);
        int64_t e = modlog(d);
        if (e == -1 || e % 2 == 1) {
            puts("-1");
        } else {
            d = e == 1000000000 ? 0LL : modpow(r, e / 2, p);
            int64_t x1 = modulo((-b + d) * modinv(2 * a % p, p));
            int64_t x2 = modulo((-b - d) * modinv(2 * a % p, p));
            if (x1 > x2) std::swap(x1, x2);
            if (x1 == x2) {
                printf("%lld\n", x1);
            } else {
                printf("%lld %lld\n", x1, x2);
            }
        }
    }
}
0