蓝桥杯3527阶乘的和 | 组合数学

写在前面的话

昨天的蓝桥杯每日一题 奇怪的数 用python写太难了,甚至题解的优化处理也只能得70分(后几个TLE了),但是C++写 不优化的6层for循环+后4位复用 可以直接过。(雾)

题目传送门


这个题目的思路是对Ai排序后,记录每个Ai出现的次数dicA~i~。显然最小的A0满足m。每次遍历dic,如果m对应的Ai出现的次数是Ai+1的倍数,那么将其并入dicA~i+1~,同时m++。当m无法满足次数的倍数关系时m值即为所求。


python 复制代码
from collections import defaultdict
n = int(input())
ai = list(map(int, input().split()))
ai.sort()
m = ai[0]
dic = defaultdict(int)	# 因为python的dict()不支持类似C++中map的运算操作,所以引入defaultdict

for it in ai:
    dic[it] += 1

while True:
    x = dic[m]
    if x %(m + 1) != 0:
        break
    dic[m+1] += x//(m+1)
    m += 1

print(m)

END✨


相关推荐
luckdewei2 小时前
FastAPI 资产管理系统实战:复杂 ORM 关联、Alembic 迁移与 N+1 查询优化
python
aqi008 小时前
15天学会AI应用开发(八)使用向量数据库实现RAG功能
人工智能·python·大模型·ai编程·ai应用
Csvn9 小时前
`functools.lru_cache` —— 一行代码搞定缓存加速
后端·python
金銀銅鐵1 天前
[Python] 从《千字文》中随机挑选汉字
后端·python
cup111 天前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi001 天前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵1 天前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf1 天前
Agent 流程编排
后端·python·agent