当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Devops' operational and commercial benefits Guide
Self made dataset in pytoch for dataset rewriting
漫画 | 宇宙第一 IDE 到底是谁?
如何在软件研发阶段落地安全实践
MRS离线数据分析:通过Flink作业处理OBS数据
【TPM2.0原理及应用指南】 1-3章
大笨钟(Lua)
textSwitch文本切换器的功能和用法
本周小贴士#134:make_unique与私有构造函数
阿富汗临时政府安全部队对极端组织“伊斯兰国”一处藏匿点展开军事行动
Deep learning - make your own dataset
【TPM2.0原理及应用指南】 12、13、14章
Functions and usage of viewswitch
【OKR目标管理】案例分析
Yarn capacity scheduler (ultra detailed interpretation)
Dragging the custom style of Baidu map to the right makes the global map longitude 0 unable to be displayed normally
【重新理解通信模型】Reactor 模式在 Redis 和 Kafka 中的应用
Functions and usage of imageswitch
Ansible 学习总结(9)—— Ansible 循环、条件判断、触发器、处理失败等任务控制使用总结
LeetCode 497(C#)