当前位置:网站首页>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)
总结
选不选板子
边栏推荐
- 数值 - number(Lua)
- 本周小贴士#136:无序容器
- L1-019 谁先倒(Lua)
- 百度地图自定义样式向右拖拽导致全球地图经度0度无法正常显示
- LeetCode 497(C#)
- Share the latest high-frequency Android interview questions, and take you to explore the Android event distribution mechanism
- Notification is the notification displayed in the status bar of the phone
- 赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性
- 【OKR目标管理】价值分析
- imageswitcher的功能和用法
猜你喜欢
随机推荐
做软件测试 掌握哪些技术才能算作 “ 测试高手 ”?
Ansible 学习总结(9)—— Ansible 循环、条件判断、触发器、处理失败等任务控制使用总结
Functions and usage of imageswitch
专精特新软件开发类企业实力指数发布,麒麟信安荣誉登榜
本周小贴士#136:无序容器
跟奥巴马一起画方块(Lua)
【可信计算】第十一次课:TPM密码资源管理(三) NV索引与PCR
使用popupwindow創建对话框风格的窗口
【解惑】App处于前台,Activity就不会被回收了?
网络攻防复习篇
Examen des lois et règlements sur la sécurité de l'information
Devops' operational and commercial benefits Guide
Alertdialog create dialog
本周小贴士131:特殊成员函数和`= default`
鲲鹏开发者峰会2022 | 麒麟信安携手鲲鹏共筑计算产业新生态
原生js验证码
Face recognition attendance system based on Baidu flying plasma platform (easydl)
alertDialog創建对话框
TabHOST 选项卡的功能和用法
基于RGB图像阈值分割并利用滑动调节阈值








