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

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

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

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

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

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