This action will force synchronization from 编程语言算法集/Python, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
"""The number of partitions of a number n into at least k parts equals the number ofpartitions into exactly k parts plus the number of partitions into at least k-1 parts.Subtracting 1 from each part of a partition of n into k parts gives a partition of n-kinto k parts. These two facts together are used for this algorithm.* https://en.wikipedia.org/wiki/Partition_(number_theory)* https://en.wikipedia.org/wiki/Partition_function_(number_theory)"""def partition(m: int) -> int:""">>> partition(5)7>>> partition(7)15>>> partition(100)190569292>>> partition(1_000)24061467864032622473692149727991>>> partition(-7)Traceback (most recent call last):...IndexError: list index out of range>>> partition(0)Traceback (most recent call last):...IndexError: list assignment index out of range>>> partition(7.8)Traceback (most recent call last):...TypeError: 'float' object cannot be interpreted as an integer"""memo: list[list[int]] = [[0 for _ in range(m)] for _ in range(m + 1)]for i in range(m + 1):memo[i][0] = 1for n in range(m + 1):for k in range(1, m):memo[n][k] += memo[n][k - 1]if n - k > 0:memo[n][k] += memo[n - k - 1][k]return memo[m][m - 1]if __name__ == "__main__":import sysif len(sys.argv) == 1:try:n = int(input("Enter a number: ").strip())print(partition(n))except ValueError:print("Please enter a number.")else:try:n = int(sys.argv[1])print(partition(n))except ValueError:print("Please pass a number.")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。