当前位置:网站首页>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
边栏推荐
- Examen des lois et règlements sur la sécurité de l'information
- Target detection 1 -- actual operation of Yolo data annotation and script for converting XML to TXT file
- Devops' operational and commercial benefits Guide
- 【TPM2.0原理及应用指南】 16、17、18章
- Ansible learning summary (9) -- ansible loop, condition judgment, trigger, processing failure and other task control use summary
- 99%的人都不知道|私有化部署还永久免费的即时通讯软件!
- 机器视觉(1)——概述
- Management by objectives [14 of management]
- <代码随想录二刷>链表
- 2021-06-28
猜你喜欢
随机推荐
Pro2:修改div块的颜色
calendarview日历视图组件的功能和用法
USB通信协议深入理解
alertDialog創建对话框
YARN Capacity Scheduler容量调度器(超详细解读)
简单的loading动画
Simple loading animation
漫画 | 宇宙第一 IDE 到底是谁?
Examen des lois et règlements sur la sécurité de l'information
Function and usage of calendar view component
【网络攻防原理与技术】第5章:拒绝服务攻击
数字化转型的主要工作
zdog.js火箭转向动画js特效
Devops' operational and commercial benefits Guide
Based on pytorch, we use CNN to classify our own data sets
99%的人都不知道|私有化部署还永久免费的即时通讯软件!
Please insert the disk into "U disk (H)" & unable to access the disk structure is damaged and cannot be read
How to implement safety practice in software development stage
【可信计算】第十二次课:TPM授权与会话
基于百度飞浆平台(EasyDL)设计的人脸识别考勤系统
![[distributed theory] (II) distributed storage](/img/51/473a8f6a0d109277eab54d72156539.png)








