結果

問題 No.300 平方数
ユーザー むらためむらため
提出日時 2019-07-25 13:44:52
言語 Nim
(2.0.2)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,364 bytes
コンパイル時間 3,692 ms
コンパイル使用メモリ 73,120 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-14 23:48:11
合計ジャッジ時間 5,986 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import sequtils,strutils,math,tables
proc toCountSeq[T](x:seq[T]) : seq[tuple[k:T,v:int]] = toSeq(x.toCountTable().pairs)

proc powerWhenTooBig(x,n:int,modulo:int = 0): int =
  proc mul(x,n,modulo:int):int =
    if n == 0: return 0
    if n == 1: return x
    result = mul(x,n div 2,modulo) mod modulo
    result = (result * 2) mod modulo
    result = (result + x * (n mod 2 == 1).int) mod modulo
    if n == 0: return 1
  if n == 1: return x
  let
    pow_2 = powerWhenTooBig(x,n div 2,modulo)
    odd = if n mod 2 == 1: x else: 1
  if modulo > 0:
    const maybig = int.high.float.sqrt.int div 2
    if pow_2 > maybig or odd > maybig:
      result = mul(pow_2,pow_2,modulo)
      result = mul(result,odd,modulo)
    else:
      result = (pow_2 * pow_2) mod modulo
      result = (result * odd) mod modulo
  else:
    return pow_2 * pow_2 * odd
proc millerRabinIsPrime(n:int):bool = # O(log n)
  proc ctz(n:int):int{.importC: "__builtin_ctzll", noDecl .} # 01<0000> -> 4
  proc power(x,n:int,modulo:int = 0): int =
    if n == 0: return 1
    if n == 1: return x
    let pow_2 = power(x,n div 2,modulo)
    result = pow_2 * pow_2 * (if n mod 2 == 1: x else: 1)
    if modulo > 0: result = result mod modulo
  if n <= 1 : return false
  if n == 2 or n == 3 or n == 5: return true
  if n mod 2 == 0: return false
  let
    s = ctz(n - 1)
    d = (n - 1) div (1 shl s)
  var a_list = @[2, 7, 61]
  if n >= 4_759_123_141 and n < 341_550_071_728_321:
    a_list = @[2, 3, 5, 7, 11, 13, 17]
  if n in a_list : return true
  for a in a_list:
    if powerWhenTooBig(a,d,n) == 1 : continue
    let notPrime = (0..<s).allIt(powerWhenTooBig(a,d*(1 shl it),n) != n-1)
    if notPrime : return false
  return true
proc squareFormFactor(n:int):int =
  if millerRabinIsPrime(n) : return n
  proc check(k:int):int =
    proc √(x:int):int = x.float.sqrt.int
    if n <= 1 : return n
    if n mod 2 == 0 : return 2
    if √(n) * √(n) == n : return √(n)
    let ncb = n.float.cbrt.int
    if ncb * ncb * ncb == n : return ncb
    var P1 = √(k * n)
    var Q2 = 1
    var Q1 = k * n - P1 * P1
    while √(Q1) * √(Q1) != Q1:
      let b = (√(k * n) + P1 ) div Q1
      let ppP = P1
      let pP = b * Q1 - P1
      let ppQ = Q2
      P1 = pP
      Q2 = Q1
      Q1 = ppQ + b * (ppP - pP)
    block:
      if Q1 == 0 : return check(k + 1)
      let
        b = (√(k * n) - P1 ) div Q1
        P0 = b * √(Q1) + P1
        Q0 = √(Q1)
        QX = (k*n - P0 * P0) div Q0
      P1 = P0
      Q1 = QX
      Q2 = Q0
    while true:
      let b = (√(k * n) + P1 ) div Q1
      let ppP = P1
      let pP = b * Q1 - P1
      let ppQ = Q2
      let pQ = Q1
      let q = ppQ + b * (ppP - pP)
      P1 = pP
      Q2 = Q1
      Q1 = q
      if ppP == pP or q == pQ: break
    let f = gcd(n,P1)
    if f != 1 and f != n : return f
    else: return check(k+1)
  return check(1)
  

proc getFactors(n:int):seq[int]=
  if n == 1 : return @[1]
  if n == 0 : return @[0]
  result = @[]
  var m = n
  while true:
    let p = m.squareFormFactor()
    if p.millerRabinIsPrime(): result .add p
    else: result .add p.getFactors()
    if p == m: return
    m = m div p



let x = stdin.readLine().parseInt()
if x > 10000 and x.millerRabinIsPrime(): quit $x,0
let factors = x.getFactors().toCountSeq()
var ans = 1
for f in factors:
  if f.v mod 2 == 0 : continue
  ans *= f.k
echo ans
0