結果

問題 No.551 夏休みの思い出(2)
ユーザー PachicobuePachicobue
提出日時 2017-07-29 02:57:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 5,799 bytes
コンパイル時間 1,930 ms
コンパイル使用メモリ 186,436 KB
実行使用メモリ 13,640 KB
最終ジャッジ日時 2024-10-10 20:30:53
合計ジャッジ時間 13,820 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 4 ms
5,248 KB
testcase_03 AC 12 ms
5,248 KB
testcase_04 AC 23 ms
5,248 KB
testcase_05 AC 54 ms
5,248 KB
testcase_06 AC 89 ms
5,248 KB
testcase_07 AC 6 ms
5,248 KB
testcase_08 AC 6 ms
5,248 KB
testcase_09 AC 5 ms
5,248 KB
testcase_10 AC 3 ms
5,248 KB
testcase_11 AC 5 ms
5,248 KB
testcase_12 AC 5 ms
5,248 KB
testcase_13 AC 4 ms
5,248 KB
testcase_14 AC 5 ms
5,248 KB
testcase_15 AC 4 ms
5,248 KB
testcase_16 AC 5 ms
5,248 KB
testcase_17 AC 339 ms
5,248 KB
testcase_18 AC 780 ms
5,248 KB
testcase_19 AC 703 ms
5,248 KB
testcase_20 AC 753 ms
5,248 KB
testcase_21 AC 813 ms
5,248 KB
testcase_22 AC 236 ms
5,248 KB
testcase_23 AC 308 ms
5,248 KB
testcase_24 AC 669 ms
5,248 KB
testcase_25 AC 430 ms
5,248 KB
testcase_26 AC 495 ms
5,248 KB
testcase_27 TLE -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}

constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

ll P, R;

template <typename T>
T gcd(const T a, const T b)
{
    return (b != 0) ? gcd(b, a % b) : a;
}

template <typename T>
T extgcd(const T a, const T b, T& x, T& y)  // ax+by=gcd(a,b)
{
    T d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1;
        y = 0;
    }
    return d;
}

template <typename T>
T inverse(const T a, const T mod)
{
    if (gcd(a, mod) != 1) {
        //        cerr << "No inverse" << endl;
        return -1;
    }
    T x, y;
    extgcd(a, mod, x, y);
    return (mod + x % mod) % mod;
}

template <typename T>
T divide(const T a, const T b, const T mod)  // return x (s.t. ax=b)(remark: x is value of modulo (mod/gcd(a, mod)))
{
    const T g = gcd(a, mod);
    if ((g + b % g) % g != 0) {
        //        cerr << "Cannot divide" << endl;
        return -1;
    } else {
        return (mod + (inverse(a / g, mod / g) * (b / g)) % mod) % mod;
    }
}


ll power(const ll a, const ll n)
{
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 0) {
        const ll p = power(a, n / 2);
        return (p * p) % P;
    } else {
        return (a * power(a, n - 1)) % P;
    }
}

ll logR(const ll a)  // return log_R(a)
{
    assert(a != 0);
    if (a == 1) {
        return 0;
    }
    static mt19937 mt{random_device{}()};
    static uniform_int_distribution<ll> dist{0, P - 2};
    unordered_map<ll, pair<ll, ll>> mp;
    while (true) {
        const ll alpha = dist(mt);
        const ll beta = dist(mt);
        const ll c = (power(a, alpha) * power(R, beta)) % P;
        if (mp.find(c) != mp.end()) {
            const ll prev_alpha = mp.at(c).first;
            const ll prev_beta = mp.at(c).second;
            if (beta == prev_beta) {
                continue;
            }

            const ll alpha_sa = ((P - 1) + (alpha - prev_alpha) % (P - 1)) % (P - 1);
            const ll beta_sa = ((P - 1) + (prev_beta - beta) % (P - 1)) % (P - 1);
            const ll x = divide(alpha_sa, beta_sa, P - 1);

            //            const ll x = divide((alpha - prev_alpha), (prev_beta - beta), P - 1);
            if (x == -1) {
                continue;
            }
            if (power(R, x) == a) {
                return x;
            }
        }
        mp[c] = make_pair(alpha, beta);
    }
}

pair<ll, ll> square_root(const ll a)
{
    if (a == 0) {
        return make_pair(0, 0);
    } else {
        const ll x2 = logR(a);
        if (x2 % 2 == 1) {
            return make_pair(-1, -1);
        } else {
            return make_pair(power(R, x2 / 2), power(R, (x2 + (P - 1)) / 2));
        }
    }
}

int main()
{
    cin >> P >> R;
    ll Q;
    cin >> Q;
    for (ll q = 0; q < Q; q++) {
        ll A, B, C;
        cin >> A >> B >> C;
        const ll a = logR(A);
        if (B != 0 and C != 0) {
            const ll sq = (P + (B * B - 4 * A * C) % P) % P;
            const pair<ll, ll> root = square_root(sq);
            if (root.first == -1) {
                cout << -1 << endl;
                continue;
            }
            ll Z[4];
            for (int i = 0; i < 4; i++) {
                if (i % 2 == 0) {
                    if (i / 2 == 0) {
                        Z[i] = (P + (-B + root.first) % P) % P;
                    } else {
                        Z[i] = (P + (-B + root.second) % P) % P;
                    }
                } else {
                    if (i / 2 == 0) {
                        Z[i] = (P + (-B - root.first) % P) % P;
                    } else {
                        Z[i] = (P + (-B - root.second) % P) % P;
                    }
                }
            }
            ll z[4];
            vector<ll> X(4);
            for (int i = 0; i < 4; i++) {
                if (Z[i] == 0) {
                    X[i] = 0;
                } else {
                    z[i] = ((P - 1) + (logR(Z[i]) - a) % (P - 1)) % (P - 1);
                    X[i] = power(R, z[i]);
                    X[i] = (X[i] % 2 == 0) ? X[i] / 2 : (X[i] + P) / 2;
                }
            }
            sort(X.begin(), X.end());
            X.erase(unique(X.begin(), X.end()), X.end());
            for (int i = 0; i < X.size(); i++) {
                if (i != 0) {
                    cout << " ";
                }
                cout << X[i];
            }
            cout << endl;
        } else if (B != 0) {
            cout << 0 << " ";
            const ll b = logR(B);
            ll x = (P - 1) / 2 - (a - b);
            x = ((P - 1) + x % (P - 1)) % (P - 1);
            cout << power(R, x) << endl;
        } else if (C != 0) {
            const ll c = logR(C);
            ll x2 = (P - 1) / 2 - (a - c);
            x2 = ((P - 1) + x2 % (P - 1)) % (P - 1);
            if (x2 % 2 == 1) {
                cout << -1 << endl;
            } else {
                const ll x = x2 / 2;
                const ll y = x2 / 2 + (P - 1) / 2;
                const ll u = power(R, x);
                const ll v = power(R, y);
                cout << min(u, v) << " " << max(u, v) << endl;
            }
        } else {
            cout << 0 << endl;
        }
    }
    return 0;
}
0