結果

問題 No.551 夏休みの思い出(2)
ユーザー mamekinmamekin
提出日時 2017-07-29 18:24:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,313 bytes
コンパイル時間 1,225 ms
コンパイル使用メモリ 116,288 KB
実行使用メモリ 14,016 KB
最終ジャッジ日時 2024-04-19 03:00:06
合計ジャッジ時間 7,312 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 4 ms
5,376 KB
testcase_04 AC 7 ms
5,376 KB
testcase_05 AC 12 ms
5,376 KB
testcase_06 AC 16 ms
5,376 KB
testcase_07 AC 3 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 4 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 6 ms
5,376 KB
testcase_22 AC 3 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 5 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 #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

// 累乗、べき乗
long long power(int a, int b, int p)
{
    long long ret = 1;
    long long tmp = a;
    while(b > 0){
        if(b & 1){
            ret *= tmp;
            ret %= p;
        }
        tmp *= tmp;
        tmp %= p;
        b >>= 1;
    }
    return ret;
}

// a,b の最大公約数と、ax + by = gcd(a,b) となる x,y を求める
long long extgcd(long long a, long long b, long long &x, long long &y) {
    long long g = a;
    if(b != 0){
        g = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }else{
        x = 1;
        y = 0;
    }
    return g;
}

// ax ≡ gcd(a, m) (mod m) となる x を求める
// a, m が互いに素ならば、関数値は mod m での a の逆数となる
long long mod_inverse(long long a, long long m)
{
    long long x, y;
    extgcd(a, m, x, y);
    return (x % m + m) % m;
}

/***************************************************************************************************/
// 離散対数問題
//   x ^ i ≡ y (mod p) となる i を、Baby-step giant-step algorithm により求める。
//   ただし、p は素数。
/***************************************************************************************************/
class discreteLogarithm
{
private:
    int p;
    int step;
    long long inv;
    map<long long, int> m;
public:
    discreteLogarithm(int x, int p)
    {
        int i;
        long long a = 1;
        for(i=0; i*i<p; ++i){
            if(m.find(a) == m.end())
                m[a] = i;
            a *= x;
            a %= p;
        }
        this->p = p;
        step = i;
        inv = mod_inverse(a, p);
    }
    int get(int y)
    {
        long long z = y;
        for(int j=0; j<p; j+=step){
            if(m.find(z) != m.end())
                return j + m[z];
            z *= inv;
            z %= p;
        }
        return -1;
    }
};

int main()
{
    int p, r, q;
    cin >> p >> r >> q;
    discreteLogarithm dl(r, p);

    while(--q >= 0){
        int a, b, c;
        cin >> a >> b >> c;

        // a*x^2 + b*x + c = 0 → (a-s)^2 = t に変換する
        long long s = (-b * mod_inverse(2 * a % p, p)) % p;
        long long t = (s * s - c * mod_inverse(a, p)) % p;
        s += p;
        s %= p;
        t += p;
        t %= p;
        if(t == 0){
            cout << s << endl;
            continue;
        }

        int e = dl.get(t);
        if(e == -1 || e % 2 != 0){
            cout << -1 << endl;
            continue;
        }

        long long tmp = power(r, e/2, p);
        vector<long long> ans;
        ans.push_back(((s + tmp) % p + p) % p);
        ans.push_back(((s - tmp) % p + p) % p);
        if(ans[0] == ans[1]){
            cout << ans[0] << endl;
        }
        else{
            sort(ans.begin(), ans.end());
            cout << ans[0] << ' ' << ans[1] << endl;
        }
    }

    return 0;
}
0