結果

問題 No.551 夏休みの思い出(2)
ユーザー pekempeypekempey
提出日時 2017-07-29 00:40:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 4,000 ms
コード長 1,776 bytes
コンパイル時間 990 ms
コンパイル使用メモリ 70,416 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 17:55:39
合計ジャッジ時間 2,693 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 3 ms
6,940 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 5 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 28 ms
6,940 KB
testcase_28 AC 27 ms
6,940 KB
testcase_29 AC 25 ms
6,944 KB
testcase_30 AC 27 ms
6,940 KB
testcase_31 AC 24 ms
6,940 KB
testcase_32 AC 27 ms
6,940 KB
testcase_33 AC 27 ms
6,944 KB
testcase_34 AC 25 ms
6,940 KB
testcase_35 AC 25 ms
6,944 KB
testcase_36 AC 26 ms
6,944 KB
testcase_37 AC 31 ms
6,944 KB
testcase_38 AC 31 ms
6,944 KB
testcase_39 AC 28 ms
6,940 KB
testcase_40 AC 30 ms
6,940 KB
testcase_41 AC 28 ms
6,944 KB
testcase_42 AC 31 ms
6,944 KB
testcase_43 AC 28 ms
6,944 KB
testcase_44 AC 29 ms
6,940 KB
testcase_45 AC 27 ms
6,944 KB
testcase_46 AC 31 ms
6,940 KB
testcase_47 AC 1 ms
6,944 KB
testcase_48 AC 2 ms
6,940 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);
}

bool is_square(int64_t a, int64_t p) {
    return modpow(a, (p - 1) / 2, p) != p - 1;
}

int64_t modsqrt(int64_t a, int64_t p) {
    if (a == 0) return 0;
    if (p == 2) return a;
    if (!is_square(a, p)) return -1;
    int64_t b = 2;
    while (is_square((b * b + p - a) % p, p)) b++;
    int64_t w = (b * b + p - a) % p;
    struct P { int64_t x, y; };
    auto mul = [&](P u, P v) {
        int64_t a = (u.x * v.x + u.y * v.y % p * w) % p;
        int64_t b = (u.x * v.y + u.y * v.x) % p;
        return P{ a, b };
    };
    P ret{ 1, 0 };
    P v{ b, 1 };
    for (int64_t e = (p + 1) / 2; e > 0; e >>= 1) {
        if (e & 1) ret = mul(ret, v);
        v = mul(v, v);
    }
    return ret.x;
}

int main() {
    int64_t p, r;
    int q;
    std::cin >> p >> r >> q;
    auto modulo = [&](int64_t a) {
        a %= p;
        if (a < 0) a += p;
        return a;
    };
    while (q--) {
        int64_t a, b, c;
        scanf("%lld %lld %lld", &a, &b, &c);
        int64_t d = modulo(b * b - 4 * a * c);
        if (is_square(d, p)) {
            d = modsqrt(d, 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);
            }
        } else {
            puts("-1");
        }
    }
}
0