公式

C - 3^A 解説 by en_translator


Let \(C_k\) \((0\le C_k < 3)\) be the \(3^k\)s place \((0\le k\le 10)\) in the ternary representation of \(M\). Then, \(\displaystyle M=\sum_{k=0}^{10} C_k3^k\) holds.

Then we can construct \(A\) as follows:

  • Let \(A=()\).
  • For \(k=0,1,\ldots,10\) in order, do the following:
    • Insert \(C_k\) copies of \(k\) to the tail of \(A\).

For example, \(7_{(10)}=21_{(3)}\), for which the procedure above yields \(A=(0,1,1)\). As \(3^0+3^1+3^1=7\), this \(A\) indeed satisfies the conditions.

One can assert that the sequence \(A\) of length \(N\) obtained by the procedure above always satisfies the condition \(1\le N\le 20\) by actually constructing \(A\) for all integers \(M\) between \(1\) and \(10^5\) and checking their lengths.

Sample code (Python3)

M = int(input())
A = []
for k in range(11):
    A += [k] * (M % 3)
    M //= 3
print(len(A))
print(*A)

投稿日時:
最終更新: