当前位置:网站首页>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)
总结
选不选板子
边栏推荐
猜你喜欢
随机推荐
【TPM2.0原理及应用指南】 1-3章
数值 - number(Lua)
【网络攻防原理与技术】第6章:特洛伊木马
深度学习机器学习各种数据集汇总地址
Vscode three configuration files about C language
基于RGB图像阈值分割并利用滑动调节阈值
策略模式 - Unity
<代码随想录二刷>链表
百度地图自定义样式向右拖拽导致全球地图经度0度无法正常显示
LeetCode 535(C#)
阿富汗临时政府安全部队对极端组织“伊斯兰国”一处藏匿点展开军事行动
原生js验证码
【4500字归纳总结】一名软件测试工程师需要掌握的技能大全
第1章CRM核心业务介绍
手机版像素小鸟游js戏代码
运行yolo v5-5.0版本报错找不到SPPF错误,进行解决
Dragging the custom style of Baidu map to the right makes the global map longitude 0 unable to be displayed normally
深入浅出【机器学习之线性回归】
目标检测1——YOLO数据标注以及xml转为txt文件脚本实战
责任链模式 - Unity