結果

問題 No.551 夏休みの思い出(2)
ユーザー kimiyukikimiyuki
提出日時 2017-07-28 23:53:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 4,000 ms
コード長 3,995 bytes
コンパイル時間 1,200 ms
コンパイル使用メモリ 87,040 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 13:42:12
合計ジャッジ時間 2,917 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 4 ms
6,940 KB
testcase_05 AC 6 ms
6,944 KB
testcase_06 AC 8 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 3 ms
6,944 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 3 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 3 ms
6,940 KB
testcase_27 AC 30 ms
6,940 KB
testcase_28 AC 28 ms
6,940 KB
testcase_29 AC 26 ms
6,940 KB
testcase_30 AC 39 ms
6,944 KB
testcase_31 AC 25 ms
6,944 KB
testcase_32 AC 34 ms
6,944 KB
testcase_33 AC 30 ms
6,940 KB
testcase_34 AC 27 ms
6,940 KB
testcase_35 AC 29 ms
6,940 KB
testcase_36 AC 30 ms
6,944 KB
testcase_37 AC 31 ms
6,944 KB
testcase_38 AC 34 ms
6,940 KB
testcase_39 AC 32 ms
6,944 KB
testcase_40 AC 33 ms
6,940 KB
testcase_41 AC 50 ms
6,940 KB
testcase_42 AC 30 ms
6,940 KB
testcase_43 AC 29 ms
6,940 KB
testcase_44 AC 29 ms
6,940 KB
testcase_45 AC 28 ms
6,940 KB
testcase_46 AC 31 ms
6,944 KB
testcase_47 AC 2 ms
6,940 KB
testcase_48 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <algorithm>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cassert>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_from(i, m, n) for (int i = (m); (i) < int(n); ++(i))
#define repeat_reverse(i, n) for (int i = (n)-1; (i) >= 0; --(i))
#define repeat_from_reverse(i, m, n) for (int i = (n)-1; (i) >= int(m); --(i))
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define unittest_name_helper(counter) unittest_ ## counter
#define unittest_name(counter) unittest_name_helper(counter)
#define unittest __attribute__((constructor)) void unittest_name(__COUNTER__) ()
using ll = long long;
using namespace std;
template <class T> inline void setmax(T & a, T const & b) { a = max(a, b); }
template <class T> inline void setmin(T & a, T const & b) { a = min(a, b); }
template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }

ll powmod(ll x, ll y, ll p) { // O(log y)
    x %= p; if (x < 0) x += p;
    assert (0 <= x and x < p);
    assert (0 <= y);
    ll z = 1;
    for (ll i = 1; i <= y; i <<= 1) {
        if (y & i) z = z * x % p;
        x = x * x % p;
    }
    return z;
}
ll modinv(ll x, ll p) { // p must be a prime, O(log p)
    assert ((x % p + p) % p != 0);
    return powmod(x, p-2, p);
}

vector<bool> sieve_of_eratosthenes(int n) { // enumerate primes in [2,n] with O(n log log n)
    vector<bool> is_prime(n+1, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i*i <= n; ++i)
        if (is_prime[i])
            for (int k = i+i; k <= n; k += i)
                is_prime[k] = false;
    return is_prime;
}
vector<int> list_primes(int n) {
    auto is_prime = sieve_of_eratosthenes(n);
    vector<int> primes;
    for (int i = 2; i <= n; ++i)
        if (is_prime[i])
            primes.push_back(i);
    return primes;
}

int legendre_symbol(int a, int p) {
    return powmod(a, (p - 1) / 2, p); // Euler's criterion
}

int modsqrt(int a, int p) {
    a %= p;
    if (a == 0) return 0;
    if (p == 2) return a;
    assert (p >= 3);
    if (legendre_symbol(a, p) != +1) return -1;
    int b = 1;
    while (legendre_symbol(b, p) == 1) {
        b += 1;
    }
    int e = 0;
    int m = p - 1;
    while (m % 2 == 0) {
        m /= 2;
        e += 1;
    }
    ll x = powmod(a, (m - 1) / 2, p);
    ll y = a * x % p * x % p;
    x = x * a % p;
    ll z = powmod(b, m, p);
    while (y != 1) {
        int j = 0;
        for (ll t = y; t != 1; t = t * t % p) ++ j;
        if (e <= j) return -1;
        z = powmod(z, 1ll << (e - j - 1), p);
        x =x * z % p;
        z =z * z % p;
        y =y * z % p;
        e = j;
    }
    assert (x * x % p == a);
    return x;
}

vector<int> solve_modeqn(int a, int b, int c, int p) { // ax^2 + bx + c = 0 mod p
    int d = (b *(ll) b - 4ll * a * c) % p;
    if (d < 0) d += p;
    int w = modsqrt(d, p);
    if (w == -1) return vector<int>();
    vector<int> xs;
    xs.push_back((- b + w) *(ll) modinv(2 * a, p) % p);
    xs.push_back((- b - w) *(ll) modinv(2 * a, p) % p);
    if (xs[0] < 0) xs[0] += p;
    if (xs[1] < 0) xs[1] += p;
    whole(sort, xs);
    xs.erase(whole(unique, xs), xs.end());
    return xs;
}

int main() {
    vector<int> primes = list_primes(1e5);
    int p, r, q; scanf("%d%d%d", &p, &r, &q);
    repeat (i, q) {
        int a, b, c; scanf("%d%d%d", &a, &b, &c);
        vector<int> xs = solve_modeqn(a, b, c, p);
        if (xs.empty()) {
            printf("-1\n");
        } else {
            repeat (i, xs.size()) {
                printf("%d%c", xs[i], i + 1 == xs.size() ? '\n' : ' ');
            }
        }
    }
    return 0;
}
0