ยาก 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

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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก