結果

問題 No.551 夏休みの思い出(2)
ユーザー pekempeypekempey
提出日時 2017-07-28 23:14:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 4,000 ms
コード長 1,992 bytes
コンパイル時間 661 ms
コンパイル使用メモリ 74,452 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 11:35:35
合計ジャッジ時間 2,929 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 5 ms
5,376 KB
testcase_06 AC 7 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 29 ms
5,376 KB
testcase_28 AC 29 ms
5,376 KB
testcase_29 AC 26 ms
5,376 KB
testcase_30 AC 28 ms
5,376 KB
testcase_31 AC 24 ms
5,376 KB
testcase_32 AC 28 ms
5,376 KB
testcase_33 AC 28 ms
5,376 KB
testcase_34 AC 27 ms
5,376 KB
testcase_35 AC 27 ms
5,376 KB
testcase_36 AC 27 ms
5,376 KB
testcase_37 AC 32 ms
5,376 KB
testcase_38 AC 32 ms
5,376 KB
testcase_39 AC 30 ms
5,376 KB
testcase_40 AC 31 ms
5,376 KB
testcase_41 AC 29 ms
5,376 KB
testcase_42 AC 31 ms
5,376 KB
testcase_43 AC 30 ms
5,376 KB
testcase_44 AC 30 ms
5,376 KB
testcase_45 AC 29 ms
5,376 KB
testcase_46 AC 31 ms
5,376 KB
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>

int modpow(int a, int b, int m) {
    int ret = 1;
    while (b > 0) {
        if (b & 1) ret = 1LL * ret * a % m;
        a = 1LL * a * a % m;
        b /= 2;
    }
    return ret;
}

int modinv(int a, int m) {
    return modpow(a, m - 2, m);
}

bool issquare(int a, int p) {
    return modpow(a, (p - 1) / 2, p) == 1;
}

int modsqrt(int a, int p) {
    if (a == 0) return 0;
    if (p == 2) return a;
    if (!issquare(a, p)) return -1;
    int b = 2;
    while (issquare((1LL * b * b - a + p) % p, p)) b++;
    int w = (1LL * b * b - a + p) % p;

    auto mul = [&](std::pair<int, int> u, std::pair<int, int> v) {
        int a = (1LL * u.first * v.first + 1LL * u.second * v.second % p * w) % p;
        int b = (1LL * u.first * v.second + 1LL * u.second * v.first) % p;
        return std::make_pair(a, b);
    };

    // (b + sqrt(b^2-a))^(p+1)/2
    int e = (p + 1) / 2;
    auto ret = std::make_pair(1, 0);
    auto v = std::make_pair(b, 1);
    while (e > 0) {
        if (e & 1) ret = mul(ret, v);
        v = mul(v, v);
        e /= 2;
    }
    return ret.first;
}

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

    auto f = [&](long long x) {
        x %= p;
        if (x < 0) {
            x += p;
        }
        return x;
    };

    int q;
    std::cin >> q;
    while (q--) {
        long long a, b, c;
        scanf("%lld %lld %lld", &a, &b, &c);

        long long d = f(b * b - 4 * a * c);

        if (d == 0) {
            long long x1 = f(f(-b) * modinv(f(2 * a), p));
            printf("%lld\n", x1);
        } else if (!issquare(d, p)) {
            puts("-1");
            continue;
        } else {
            d = modsqrt(d, p);
            long long x1 = f(f(-b + d) * modinv(f(2 * a), p));
            long long x2 = f(f(-b - d) * modinv(f(2 * a), p));
            if (x1 > x2) std::swap(x1, x2);
            printf("%lld %lld\n", x1, x2);
        }
    }
}
0