모든 챌린지
어려움 1명 해결 정답률 100.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에 맞게 수정되었습니다. 위의 설명과 제약 조건을 기준으로 풀어 주세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 ННикита Python 955 ms - 4/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 풀이

스포일러: 아래에 전체 풀이 1개가 있습니다 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.