結果

問題 No.1006 Share an Integer
ユーザー simansiman
提出日時 2020-09-11 06:18:42
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,710 ms / 2,000 ms
コード長 769 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 11,512 KB
実行使用メモリ 31,132 KB
最終ジャッジ日時 2023-08-26 02:29:08
合計ジャッジ時間 17,082 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
15,248 KB
testcase_01 AC 69 ms
15,036 KB
testcase_02 AC 68 ms
15,100 KB
testcase_03 AC 72 ms
15,040 KB
testcase_04 AC 71 ms
15,112 KB
testcase_05 AC 70 ms
15,116 KB
testcase_06 AC 70 ms
15,164 KB
testcase_07 AC 71 ms
15,160 KB
testcase_08 AC 70 ms
15,228 KB
testcase_09 AC 68 ms
15,200 KB
testcase_10 AC 70 ms
15,304 KB
testcase_11 AC 993 ms
24,000 KB
testcase_12 AC 1,561 ms
30,196 KB
testcase_13 AC 1,710 ms
31,132 KB
testcase_14 AC 1,653 ms
30,984 KB
testcase_15 AC 1,409 ms
28,560 KB
testcase_16 AC 697 ms
21,888 KB
testcase_17 AC 941 ms
24,180 KB
testcase_18 AC 1,407 ms
28,700 KB
testcase_19 AC 1,334 ms
28,180 KB
testcase_20 AC 1,476 ms
29,148 KB
testcase_21 AC 1,675 ms
31,072 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

X = gets.to_i

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

2.upto(X) do |x|
  next if SPF[x] != -1

  x.step(X, x) do |n|
    next if SPF[n] != -1

    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