Skip to content

Conversation

@docto-rin
Copy link
Owner

return math.comb(m + n - 2, m - 1)
```

- 4分かかった。(math.combを調べるのに時間がかかった)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

気が向いたら math.comb を読んでもいいでしょう。(ライブラリーではどのような使われ方がするかが分からないのでとてもチューニングがされる傾向があります。)

@docto-rin
Copy link
Owner Author

docs: https://docs.python.org/3/library/math.html
source code (math): https://github.com/python/cpython/blob/main/Modules/mathmodule.c
source code (math.integer): https://github.com/python/cpython/blob/main/Modules/mathintegermodule.c

c実装かつ長いのでGPT-5を噛ませました...

どれくらいチューニングされているか

  • 閾値テーブルを複数用意して「64bit で確実に収まる範囲」を精密に切り取る。
  • 64bit に収まる場合は、テーブル×乗算×ビットシフトだけで終了。中間の多倍長生成ゼロ。
  • 収まらない場合でも、分割統治で多倍長乗算の特性に合わせ、同時に小さな部分式は再び 64bit fast path に落とし込む。
  • 典型的な入力分布(k が小さい、kn/2 より小さい方に寄せるなど)を前提に、対称性と再帰式を最大限活用。

要するに、「先に決め打ち高速経路を広く張る」「落ちたら分割統治で多倍長を賢く使う」「分割の各所で再び小経路に戻す」という三段構えです。標準ライブラリでもここまでやるか、というレベルで丁寧にチューニングされています。

余談:math.integerサブモジュールに切り出す提案がpython 3.15などで議論されているらしいです。
https://peps.python.org/pep-0791

for _ in range(1, m):
for col in range(1, n):
count[col] += count[col - 1]
return count[n - 1]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count[-1] でも良いと思います。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants