結果

問題 No.2271 平方根の13桁精度近似計算
ユーザー tomeruntomerun
提出日時 2023-04-14 23:18:29
言語 Crystal
(1.11.2)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 1,201 bytes
コンパイル時間 12,077 ms
コンパイル使用メモリ 297,468 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 20:59:08
合計ジャッジ時間 13,496 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

n = read_line.to_i64
e = read_line.to_i
if e == 0
  puts 0
  exit
end
if n < 0
  n %= 5 ** e
  n += 5 ** e
end
ab = n.abs
a = Array.new(e + 1) do
  v = ab % 5
  ab //= 5
  n < 0 ? (5 - v) % 5 : v
end
if a[0] == 2 || a[0] == 3
  puts "NaN"
  exit
end
if a[0] == 0
  q = [[0, 0]]
elsif a[0] == 1
  q = [[1, 0], [4, 3]]
else
  q = [[2, 0], [3, 1]]
end
1.upto(e - 1) do |i|
  nq = [] of Array(Int32)
  q.each do |cand|
    sum = cand[i]
    1.upto(i - 1) do |j|
      sum += cand[j] * cand[i - j]
    end
    5.times do |j|
      if (j * cand[0] * 2 + sum) % 5 == a[i]
        carry = (j * cand[0] * 2 + sum) // 5
        nc = cand.dup
        nc[i] = j
        nc << carry
        nq << nc
      end
    end
  end
  if nq.empty?
    puts "NaN"
    exit
  end
  q = nq
end
q.each do |cand|
  ans = 0i64
  base = 1i64
  e.times do |i|
    ans += base * cand[i]
    base *= 5
  end
  while ans > 1 << 29
    ans -= 5 ** e
  end
  if ans < -(1 << 29)
    next
  end
  v = ans * ans - n
  c = 0
  while v != 0 && v % 5 == 0
    v //= 5
    c += 1
  end
  if v != 0 && c < e
    puts q.size
    puts q[0]
    puts ans
    puts c
    puts "invalid"
    while true
    end
  end
  puts ans
  exit
end
puts "NaN"
0