Python 在列表中找到n个整数,这些整数相乘后等于m
我需要打印出列表元素的n个索引,在乘以某个给定的整数后。可以保证组合存在于列表中。例如,对于以下输入(数组中的元素数、所需乘法数、所需子列表和给定数组中的元素数): 7604Python 在列表中找到n个整数,这些整数相乘后等于m,python,python-3.x,algorithm,Python,Python 3.x,Algorithm,我需要打印出列表元素的n个索引,在乘以某个给定的整数后。可以保证组合存在于列表中。例如,对于以下输入(数组中的元素数、所需乘法数、所需子列表和给定数组中的元素数): 7604 301130064 我应该收拾一下 1 2 4 5 因为1*1*10*6==60。如果有多个解决方案,我需要打印其中任何一个。 我的解决方案工作得很慢,但我如何使它工作得更快 from itertools import chain, combinations arr = list(map(int, input().spl
301130064 我应该收拾一下 1 2 4 5 因为1*1*10*6==60。如果有多个解决方案,我需要打印其中任何一个。
我的解决方案工作得很慢,但我如何使它工作得更快
from itertools import chain, combinations
arr = list(map(int, input().split()))
numbers = list(map(int, input().split()))
s = sorted(numbers)
def filtered_sublists(input_list, length):
return (
l for l in all_sublists(input_list)
if len(l) == length
)
def all_sublists(l):
return chain(*(combinations(l, i) for i in range(len(l) + 1)))
def multiply(arr):
result = 1
for x in arr:
result = result * x
return result
def get_indexes(data):
indexes = []
for i in range(len(data)):
if arr[1] == multiply(data[i]):
for el in data[i]:
if numbers.index(el) in indexes:
all_ind = [i for i, x in enumerate(numbers) if x == el]
for ind in all_ind:
if ind not in indexes:
indexes.append(ind)
break
else:
indexes.append(numbers.index(el))
break
return indexes
sublists = list(filtered_sublists(numbers, arr[2]))
print(*get_indexes(sublists))
关键是不要测试每个组合
def combo(l, n=4, target=60, current_indices=[], current_mul=1):
if current_mul > target and target > 0:
return
elif len(current_indices) == n and current_mul == target:
yield current_indices
return
for i, val in enumerate(l):
if (not current_indices) or (i > current_indices[-1] and val * current_mul <= target):
yield from combo(l, n, target, current_indices + [i], val * current_mul)
l = [30,1,1,3,10,6,4]
for indices in combo(l, n=4, target=60):
print(*indices)
更多测试用例:
l = [1,1,1,2,3,3,9]
for c, indices in combo(l, n=4, target=9):
print(*indices)
印刷品:
1 2 4 5
0 1 2 6
0 1 4 5
0 2 4 5
1 2 4 5
我们可以对
O(n*k*num\u factors)
解决方案使用记忆递归,其中num\u factors
取决于我们可以创建的目标产品的因子数量。代码中的重复应该相当清楚。(不处理零,但添加额外处理应该非常简单。)
Pythonesque JavaScript代码:
函数f(A,prod,k,i=0,map={}){
如果(i==A.length | k==0)
返回[]
如果(映射[[prod,k]])
返回映射[[prod,k]]
if(prod==A[i]&&k==1)
返回[i]
如果(产品%A[i]==0){
常数系数=f(A,生产/A[i],k-1,i+1,映射)
if(系数长度){
map[[prod,k]]=[i].concat(因子)
返回映射[[prod,k]]
}
}
返回f(A,prod,k,i+1,map)
}
变量A=[30,1,1,3,10,6,4]
log(JSON.stringify(f(A,60,4)))
log(JSON.stringify(f(A,60,3)))
console.log(JSON.stringify(f(A,60,1))您可以从目标产品开始,递归地除以剩余列表中的因子,直到使用指定的因子数并降到1为止。这样做的优点是可以快速消除不是目标乘积因子的数字下的整个递归分支 处理列表中的零值和零的目标乘积需要在开始和遍历因子时满足两个特殊条件 例如:
def findFactors(product, count, factors, offset=0):
if product == 0: return sorted((factors.index(0)+i)%len(factors) for i in range(count))
if not count: return [] if product == 1 else None
if not factors: return None
for i,factor in enumerate(factors,1):
if factor == 0 or product%factor != 0: continue
subProd = findFactors(product//factor,count-1,factors[i:],i+offset)
if subProd is not None: return [i+offset-1]+subProd
r = findFactors(60, 4, [30,1,1,3,10,6,4])
print(r) # [1, 2, 4, 5]
r = findFactors(60, 4, [30,1,1,0,3,10,6,4])
print(r) # [1, 2, 5, 6]
r = findFactors(0, 4, [30,1,1,3,10,6,0,4])
print(r) # [0, 1, 6, 7]
谢谢你的解决方案,它是有效的,但我仍然得到时间限制错误。我不知道输入,但时间限制是1s,你的解决方案需要1.091它过去在第13次测试时返回时间限制错误,现在我在第11次测试中得到“演示错误”,可能的原因是“1.输出格式不正确2.根本没有输出3.额外输出”@Steve请参阅我的编辑,我添加了
*
来打印输出。第11个测试是什么?输入是什么?问题是测试是隐藏的,我不知道它们,结果必须是一个数组,从1开始打印索引,所以我用print(*map(lambda x:x+1,index))
打印它们,并将break
放在循环的末尾,所以我只得到一个解决方案,它看起来像2 3 5 6@n,m,k-(1≤K≤N≤5000,0≤M≤10^9),数组元素-(0)≤艾岛≤10^9),不幸的是,无法共享测试wabpage,因为这是一种私人任务。我们将在明天尝试此功能,并返回feedback@Steve酷。我修改并简化了代码,使地图键中不包含I
。