結果

問題 No.1498 Factorization from -1 to 1
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-05-04 00:10:04
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 321 ms / 3,000 ms
コード長 4,722 bytes
コンパイル時間 1,525 ms
コンパイル使用メモリ 109,556 KB
実行使用メモリ 13,676 KB
最終ジャッジ日時 2023-09-04 12:37:37
合計ジャッジ時間 7,509 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
9,008 KB
testcase_01 AC 55 ms
8,784 KB
testcase_02 AC 55 ms
7,584 KB
testcase_03 AC 166 ms
12,736 KB
testcase_04 AC 314 ms
13,036 KB
testcase_05 AC 320 ms
13,676 KB
testcase_06 AC 321 ms
12,852 KB
testcase_07 AC 320 ms
12,092 KB
testcase_08 AC 320 ms
12,128 KB
testcase_09 AC 317 ms
12,084 KB
testcase_10 AC 60 ms
9,600 KB
testcase_11 AC 58 ms
8,512 KB
testcase_12 AC 59 ms
7,748 KB
testcase_13 AC 59 ms
8,744 KB
testcase_14 AC 59 ms
10,940 KB
testcase_15 AC 54 ms
7,544 KB
testcase_16 AC 55 ms
7,540 KB
testcase_17 AC 54 ms
7,484 KB
testcase_18 AC 54 ms
7,476 KB
testcase_19 AC 54 ms
7,484 KB
testcase_20 AC 54 ms
9,384 KB
testcase_21 AC 54 ms
7,484 KB
testcase_22 AC 54 ms
7,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, 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)); }


// 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;
}

// 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;
  }
}

// (a / 2) mod m
long div2(long a, long m)
in {
  assert(1 <= m && m < 1L << 62, "div2: 1 <= m < 2^62 must hold");
  assert(m % 2 != 0, "div2: m must not be divisible by 2");
  assert(0 <= a && a < m, "div2: 0 <= a < m must hold");
}
do {
  return (a + (a % 2) * m) / 2;
}

// (a / 3) mod m
long div3(long a, long m)
in {
  assert(1 <= m && m < 1L << 61, "div3: 1 <= m < 2^61 must hold");
  assert(m % 3 != 0, "div3: m must not be divisible by 3");
  assert(0 <= a && a < m, "div3: 0 <= a < m must hold");
}
do {
  return (a + (((3 - a % 3) * (m % 3)) % 3) * m) / 3;
}

// 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 << 31, "modSqrt: p < 2^31 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] mul(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 = mul(g, f);
    f = mul(f, f);
  }
  assert(g[1] == 0);
  return (g[0] < p - g[0]) ? [g[0], p - g[0]] : [p - g[0], g[0]];
}


enum N = 10^^5 + 10;

void main() {
  auto lpf = iota(N).array;
  foreach (p; 2 .. N) {
    if (lpf[p] == p) {
      for (int n = 2 * p; n < N; n += p) {
        chmin(lpf[n], p);
      }
    }
  }
  auto pss = new int[][N];
  foreach (p; 2 .. N) {
    if (lpf[p] == p) {
      foreach (x; modSqrt(-1, p)) {
        for (long n = x; n < N; n += p) {
          pss[cast(int)(n)] ~= p;
        }
      }
    }
  }
  
  try {
    for (; ; ) {
      const Q = readInt();
      foreach (q; 0 .. Q) {
        const n = readInt();
        
        long m = 1L * n * n + 1;
        long[] ans;
        foreach (p; pss[n]) {
          for (; m % p == 0; m /= p) {
            ans ~= p;
          }
        }
        if (m > 1) {
          ans ~= m;
        }
        
        foreach (index, p; ans) {
          if (index > 0) write(" ");
          write(p);
        }
        writeln;
      }
    }
  } catch (EOFException e) {
  }
}
0