Средне Решено: 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. Следуйте условию и ограничениям выше.

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

Принятых решений пока нет. Решите задачу и займите первое место.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.