当前位置:网站首页>cf:C. Factorials and Powers of Two【dp + 排序 + 选不选板子 + 选若干个数等于已知和的最少数】
cf:C. Factorials and Powers of Two【dp + 排序 + 选不选板子 + 选若干个数等于已知和的最少数】
2022-07-07 15:41:00 【白速龙王的回眸】
分析
先把数组构造出来,用set存保证唯一
然后变成倒排的list
然后dfs,dfs记录当前和,剩余可选,选的个数
如果当前和为n成功
剪枝:剩余可选为0,当前和大于n,当前和+剩余可选和小于n
然后返回的是选或不选当前第一个的最小值dfs
ac code
import sys
import functools
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
s = set()
cnt = 1
now = 1
while cnt <= n:
s.add(cnt)
now += 1
cnt *= now
cnt = 1
while cnt <= n:
s.add(cnt)
cnt *= 2
lst = tuple(sorted(list(s), reverse = True))
def dfs(tempSum, tempLst, cnt):
# success
if tempSum == n:
return cnt
# fail 1
if len(tempLst) == 0 or tempSum > n:
return 0x7f7f7f7f
# cut branch, fail 2
if tempSum + sum(tempLst) < n:
return 0x7f7f7f7f
return min(dfs(tempSum + tempLst[0], tempLst[1:], cnt + 1), dfs(tempSum, tempLst[1:], cnt))
ans = dfs(0, lst, 0)
if ans < 0x7f7f7f7f:
print(ans)
else:
print(-1)
总结
选不选板子
边栏推荐
猜你喜欢
Examen des lois et règlements sur la sécurité de l'information
Share the latest high-frequency Android interview questions, and take you to explore the Android event distribution mechanism
js拉下帷幕js特效显示层
【网络攻防原理与技术】第5章:拒绝服务攻击
漫画 | 宇宙第一 IDE 到底是谁?
使用popupwindow創建对话框风格的窗口
目标检测1——YOLO数据标注以及xml转为txt文件脚本实战
MRS离线数据分析:通过Flink作业处理OBS数据
状态模式 - Unity(有限状态机)
使用OneDNS完美解决办公网络优化问题
随机推荐
Show progress bar above window
第3章业务功能开发(实现记住账号密码)
第2章搭建CRM项目开发环境(搭建开发环境)
麒麟信安中标国网新一代调度项目!
Functions and usage of tabhost tab
2021-06-28
做软件测试 掌握哪些技术才能算作 “ 测试高手 ”?
Lex & yacc of Pisa proxy SQL parsing
99%的人都不知道|私有化部署还永久免费的即时通讯软件!
线上比赛相关规则补充说明
Please insert the disk into "U disk (H)" & unable to access the disk structure is damaged and cannot be read
L1-028 判断素数(Lua)
状态模式 - Unity(有限状态机)
测试3个月,成功入职 “字节”,我的面试心得总结
【网络攻防原理与技术】第5章:拒绝服务攻击
策略模式 - Unity
深度学习机器学习各种数据集汇总地址
麒麟信安云平台全新升级!
【4500字归纳总结】一名软件测试工程师需要掌握的技能大全
简单的loading动画