結果

問題 No.551 夏休みの思い出(2)
ユーザー risujirohrisujiroh
提出日時 2019-02-02 03:45:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,450 bytes
コンパイル時間 1,730 ms
コンパイル使用メモリ 173,912 KB
実行使用メモリ 9,000 KB
最終ジャッジ日時 2023-08-16 07:50:12
合計ジャッジ時間 8,834 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,084 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 6 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 6 ms
4,380 KB
testcase_20 AC 6 ms
4,384 KB
testcase_21 AC 6 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 3 ms
4,380 KB
testcase_24 AC 5 ms
4,384 KB
testcase_25 AC 4 ms
4,376 KB
testcase_26 AC 5 ms
4,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;
using uint = unsigned int;
using lint = long long int;
using ulint = unsigned long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T, class U> void assign(V<T>& v, int n, const U& a) { v.assign(n, a); }
template<class T, class... Args> void assign(V<T>& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); }


lint tmod(lint a, lint p) {
  assert(p > 0);
  return (a %= p) < 0 ? a + p : a;
}

int mod_pow(lint a, lint n, int p) {
  assert(n >= 0);
  a = tmod(a, p);
  lint res = 1;
  while (n > 0) {
    if (n & 1) {
      (res *= a) %= p;
    }
    (a *= a) %= p;
    n >>= 1;
  }
  return res;
}

lint mod_inv(lint a, lint p) {
  a = tmod(a, p);
  lint b = p, x = 1, u = 0;
  while (b) {
    lint q = a / b;
    swap(a -= q * b, b);
    swap(x -= q * u, u);
  }
  return a == 1 ? tmod(x, p) : -1;
}

int mod_log(lint g, lint x, int p) {
  g = tmod(g, p);
  x = tmod(x, p);
  int m = ceil(sqrt(p - 1));
  static map<int, int> mp;
  lint a = 1;
  if (mp.empty()) {
    for (int j = 0; j < m; ++j) {
      mp[a] = j;
      (a *= g) %= p;
    }
  }
  g = mod_inv(mod_pow(g, m, p), p), a = 1;
  for (int i = 0; i < m; ++i) {
    if (mp.count(a * x % p)) {
      return i * m + mp[a * x % p];
    }
    (a *= g) %= p;
  }
  return -1;
}

template<class Int> Int ext_gcd(Int a, Int b, Int& x, Int& y) {
  Int u = y = 0, v = x = 1; 
  while (b) {
    Int q = a / b;
    swap(a -= q * b, b);
    swap(x -= q * u, u);
    swap(y -= q * v, v);
  }
  return a;
}

int mod_root(lint a, lint n, int p, int g) {
  a = tmod(a, p);
  if (a == 0) {
    return n > 0 ? 0 : -1;
  }
  n = tmod(n, p - 1);
  int e = mod_log(g, a, p);
  assert(e != -1);
  lint x, y;
  int d = ext_gcd<lint>(n, p - 1, x, y);
  if (e % d) return -1;
  return mod_pow(g, e / d * x, p);
}

int main() {
  cin.tie(nullptr); ios_base::sync_with_stdio(false);
  int p, g, q; cin >> p >> g >> q;
  while (q--) {
    lint a, b, c; cin >> a >> b >> c;
    lint D = tmod(b * b - 4 * a * c, p);
    lint sqrt_D = mod_root(D, 2, p, g);
    if (sqrt_D == -1) {
      cout << -1 << '\n';
      continue;
    }
    lint x = -b + sqrt_D;
    lint y = -b - sqrt_D;
    x = tmod(mod_inv(2 * a, p) * x, p);
    y = tmod(mod_inv(2 * a, p) * y, p);
    if (x > y) swap(x, y);
    cout << x;
    if (y != x) {
      cout << ' ' << y;
    }
    cout << '\n';
  }
}
0