Tous les défis
Moyen 0 résolus 0 % de réussite

Pandigital Products

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

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

1 ≤ n ≤ 9

Exemples

Exemple 1
Entrée :
n = 4

Sortie attendue :
expected = 12
Entrée :
long long n = 4;

Sortie attendue :
long long expected = 12;
Entrée :
long n = 4;

Sortie attendue :
long expected = 12;

Explication

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

Exemple 2
Entrée :
n = 3

Sortie attendue :
expected = 0
Entrée :
long long n = 3;

Sortie attendue :
long long expected = 0;
Entrée :
long n = 3;

Sortie attendue :
long expected = 0;

Explication

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

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

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

Indice 2

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

Indice 3

Store products in a set so each is added once.

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.