結果

問題 No.1006 Share an Integer
ユーザー simansiman
提出日時 2022-05-18 03:01:26
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,911 ms / 2,000 ms
コード長 765 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 11,504 KB
実行使用メモリ 41,056 KB
最終ジャッジ日時 2023-10-14 07:08:59
合計ジャッジ時間 19,108 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,296 KB
testcase_01 AC 83 ms
15,264 KB
testcase_02 AC 82 ms
15,124 KB
testcase_03 AC 81 ms
15,176 KB
testcase_04 AC 83 ms
15,216 KB
testcase_05 AC 82 ms
15,276 KB
testcase_06 AC 84 ms
15,336 KB
testcase_07 AC 83 ms
15,292 KB
testcase_08 AC 84 ms
15,344 KB
testcase_09 AC 83 ms
15,156 KB
testcase_10 AC 83 ms
15,128 KB
testcase_11 AC 1,056 ms
30,300 KB
testcase_12 AC 1,764 ms
39,512 KB
testcase_13 AC 1,911 ms
40,896 KB
testcase_14 AC 1,892 ms
41,056 KB
testcase_15 AC 1,627 ms
39,376 KB
testcase_16 AC 805 ms
27,552 KB
testcase_17 AC 1,079 ms
32,468 KB
testcase_18 AC 1,628 ms
37,744 KB
testcase_19 AC 1,548 ms
37,044 KB
testcase_20 AC 1,666 ms
38,180 KB
testcase_21 AC 1,880 ms
40,816 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

GC.disable

X = gets.to_i

SPF = Array.new(X + 1)
SPF[0] = 0
SPF[1] = 1

2.upto(X) do |x|
  next if SPF[x]

  x.step(X, x) do |n|
    next if SPF[n]

    SPF[n] = x
  end
end

def divisor_count(x)
  res = 1
  cur = -1
  cnt = 0

  while x != 1
    f = SPF[x]

    if cur != f
      res *= (cnt + 1)
      cur = f
      cnt = 1
    else
      cnt += 1
    end

    x /= f
  end

  res * (cnt + 1)
end

min_diff = Float::INFINITY
ans = []

1.upto(X / 2) do |a|
  b = X - a
  v1 = a - divisor_count(a)
  v2 = b - divisor_count(b)
  diff = (v1 - v2).abs

  if min_diff > diff
    min_diff = diff
    ans = [[a, b]]
    ans << [b, a] if a != b
  elsif min_diff == diff
    ans << [a, b]
    ans << [b, a] if a != b
  end
end

puts ans.sort.map { |nums| nums.join(' ') }
0