आसान 0 हल किए गए 0% स्वीकृति

Sum of Multiples

Add values below a limit divisible by any supplied factor.

आप क्या सीखेंगे

  • Test a number against several factors
  • Avoid counting a value twice
  • Accumulate a total below a limit

समस्या

अनुवाद दिखाएँ

Consider positive integers strictly below limit. Include a value if it is divisible by at least one nonzero factor. Count each included value once even if several factors divide it. Ignore zero factors. Factors [3, 5] give the familiar multiples-of-3-or-5 task.

Implement sum_of_multiples(limit, factors).

Input

limit: The exclusive upper bound.

factors: The factors whose multiples are eligible.

Output

Return the sum of distinct eligible positive integers.

सीमाएँ

0 ≤ limit ≤ 100,000. There may be 0 through 20 factors, each between 0 and 100,000. Duplicate factors are allowed.

उदाहरण

उदाहरण 1
इनपुट:
limit = 12
factors = [3, 5]

अपेक्षित आउटपुट:
expected = 33
इनपुट:
long long limit = 12;
std::vector<long long> factors = {3, 5};

अपेक्षित आउटपुट:
long long expected = 33;
इनपुट:
long limit = 12;
long[] factors = new long[] {3, 5};

अपेक्षित आउटपुट:
long expected = 33;

व्याख्या

The included values are 3, 5, 6, 9, and 10. Their sum is 33.

उदाहरण 2
इनपुट:
limit = 10
factors = [2, 4]

अपेक्षित आउटपुट:
expected = 20
इनपुट:
long long limit = 10;
std::vector<long long> factors = {2, 4};

अपेक्षित आउटपुट:
long long expected = 20;
इनपुट:
long limit = 10;
long[] factors = new long[] {2, 4};

अपेक्षित आउटपुट:
long expected = 20;

व्याख्या

Include 2, 4, 6, and 8. Values 4 and 8 are counted once despite matching both factors.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Only values divisible by at least one usable factor should contribute once.

संकेत 2

Use inclusion-exclusion to combine overlapping factor multiples without double counting.

संकेत 3

Ignore zero factors and duplicates before doing any arithmetic.

अपेक्षित जटिलता

O(2^m) time, O(1) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।