結果

問題 No.551 夏休みの思い出(2)
ユーザー rsk0315rsk0315
提出日時 2019-03-30 04:02:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 4,000 ms
コード長 1,850 bytes
コンパイル時間 697 ms
コンパイル使用メモリ 59,240 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-07 06:08:04
合計ジャッジ時間 3,510 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 3 ms
4,384 KB
testcase_05 AC 5 ms
4,384 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,384 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,388 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 37 ms
4,384 KB
testcase_28 AC 18 ms
4,380 KB
testcase_29 AC 28 ms
4,380 KB
testcase_30 AC 55 ms
4,384 KB
testcase_31 AC 16 ms
4,384 KB
testcase_32 AC 45 ms
4,380 KB
testcase_33 AC 18 ms
4,380 KB
testcase_34 AC 30 ms
4,380 KB
testcase_35 AC 17 ms
4,384 KB
testcase_36 AC 18 ms
4,384 KB
testcase_37 AC 20 ms
4,384 KB
testcase_38 AC 40 ms
4,380 KB
testcase_39 AC 19 ms
4,380 KB
testcase_40 AC 19 ms
4,384 KB
testcase_41 AC 75 ms
4,380 KB
testcase_42 AC 19 ms
4,380 KB
testcase_43 AC 19 ms
4,380 KB
testcase_44 AC 18 ms
4,384 KB
testcase_45 AC 18 ms
4,380 KB
testcase_46 AC 35 ms
4,380 KB
testcase_47 AC 1 ms
4,380 KB
testcase_48 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdint>
#include <vector>
#include <set>

intmax_t modpow(intmax_t base, intmax_t iexp, intmax_t mod) {
  intmax_t res = 1;
  for (intmax_t dbl = base; iexp; iexp >>= 1) {
    if (iexp & 1) (res *= dbl) %= mod;
    (dbl *= dbl) %= mod;
  }
  return res;
}

intmax_t moddiv(intmax_t n, intmax_t d, intmax_t mod) {
  return n * modpow(d, mod-2, mod) % mod;
}

std::vector<intmax_t> modsqrt(intmax_t n, intmax_t p) {

  if (p % 4 == 3) {
    intmax_t r = modpow(n, (p+1)/4, p);
    if (r*r % p == n) return {r, p-r};
    return {};
  }

  intmax_t s = __builtin_ctzll(p-1);
  intmax_t q = (p-1) >> s;

  intmax_t z;
  for (z = 2; z < p; ++z)
    if (modpow(z, (p-1)/2, p) == p-1) break;

  intmax_t m = s;
  intmax_t c = modpow(z, q, p);
  intmax_t t = modpow(n, q, p);
  intmax_t r = modpow(n, (q+1)/2, p);

  while (true) {
    if (t == 0) return {0};
    if (t == 1) return {r, p-r};

    intmax_t i = 0;
    for (intmax_t tt = t; tt != 1; ++i)
      (tt *= tt) %= p;

    if (i == m) return {};

    intmax_t b = c;
    for (intmax_t j = 0; j < m-i-1; ++j)
      (b *= b) %= p;

    m = i;
    c = b * b % p;
    (t *= c) %= p;
    (r *= b) %= p;
  }
}

int main() {
  intmax_t P, R;
  size_t Q;
  scanf("%jd %jd %zu", &P, &R, &Q);

  for (size_t i = 0; i < Q; ++i) {
    intmax_t A, B, C;
    scanf("%jd %jd %jd", &A, &B, &C);

    // (-b +- sqrt(b*b-4*a*c)) / (2*a)

    std::set<intmax_t> sol;
    intmax_t S = (B*B - 4*A*C) % P;
    if (S < 0) S += P;
    for (intmax_t s: modsqrt(S, P)) {
      intmax_t n = (s - B) % P;
      if (n < 0) n += P;
      intmax_t a = 2*A % P;
      sol.insert(moddiv(n, a, P));
    }
    if (sol.empty()) {
      puts("-1");
    } else {
      bool out = false;
      for (auto x: sol) {
        printf("%s%jd", out? " ":"", x);
        out = true;
      }
      puts("");
    }
  }
}
0