Mittel 0 gelöst 0% Erfolgsquote

Pandigital Products

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

Das lernst du

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

Problem

Übersetzung anzeigen

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.

Einschränkungen

1 ≤ n ≤ 9

Beispiele

Beispiel 1
Eingabe:
n = 4

Erwartete Ausgabe:
expected = 12
Eingabe:
long long n = 4;

Erwartete Ausgabe:
long long expected = 12;
Eingabe:
long n = 4;

Erwartete Ausgabe:
long expected = 12;

Erläuterung

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

Beispiel 2
Eingabe:
n = 3

Erwartete Ausgabe:
expected = 0
Eingabe:
long long n = 3;

Erwartete Ausgabe:
long long expected = 0;
Eingabe:
long n = 3;

Erwartete Ausgabe:
long expected = 0;

Erläuterung

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

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

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

Hinweis 2

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

Hinweis 3

Store products in a set so each is added once.

Erwartete Komplexität

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

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.