公式

C - Tetrahedral Number 解説 by en_translator


Since \(x+y+z\leq N\), it is sufficient to search within \(x,y,z \leq N\). In order to find tuples in lexicographical order, one can loop in ascending order for \(x\), \(y\), and \(z\) in this order.

Sample code (Python)

N=int(input())

for x in range(N+1):
  for y in range(N+1):
    for z in range(N+1):
      if x+y+z<=N:
        print(x,y,z)

Bonus:
When \(N=21\), there are \(\binom{21+3}{3}=2024\) conforming tuples. あけましておめでとうございます!

投稿日時:
最終更新: