Trung bình 0 đã giải Tỷ lệ chấp nhận 0%

Pandigital Products

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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.

Ràng buộc

1 ≤ n ≤ 9

Ví dụ

Ví dụ 1
Đầu vào:
n = 4

Kết quả mong đợi:
expected = 12
Đầu vào:
long long n = 4;

Kết quả mong đợi:
long long expected = 12;
Đầu vào:
long n = 4;

Kết quả mong đợi:
long expected = 12;

Giải thích

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

Ví dụ 2
Đầu vào:
n = 3

Kết quả mong đợi:
expected = 0
Đầu vào:
long long n = 3;

Kết quả mong đợi:
long long expected = 0;
Đầu vào:
long n = 3;

Kết quả mong đợi:
long expected = 0;

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

Store products in a set so each is added once.

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.