当前位置:网站首页>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)
总结
选不选板子
边栏推荐
- 【网络攻防原理与技术】第1章:绪论
- 数字化转型的主要工作
- With the latest Alibaba P7 technology system, mom doesn't have to worry about me looking for a job anymore
- Function and usage of numberpick
- 跟奥巴马一起画方块(Lua)
- [distributed theory] (I) distributed transactions
- 状态模式 - Unity(有限状态机)
- redis主从、哨兵主备切换搭建一步一步图解实现
- 【分布式理论】(一)分布式事务
- 数值 - number(Lua)
猜你喜欢
随机推荐
基于百度飞浆平台(EasyDL)设计的人脸识别考勤系统
calendarview日历视图组件的功能和用法
alertDialog創建对话框
Functions and usage of imageswitch
Functions and usage of tabhost tab
VSCode关于C语言的3个配置文件
MySQL index hit level analysis
第3章业务功能开发(用户登录)
Vscode three configuration files about C language
Show progress bar above window
简单的loading动画
本周小贴士131:特殊成员函数和`= default`
【网络攻防原理与技术】第1章:绪论
第2章搭建CRM项目开发环境(搭建开发环境)
[distributed theory] (I) distributed transactions
面试官:页面很卡的原因分析及解决方案?【测试面试题分享】
Toast will display a simple prompt message on the program interface
L1-025 正整数A+B(Lua)
DatePickerDialog和trimepickerDialog
Enum + Validation 的个人最佳实践 demo 分享