結果

問題 No.551 夏休みの思い出(2)
ユーザー tottoripapertottoripaper
提出日時 2017-08-07 23:56:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,853 ms / 4,000 ms
コード長 4,037 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 184,392 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2024-04-20 04:42:56
合計ジャッジ時間 24,741 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 3 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 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 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 3 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 651 ms
5,376 KB
testcase_28 AC 624 ms
5,376 KB
testcase_29 AC 181 ms
5,376 KB
testcase_30 AC 561 ms
5,376 KB
testcase_31 AC 176 ms
5,376 KB
testcase_32 AC 544 ms
5,376 KB
testcase_33 AC 654 ms
5,376 KB
testcase_34 AC 480 ms
5,376 KB
testcase_35 AC 208 ms
5,376 KB
testcase_36 AC 364 ms
5,376 KB
testcase_37 AC 2,853 ms
6,400 KB
testcase_38 AC 2,454 ms
6,016 KB
testcase_39 AC 922 ms
5,376 KB
testcase_40 AC 1,870 ms
5,376 KB
testcase_41 AC 786 ms
5,376 KB
testcase_42 AC 2,420 ms
6,016 KB
testcase_43 AC 908 ms
5,376 KB
testcase_44 AC 1,309 ms
5,376 KB
testcase_45 AC 1,031 ms
5,376 KB
testcase_46 AC 2,759 ms
6,272 KB
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;

template <typename T>
T expt(T a, T n, T mod = std::numeric_limits<T>::max()){
    T res = 1;
    while(n){
        if(n & 1){res = res * a % mod;}
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

template <typename T>
T inverse(T n, T mod){
    return expt(n, mod-2, mod);
}

struct BabyStepGiantStep{
    int bucket_size, bucket_n;
    long long p, g;    
    std::vector<std::tuple<long long, long long>> expos;
    std::vector<long long> invs;

    BabyStepGiantStep() = default;
    BabyStepGiantStep(long long p, long long g, int bucket_size) : p(p), g(g), bucket_size(bucket_size) {
        bucket_n = (p + bucket_size - 1) / bucket_size;
        // expos = std::vector<std::tuple<long long, long long>>(bucket_size);
        expos = std::vector<std::tuple<long long, long long>>((bucket_size + 1) / 2);
        expos[0] = std::make_tuple(1, 0);
        {
            long long ex = 1;
            // for(int i=1;i<bucket_size;++i){
            for(int i=1;i<(bucket_size + 1)/2;++i){
                // ex = ex * g % p;
                ex = ex * g % p * g % p;
                // expos[i] = std::make_tuple(ex, i);
                expos[i] = std::make_tuple(ex, i * 2);
            }
        }
        std::sort(expos.begin(), expos.end());

        invs = std::vector<long long>(bucket_n);
        
        long long alpha = expt<long long>(g, bucket_size, p),
            inv_alpha = inverse(alpha, p);

        invs[0] = 1ll;
        for(int i=0;i+1<bucket_n;++i){
            invs[i+1] = invs[i] * inv_alpha % p;
        }        
    }
    BabyStepGiantStep& operator=(const BabyStepGiantStep&) = default;
    BabyStepGiantStep& operator=(BabyStepGiantStep&&) = default;

    // solve g^n = b (mod p)
    long long solve(long long b){
        for(long long i=0;i<bucket_n;++i){
            long long v = b * invs[i] % p;
        
            auto it = std::lower_bound(expos.begin(), expos.end(), std::make_tuple(v, 0), [](const auto& lhs, const auto& rhs){return std::get<0>(lhs) < std::get<0>(rhs);});
        
            if(it != expos.end() && std::get<0>(*it) == v){
                long long c = i * bucket_size + std::get<1>(*it);
                return c;
            }
        }

        return -1; // no solution
    }
};

// extgcd(a, b) = (g, s, t)
// g: GCD of a and b, s, t: a solution of sa + tb = g
template <typename T>
std::tuple<T, T, T> extgcd(T a, T b){
    if(b == 0){
        return std::make_tuple(a, 1, 0);
    }
    T g, _s, _t;
    std::tie(g, _s, _t) = extgcd(b, a % b);
    return std::make_tuple(g, _t, _s - (a/b) * _t);
}

BabyStepGiantStep babyStepGiantStep;

int ti;
ll P, R;

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    ti = 12;

    scanf("%lld %lld", &P, &R);

    babyStepGiantStep = std::move(BabyStepGiantStep(P, R, (int)std::sqrt(P) * ti));
    
    ll g, s;
    tie(g, s, ignore) = extgcd(2ll, P-1);
    
    int Q;
    scanf("%d", &Q);
        
    for(int i=0;i<Q;++i){
        ll a, b, c;
        scanf("%lld %lld %lld", &a, &b, &c);

        ll D = (b * b - 4ll * a * c) % P;
        D = D >= 0 ? D : D + P;
        
        ll sq;

        if(D == 0){
            sq = 0;
        }else{
            ll m = babyStepGiantStep.solve(D);
            
            if(m == -1){
                puts("-1");
                continue;
            }
            
            ll t = s * (m / g) % (P - 1);
            t = t >= 0 ? t : t + (P - 1);
            
            sq = expt(R, t, P);
        }

        ll den = inverse(2ll * a % P, P);
        
        ll x0 = (-b - sq) * den % P,
            x1 = (-b + sq) * den % P;
        x0 = x0 >= 0 ? x0 : x0 + P;
        x1 = x1 >= 0 ? x1 : x1 + P;

        if(x0 > x1){swap(x0, x1);}
        if(x0 == x1){
            printf("%lld\n", x0);
        }else{
            printf("%lld %lld\n", x0, x1);
        }
    }
}
0