結果

問題 No.551 夏休みの思い出(2)
ユーザー tottoripapertottoripaper
提出日時 2017-08-02 01:28:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,123 bytes
コンパイル時間 2,046 ms
コンパイル使用メモリ 181,364 KB
実行使用メモリ 10,272 KB
最終ジャッジ日時 2024-04-19 07:54:39
合計ジャッジ時間 8,295 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,272 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 9 ms
5,376 KB
testcase_06 AC 13 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 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 26 ms
5,376 KB
testcase_18 AC 47 ms
5,376 KB
testcase_19 AC 44 ms
5,376 KB
testcase_20 AC 46 ms
5,376 KB
testcase_21 AC 55 ms
5,376 KB
testcase_22 AC 14 ms
5,376 KB
testcase_23 AC 21 ms
5,376 KB
testcase_24 AC 39 ms
5,376 KB
testcase_25 AC 24 ms
5,376 KB
testcase_26 AC 37 ms
5,376 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>

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;

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

ll babyStepGiantStep(ll a, ll b, ll p, ll l, ll r);
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);
std::tuple<ll,int> expos[100000];

ll P, R;
map<ll,ll> inv_map, bsgs_map;

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

    std::cin >> P >> R;

    int Q;
    std::cin >> Q;

    ll g, s;
    tie(g, s, ignore) = extgcd(2ll, P-1);
        
    for(int i=0;i<Q;++i){
        ll a, b, c;
        std::cin >> a >> b >> c;

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

        if(D == 0){
            sq = 0;
        }else{
            if(bsgs_map.find(D) == bsgs_map.end()){
                bsgs_map[D] = babyStepGiantStep(R, D, P, 0, P - 1);
            }
            ll m = bsgs_map[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 = 2ll * a % P;
        if(inv_map.find(den) == inv_map.end()){
            inv_map[den] = inverse(2ll * a % P, P);
        }
        den = inv_map[den];
        
        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;
        }
    }
}

// solve a^n = b, 0 <= l <= n <= r < p (in F_p)
ll babyStepGiantStep(ll a, ll b, ll p, ll l, ll r){
    ll n = std::floor(std::sqrt(p)), alpha = expt(a, n, p);

    for(int i=0;i<n;++i){
        expos[i] = std::make_tuple(expt(a, 1ll * i, p), i);
    }

    std::sort(expos, expos + n);
    
    for(int i=l/n;n*i<p;++i){
        ll v = b * inverse(expt(alpha, 1ll * i, p), p) % p;
        
        auto it = std::lower_bound(expos, expos + n, std::make_tuple(v, i == l/n ? l % n : 0));
        
        if(it != expos + n && std::get<0>(*it) == v){
            ll c = n * i + std::get<1>(*it);
            
            if(c <= r){
                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>
T inverse(T n, T mod){
    return expt(n, mod-2, mod);
}
0