結果

問題 No.551 夏休みの思い出(2)
ユーザー risujirohrisujiroh
提出日時 2019-02-02 04:01:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,535 ms / 4,000 ms
コード長 2,464 bytes
コンパイル時間 1,646 ms
コンパイル使用メモリ 172,140 KB
実行使用メモリ 24,276 KB
最終ジャッジ日時 2023-08-16 08:31:22
合計ジャッジ時間 21,716 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
4,380 KB
testcase_01 AC 13 ms
4,376 KB
testcase_02 AC 13 ms
4,376 KB
testcase_03 AC 14 ms
4,376 KB
testcase_04 AC 15 ms
4,376 KB
testcase_05 AC 17 ms
4,384 KB
testcase_06 AC 20 ms
4,376 KB
testcase_07 AC 13 ms
4,376 KB
testcase_08 AC 13 ms
4,376 KB
testcase_09 AC 13 ms
4,380 KB
testcase_10 AC 12 ms
4,380 KB
testcase_11 AC 13 ms
4,380 KB
testcase_12 AC 13 ms
4,380 KB
testcase_13 AC 13 ms
4,376 KB
testcase_14 AC 13 ms
4,380 KB
testcase_15 AC 13 ms
4,376 KB
testcase_16 AC 13 ms
4,380 KB
testcase_17 AC 64 ms
13,772 KB
testcase_18 AC 147 ms
24,136 KB
testcase_19 AC 130 ms
24,140 KB
testcase_20 AC 132 ms
24,152 KB
testcase_21 AC 171 ms
24,260 KB
testcase_22 AC 26 ms
6,216 KB
testcase_23 AC 39 ms
9,580 KB
testcase_24 AC 121 ms
23,352 KB
testcase_25 AC 58 ms
13,248 KB
testcase_26 AC 117 ms
22,892 KB
testcase_27 AC 421 ms
24,232 KB
testcase_28 AC 407 ms
24,272 KB
testcase_29 AC 224 ms
24,240 KB
testcase_30 AC 375 ms
24,132 KB
testcase_31 AC 240 ms
24,140 KB
testcase_32 AC 356 ms
24,128 KB
testcase_33 AC 503 ms
24,192 KB
testcase_34 AC 318 ms
24,276 KB
testcase_35 AC 230 ms
24,140 KB
testcase_36 AC 279 ms
24,244 KB
testcase_37 AC 2,535 ms
24,216 KB
testcase_38 AC 1,769 ms
24,136 KB
testcase_39 AC 533 ms
24,132 KB
testcase_40 AC 1,272 ms
24,256 KB
testcase_41 AC 498 ms
24,248 KB
testcase_42 AC 1,976 ms
24,196 KB
testcase_43 AC 529 ms
24,212 KB
testcase_44 AC 841 ms
24,256 KB
testcase_45 AC 629 ms
24,260 KB
testcase_46 AC 2,340 ms
24,128 KB
testcase_47 AC 13 ms
4,376 KB
testcase_48 AC 13 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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 = 5e5;
  static unordered_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 < (p - 1 + m - 1) / 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