結果

問題 No.551 夏休みの思い出(2)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-06-10 18:57:12
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 54 ms / 4,000 ms
コード長 3,890 bytes
コンパイル時間 9,246 ms
コンパイル使用メモリ 666,496 KB
実行使用メモリ 4,632 KB
最終ジャッジ日時 2023-09-04 01:37:04
合計ジャッジ時間 13,087 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 3 ms
4,356 KB
testcase_03 AC 5 ms
4,356 KB
testcase_04 AC 9 ms
4,356 KB
testcase_05 AC 16 ms
4,588 KB
testcase_06 AC 22 ms
4,620 KB
testcase_07 AC 2 ms
4,356 KB
testcase_08 AC 2 ms
4,356 KB
testcase_09 AC 2 ms
4,356 KB
testcase_10 AC 2 ms
4,356 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 2 ms
4,356 KB
testcase_13 AC 2 ms
4,356 KB
testcase_14 AC 2 ms
4,356 KB
testcase_15 AC 2 ms
4,356 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 3 ms
4,352 KB
testcase_18 AC 2 ms
4,352 KB
testcase_19 AC 2 ms
4,356 KB
testcase_20 AC 2 ms
4,356 KB
testcase_21 AC 2 ms
4,356 KB
testcase_22 AC 3 ms
4,352 KB
testcase_23 AC 2 ms
4,356 KB
testcase_24 AC 2 ms
4,352 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 2 ms
4,352 KB
testcase_27 AC 51 ms
4,524 KB
testcase_28 AC 50 ms
4,608 KB
testcase_29 AC 48 ms
4,504 KB
testcase_30 AC 50 ms
4,552 KB
testcase_31 AC 47 ms
4,576 KB
testcase_32 AC 50 ms
4,540 KB
testcase_33 AC 50 ms
4,620 KB
testcase_34 AC 50 ms
4,632 KB
testcase_35 AC 48 ms
4,576 KB
testcase_36 AC 48 ms
4,544 KB
testcase_37 AC 54 ms
4,608 KB
testcase_38 AC 53 ms
4,584 KB
testcase_39 AC 52 ms
4,572 KB
testcase_40 AC 53 ms
4,616 KB
testcase_41 AC 51 ms
4,612 KB
testcase_42 AC 54 ms
4,604 KB
testcase_43 AC 52 ms
4,584 KB
testcase_44 AC 52 ms
4,592 KB
testcase_45 AC 51 ms
4,576 KB
testcase_46 AC 53 ms
4,552 KB
testcase_47 AC 2 ms
4,356 KB
testcase_48 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


// a^-1 (mod m)
long modInv(long a, long m)
in {
  assert(m > 0, "modInv: m > 0 must hold");
}
do {
  long b = m, x = 1, y = 0, t;
  for (; ; ) {
    t = a / b;
    a -= t * b;
    if (a == 0) {
      assert(b == 1 || b == -1, "modInv: gcd(a, m) != 1");
      if (b == -1) {
        y = -y;
      }
      return (y < 0) ? (y + m) : y;
    }
    x -= t * y;
    t = b / a;
    b -= t * a;
    if (b == 0) {
      assert(a == 1 || a == -1, "modInv: gcd(a, m) != 1");
      if (a == -1) {
        x = -x;
      }
      return (x < 0) ? (x + m) : x;
    }
    y -= t * x;
  }
}


// xorshift
uint xrand() {
  static uint x = 314159265, y = 358979323, z = 846264338, w = 327950288;
  uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;
}

// Jacobi symbol (a/m)
int jacobi(long a, long m)
in {
  assert(m > 0, "jacobi: m > 0 must hold");
  assert(m & 1, "jacobi: m must be odd");
}
do {
  import core.bitop : bsf;
  import std.algorithm.mutation : swap;
  int s = 1;
  if (a < 0) a = a % m + m;
  for (; m > 1; ) {
    a %= m;
    if (a == 0) return 0;
    const r = bsf(a);
    if ((r & 1) && ((m + 2) & 4)) s = -s;
    a >>= r;
    if (a & m & 2) s = -s;
    swap(a, m);
  }
  return s;
}

// sqrt(a) (mod p)
//   p: be prime
//   (b + sqrt(b^2 - a))^((p+1)/2) in F_p(sqrt(b^2 - a))
long[] modSqrt(long a, long p)
in {
  assert(p < 1L << 32, "modSqrt: p < 2^32 must hold");
  assert(-p * p <= a && a <= p * p, "modSqrt: -p^2 <= a <= p^2 must hold");
}
do {
  if (p == 2) return [a & 1];
  const j = jacobi(a, p);
  if (j == 0) return [0];
  if (j == -1) return [];
  long b, d;
  for (; ; ) {
    b = xrand() % p;
    d = (b * b - a) % p;
    if (d < 0) d += p;
    if (jacobi(d, p) == -1) break;
  }
  long[2] multiply(in long[2] x, in long[2] y) {
    return [(x[0] * y[0] + d * ((x[1] * y[1]) % p)) % p,
            (x[0] * y[1] + x[1] * y[0]) % p];
  }
  long[2] f = [b, 1], g = [1, 0];
  for (long e = (p + 1) >> 1; e; e >>= 1) {
    if (e & 1) g = multiply(g, f);
    f = multiply(f, f);
  }
  assert(g[1] == 0);
  return (g[0] < p - g[0]) ? [g[0], p - g[0]] : [p - g[0], g[0]];
}


void main() {
  try {
    for (; ; ) {
      const P = readLong();
      const R = readLong();
      const Q = readInt();
      foreach (q; 0 .. Q) {
        const A = readLong();
        const B = readLong();
        const C = readLong();
        long[] ans;
        foreach (d; modSqrt(B * B - 4 * A * C, P)) {
          long x = (-B + d) * modInv(2 * A, P) % P;
          x = (x % P + P) % P;
          ans ~= x;
        }
        ans.sort;
        if (ans.empty) {
          writeln(-1);
        } else {
          writeln(ans.to!string.replaceAll(regex(`[\[\],]`), ""));
        }
      }
    }
  } catch (EOFException e) {
  }
}
0