中級 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 つずつ開いてみましょう。

ヒント 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用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。