結果

問題 No.551 夏休みの思い出(2)
ユーザー PachicobuePachicobue
提出日時 2017-07-29 04:13:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 5,112 bytes
コンパイル時間 2,039 ms
コンパイル使用メモリ 180,988 KB
実行使用メモリ 164,836 KB
最終ジャッジ日時 2024-04-19 04:31:53
合計ジャッジ時間 36,365 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 8 ms
5,376 KB
testcase_05 AC 14 ms
5,376 KB
testcase_06 AC 19 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 5 ms
5,376 KB
testcase_18 AC 7 ms
5,376 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 6 ms
5,376 KB
testcase_22 AC 4 ms
5,376 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 5 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 6 ms
5,376 KB
testcase_27 AC 1,121 ms
42,868 KB
testcase_28 AC 1,046 ms
42,340 KB
testcase_29 AC 295 ms
14,620 KB
testcase_30 AC 1,010 ms
42,468 KB
testcase_31 AC 310 ms
14,620 KB
testcase_32 AC 1,001 ms
42,468 KB
testcase_33 AC 1,086 ms
42,608 KB
testcase_34 AC 811 ms
29,796 KB
testcase_35 AC 340 ms
15,776 KB
testcase_36 AC 603 ms
25,060 KB
testcase_37 TLE -
testcase_38 AC 3,421 ms
108,320 KB
testcase_39 AC 1,245 ms
51,848 KB
testcase_40 AC 2,697 ms
92,576 KB
testcase_41 AC 1,309 ms
47,348 KB
testcase_42 AC 3,285 ms
109,860 KB
testcase_43 AC 1,263 ms
52,088 KB
testcase_44 AC 1,837 ms
82,788 KB
testcase_45 AC 1,348 ms
54,900 KB
testcase_46 AC 3,879 ms
164,836 KB
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'll logR(ll)':
main.cpp:110:1: warning: control reaches end of non-void function [-Wreturn-type]
  110 | }
      | ^

ソースコード

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;
ll W, H;

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;
    }
}

unordered_map<ll, ll> table;
ll step;
ll logR(const ll a)  // return log_R(a)
{
    if (a == 1) {
        return 0;
    }
    ll val = a;
    for (ll i = 0; i < W; i++) {
        if (table.find(val) != table.end()) {
            return i * H + table[val];
        } else {
            val = (val * step) % P;
        }
    }
}

pair<ll, ll> square_root(const ll a)
{
    if (a == 0) {
        return make_pair(0, 0);
    } else {
        if (P % 4 == 3) {
            const ll s = power(a, (P + 1) / 4);
            return make_pair(s, P - s);
        }

        const ll x2 = logR(a);
        return make_pair(power(R, x2 / 2), power(R, (x2 + (P - 1)) / 2));
    }
}

bool quadratic(ll n)
{
    if (n == 0) {
        return true;
    } else {
        return power(n, (P - 1) / 2) == 1;
    }
}

int main()
{
    cin >> P >> R;
    ll Q;
    cin >> Q;
    ll mini = INF<ll>;
    for (ll i = 1; i * i <= P - 1; i++) {
        const ll w = i;
        const ll h = (P - 1 + i - 1) / i;
        if (mini > h + w * Q) {
            mini = h + w * Q;
            H = h;
            W = w;
        }
    }

    for (ll i = 0; i < H; i++) {
        table[power(R, i)] = i;
    }
    step = power(R, (P - 1) - H);

    for (ll q = 0; q < Q; q++) {
        ll A, B, C;
        cin >> A >> B >> C;
        if (B != 0 and C != 0) {
            const ll sq = (P + (B * B - 4 * A * C) % P) % P;
            if (not quadratic(sq)) {
                cout << -1 << endl;
                continue;
            }
            const pair<ll, ll> root = square_root(sq);
            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;
                    }
                }
            }
            vector<ll> X(4);
            for (int i = 0; i < 4; i++) {
                if (Z[i] == 0) {
                    X[i] = 0;
                } else {
                    X[i] = divide(2 * A, Z[i], P);
                }
            }
            sort(X.begin(), X.end());
            X.erase(unique(X.begin(), X.end()), X.end());
            for (ll i = 0; i < X.size(); i++) {
                if (i != 0) {
                    cout << " ";
                }
                cout << X[i];
            }
            cout << endl;
        } else if (B != 0) {
            cout << 0 << " ";
            const ll b = divide(A, -B, P);
            cout << b << endl;
        } else if (C != 0) {
            const ll x2 = divide(A, -C, P);
            if (quadratic(x2)) {
                const pair<ll, ll> root = square_root(x2);
                const ll u = root.first;
                const ll v = root.second;
                cout << min(u, v) << " " << max(u, v) << endl;
            } else {
                cout << -1 << endl;
            }
        } else {
            cout << 0 << endl;
        }
    }
    return 0;
}
0