当前位置:网站首页>Cf:c. factors and powers of two [DP + sort + Select Board + select several numbers equal to the minimum number of known sums]
Cf:c. factors and powers of two [DP + sort + Select Board + select several numbers equal to the minimum number of known sums]
2022-07-07 17:49:00 【White speed Dragon King's review】
analysis
First construct the array , use set The deposit guarantee is unique
Then it becomes inverted list
then dfs,dfs Record current and , The remaining options , Number of selected
If the current sum is n success
prune : The remaining options are 0, The current sum is greater than n, The present and + The remaining optional sum is less than n
Then it returns the minimum value of the current first one with or without selection 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)
summary
Choose board or not
边栏推荐
猜你喜欢
2021-06-28
Function and usage of numberpick
第3章业务功能开发(用户访问项目)
【TPM2.0原理及应用指南】 5、7、8章
本周小贴士#136:无序容器
Ansible learning summary (9) -- ansible loop, condition judgment, trigger, processing failure and other task control use summary
Management by objectives [14 of management]
mui侧边导航锚点定位js特效
Deep learning - make your own dataset
DatePickerDialog and trimepickerdialog
随机推荐
Dragging the custom style of Baidu map to the right makes the global map longitude 0 unable to be displayed normally
深度学习机器学习各种数据集汇总地址
使用Stace排除故障的5种简单方法
VSCode关于C语言的3个配置文件
2021-06-28
js拉下帷幕js特效显示层
MySQL index hit level analysis
L1-019 谁先倒(Lua)
YARN Capacity Scheduler容量调度器(超详细解读)
测试3个月,成功入职 “字节”,我的面试心得总结
2021-06-28
LeetCode 890(C#)
Robot engineering lifelong learning and work plan-2022-
How to implement safety practice in software development stage
什么是敏捷测试
仿今日头条APP顶部点击可居中导航
线上比赛相关规则补充说明
自动化测试:Robot FrameWork框架大家都想知道的实用技巧
Please insert the disk into "U disk (H)" & unable to access the disk structure is damaged and cannot be read
企业经营12法的领悟