結果

問題 No.551 夏休みの思い出(2)
ユーザー tottoripapertottoripaper
提出日時 2017-08-02 14:19:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,592 ms / 4,000 ms
コード長 3,036 bytes
コンパイル時間 2,171 ms
コンパイル使用メモリ 177,024 KB
実行使用メモリ 8,428 KB
最終ジャッジ日時 2024-04-19 13:30:04
合計ジャッジ時間 30,861 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 3 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 8 ms
5,376 KB
testcase_06 AC 12 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 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 1 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 1 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 5 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 854 ms
5,376 KB
testcase_28 AC 829 ms
5,376 KB
testcase_29 AC 229 ms
5,376 KB
testcase_30 AC 722 ms
5,376 KB
testcase_31 AC 245 ms
5,376 KB
testcase_32 AC 727 ms
5,376 KB
testcase_33 AC 858 ms
5,376 KB
testcase_34 AC 622 ms
5,376 KB
testcase_35 AC 270 ms
5,376 KB
testcase_36 AC 471 ms
5,376 KB
testcase_37 AC 3,592 ms
8,428 KB
testcase_38 AC 2,981 ms
7,528 KB
testcase_39 AC 1,224 ms
5,480 KB
testcase_40 AC 2,376 ms
7,528 KB
testcase_41 AC 1,030 ms
5,480 KB
testcase_42 AC 3,010 ms
7,528 KB
testcase_43 AC 1,213 ms
5,480 KB
testcase_44 AC 1,714 ms
5,484 KB
testcase_45 AC 1,319 ms
5,480 KB
testcase_46 AC 3,367 ms
7,656 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 2 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());
template <typename T>
T inverse(T n, T mod);
std::tuple<ll,ll,ll> extgcd(ll a, ll b);

ll ti;
ll B;
ll P, R;
ll n, alpha;
std::vector<std::tuple<ll,int>> expos;
ll table[252521];

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

void init();
ll babyStepGiantStep(ll b);

int main(int argc, char** argv){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    ti = 7;
    // ti = std::stoi(argv[1]);
    
    std::cin >> P >> R;
    
    init();
    
    ll g, s;
    tie(g, s, ignore) = extgcd(2ll, P-1);
    
    int Q;
    std::cin >> Q;
        
    for(int i=0;i<Q;++i){
        ll a, b, c;
        std::cin >> 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(D);
            
            if(m % g != 0){
                std::cout << -1ll << std::endl;
                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){
            std::cout << x0 << std::endl;
        }else{
            std::cout << x0 << " " << x1 << std::endl;
        }
    }
}

void init(){
    B = sqrt(P) * ti;
    if(B == 0){B = P;}
    n = P / B;
    alpha = expt(R, B, P);
    for(ll i=0;i<B;++i){
        expos.emplace_back(expt(R, i, P), i);
    }
    for(int i=0;i<=n;++i){
        table[i] = inverse(expt(alpha, 1ll * i, P), P);
    }
    std::sort(expos.begin(), expos.end());
}

// solve a^n = b (in F_p)
ll babyStepGiantStep(ll b){
    for(int i=0;i<=n;++i){
        ll v = b * table[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){
            ll c = B * i + std::get<1>(*it);
            return c;
        }
    }

    return -1ll;
}

std::tuple<ll,ll,ll> extgcd(ll a, ll b){
    if(b == 0){
        return std::make_tuple(a, 1ll, 0ll);
    }
    auto s = extgcd(b, a % b);

    return std::make_tuple(fst(s), thd(s), snd(s)-(a/b)*thd(s));
}

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

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