結果

問題 No.551 夏休みの思い出(2)
ユーザー pekempeypekempey
提出日時 2017-07-29 01:26:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,914 bytes
コンパイル時間 779 ms
コンパイル使用メモリ 78,228 KB
実行使用メモリ 134,616 KB
最終ジャッジ日時 2024-04-19 01:42:05
合計ジャッジ時間 64,165 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 561 ms
134,416 KB
testcase_01 AC 698 ms
134,588 KB
testcase_02 AC 738 ms
134,604 KB
testcase_03 AC 846 ms
134,476 KB
testcase_04 AC 873 ms
134,600 KB
testcase_05 AC 856 ms
134,476 KB
testcase_06 AC 879 ms
134,376 KB
testcase_07 AC 913 ms
134,440 KB
testcase_08 AC 920 ms
134,412 KB
testcase_09 AC 908 ms
134,488 KB
testcase_10 AC 914 ms
134,608 KB
testcase_11 AC 911 ms
134,580 KB
testcase_12 AC 927 ms
134,476 KB
testcase_13 AC 912 ms
134,488 KB
testcase_14 AC 939 ms
134,476 KB
testcase_15 AC 947 ms
134,480 KB
testcase_16 AC 938 ms
134,452 KB
testcase_17 AC 887 ms
134,476 KB
testcase_18 AC 863 ms
134,404 KB
testcase_19 AC 869 ms
134,576 KB
testcase_20 AC 873 ms
134,408 KB
testcase_21 AC 859 ms
134,480 KB
testcase_22 AC 893 ms
134,408 KB
testcase_23 AC 889 ms
134,472 KB
testcase_24 AC 885 ms
134,476 KB
testcase_25 AC 889 ms
134,532 KB
testcase_26 AC 885 ms
134,488 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 AC 693 ms
134,472 KB
testcase_48 AC 694 ms
134,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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::vector<std::pair<int64_t, int>> baby;
    for (int i = 0; i < S; i++) {
        baby.emplace_back(rs, i);
        rs = rs * r % p;
    }
    rs = modinv(rs, p);
    std::sort(baby.begin(), baby.end());

    auto modlog = [&](int64_t a) -> int64_t {
        if (a == 0) return 1000000000;
        int64_t giant = 1;
        for (int i = 0; i < T; i++) {
            auto it = std::lower_bound(baby.begin(), baby.end(), std::make_pair(a * giant % p, 0));
            if (it != baby.end() && it->first * giant % p == a) {
                return it->second + 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