鱼C论坛

 找回密码
 立即注册
查看: 2086|回复: 1

题目172:考察只有少量重复位的数字

[复制链接]
发表于 2016-9-15 01:10:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
Investigating numbers with few repeated digits

How many 18-digit numbers n (without leading zeros) are there such that no digit occurs more than three times in n?



题目:

有多少个任何数字都出现不超过三次的 18 位数字?

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-9-21 16:51:14 | 显示全部楼层
递归解法,用functools的lru_cache来加速
from functools import lru_cache
@lru_cache(maxsize=None)
def p172(digits_left=18, digit_count=tuple([0]*10)):

    # end case, no more digits left
    if digits_left == 0: return 1

    possible = 0

    # try ALL THE DIGITS!
    # start with min 1 at first digit to prevent leading zeros
    for n in range(1 if digits_left==18 else 0, 10): 

        if digit_count[n] == 3 : continue # skip used up digits

        # make modifiable copy (list) and mark digit as used +1
        tmp = list(digit_count)
        tmp[n] += 1

        possible += p172(digits_left-1, tuple(tmp))

    return possible

print(p172())
227485267000992000
[Finished in 7.9s]
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-2 22:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表