難しい 0件解決済み 正答率0%

Non-Abundant Sums

Add up the numbers up to a limit that cannot be written as the sum of two abundant numbers.

このチャレンジで学べること

  • Compute divisor sums for a range with a sieve
  • Mark reachable sums with a boolean array
  • Cut a double loop short once sums pass the limit

問題

翻訳を表示

A number is abundant when its proper divisors (all divisors except itself) add up to more than it. 12 is the smallest: 1 + 2 + 3 + 4 + 6 = 16. Some numbers can be written as the sum of two abundant numbers (the same one may be used twice, as in 24 = 12 + 12) and some cannot. Add up every positive integer from 1 to limit that cannot.

Implement non_abundant_sums(limit).

Find the divisor sums for every number at once with a sieve, list the abundant numbers, then mark every pair sum that stays within the limit.

Input

limit: The largest number to check.

Output

Return the sum of all n with 1 ≤ n ≤ limit that are not the sum of two abundant numbers.

制約

1 ≤ limit ≤ 10,000

例

例 1
入力:
limit = 23

期待される出力:
expected = 276
入力:
long long limit = 23;

期待される出力:
long long expected = 276;
入力:
long limit = 23;

期待される出力:
long expected = 276;

解説

No two abundant numbers add up to 23 or less, so every number counts: 1 + 2 + ... + 23 = 276.

例 2
入力:
limit = 30

期待される出力:
expected = 411
入力:
long long limit = 30;

期待される出力:
long long expected = 411;
入力:
long limit = 30;

期待される出力:
long expected = 411;

解説

24 = 12 + 12 and 30 = 12 + 18 are left out, so the answer is 465 − 24 − 30 = 411.

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

For each i, add i to the divisor sum of 2i, 3i, 4i, ...

ヒント 2

Collect the abundant numbers in increasing order.

ヒント 3

For each pair a ≤ b, stop the inner loop once a + b passes the limit.

目安の計算量

O(limit log limit + A²) time for A abundant numbers, O(limit) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。