当前位置:网站首页>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
边栏推荐
- 使用 xml资源文件定义菜单
- 本周小贴士131:特殊成员函数和`= default`
- Examen des lois et règlements sur la sécurité de l'information
- Pytorch中自制数据集进行Dataset重写
- Supplementary instructions to relevant rules of online competition
- USB通信协议深入理解
- 【解惑】App处于前台,Activity就不会被回收了?
- 网络攻防复习篇
- notification是显示在手机状态栏的通知
- DatePickerDialog and trimepickerdialog
猜你喜欢
随机推荐
三仙归洞js小游戏源码
【4500字归纳总结】一名软件测试工程师需要掌握的技能大全
Numberpick的功能和用法
【可信计算】第十次课:TPM密码资源管理(二)
Mysql 索引命中级别分析
99%的人都不知道|私有化部署还永久免费的即时通讯软件!
数字化转型的主要工作
自动化测试:Robot FrameWork框架大家都想知道的实用技巧
Notification is the notification displayed in the status bar of the phone
【TPM2.0原理及应用指南】 5、7、8章
Mrs offline data analysis: process OBS data through Flink job
actionBar 导航栏学习
面试官:页面很卡的原因分析及解决方案?【测试面试题分享】
What is agile testing
Audio Device Strategy 音频设备输出、输入 选择 基于7.0 代码
本周小贴士131:特殊成员函数和`= default`
本周小贴士#136:无序容器
LeetCode 515(C#)
责任链模式 - Unity
Machine vision (1) - Overview
![[tpm2.0 principle and Application guide] Chapter 1-3](/img/28/7f6e848d5c12d175214d6cc5de7c8b.png)








