結果

問題 No.1006 Share an Integer
ユーザー simansiman
提出日時 2020-09-11 06:18:13
言語 Ruby
(3.3.0)
結果
WA  
実行時間 -
コード長 764 bytes
コンパイル時間 482 ms
コンパイル使用メモリ 11,320 KB
実行使用メモリ 31,080 KB
最終ジャッジ日時 2023-08-26 02:26:53
合計ジャッジ時間 20,424 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
15,236 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 81 ms
15,352 KB
testcase_04 WA -
testcase_05 AC 79 ms
15,268 KB
testcase_06 WA -
testcase_07 AC 81 ms
15,136 KB
testcase_08 AC 82 ms
15,276 KB
testcase_09 WA -
testcase_10 AC 80 ms
15,324 KB
testcase_11 AC 1,098 ms
23,964 KB
testcase_12 WA -
testcase_13 TLE -
testcase_14 AC 1,991 ms
31,080 KB
testcase_15 AC 1,700 ms
28,824 KB
testcase_16 AC 817 ms
21,888 KB
testcase_17 AC 1,122 ms
24,248 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 1,783 ms
29,156 KB
testcase_21 TLE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.map { |nums| nums.join(' ') }
0