当前位置:网站首页>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
边栏推荐
- Run Yolo v5-5.0 and report an error. If the sppf error cannot be found, solve it
- Functions and usage of viewswitch
- Alertdialog create dialog
- Face recognition attendance system based on Baidu flying plasma platform (easydl)
- 本周小贴士#136:无序容器
- 【TPM2.0原理及应用指南】 16、17、18章
- cf:C. Factorials and Powers of Two【dp + 排序 + 选不选板子 + 选若干个数等于已知和的最少数】
- 面试官:页面很卡的原因分析及解决方案?【测试面试题分享】
- Ansible learning summary (9) -- ansible loop, condition judgment, trigger, processing failure and other task control use summary
- 命令模式 - Unity
猜你喜欢
随机推荐
基于PyTorch利用CNN对自己的数据集进行分类
Numberpick的功能和用法
[re understand the communication model] the application of reactor mode in redis and Kafka
Function and usage of calendar view component
Alertdialog create dialog
字符串 - string(Lua)
imageswitcher的功能和用法
Deep learning machine learning various data sets summary address
Create dialog style windows with popupwindow
Functions and usage of viewflipper
本周小贴士#141:注意隐式转换到bool
第1章CRM核心业务介绍
Devops' operational and commercial benefits Guide
Functions and usage of tabhost tab
Mrs offline data analysis: process OBS data through Flink job
Yarn capacity scheduler (ultra detailed interpretation)
【4500字归纳总结】一名软件测试工程师需要掌握的技能大全
利用七种方法对一个文件夹里面的所有图像进行图像增强实战
【网络攻防原理与技术】第5章:拒绝服务攻击
Pro2:修改div块的颜色