मध्यम 0 हल किए गए 0% स्वीकृति

Pandigital Products

Add up the products whose equation a × b = c uses the digits 1 to n exactly once.

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

  • Generate permutations of digits
  • Try every split into three parts
  • Deduplicate results with a set

समस्या

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

Write a multiplication a × b = c and look at all the digits in a, b and c together. The equation is n-pandigital when those digits are 1, 2, ..., n, each used exactly once (no zeros). For n = 4, 3 × 4 = 12 qualifies. Find every product c that appears in some n-pandigital equation.

Implement pandigital_products(n).

Input

n: The largest digit to use.

Output

Return the sum of the distinct products c. A product reached by more than one equation is added once. If there is none, return 0.

सीमाएँ

1 ≤ n ≤ 9

उदाहरण

उदाहरण 1
इनपुट:
n = 4

अपेक्षित आउटपुट:
expected = 12
इनपुट:
long long n = 4;

अपेक्षित आउटपुट:
long long expected = 12;
इनपुट:
long n = 4;

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

व्याख्या

3 × 4 = 12 uses 1, 2, 3, 4 once each and it is the only such equation, so the answer is 12.

उदाहरण 2
इनपुट:
n = 3

अपेक्षित आउटपुट:
expected = 0
इनपुट:
long long n = 3;

अपेक्षित आउटपुट:
long long expected = 0;
इनपुट:
long n = 3;

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

व्याख्या

With the digits 1, 2, 3 the only splits are like 1 × 2 = 3, which is false, so the answer is 0.

कैसे हल करें

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

संकेत 1

Every n-pandigital equation is an ordering of the digits 1 to n cut into three pieces.

संकेत 2

Try every cut point pair and check whether the first two pieces multiply to the third.

संकेत 3

Store products in a set so each is added once.

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

O(n! × n²) time, O(number of products) extra space

संदर्भ

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

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

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

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

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