蓝桥杯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✨


相关推荐
Warson_L3 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅3 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅3 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L3 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅4 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L4 小时前
python的类&继承
python
Warson_L4 小时前
类型标注/type annotation
python
ThreeS7 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵8 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏