b_bd_序列合并(k路归并思想)
思路:系数a≥0,所以f是一个单调递增函数;小根堆先存储k个序列的最小f值;另,k可能小于n,所以需要不断自增每个序列中的n来寻找更大一点的数(n=1,2,3,4...)
import heapq
"""
把n抽取出来,原式等价于
((((((a7*n+a6)*n+a5)*n+a4*n)+a3)*n+a2)*n+a1)*n+a0
"""
def f(a,n):
ans=a[0]
for i in range(1,8):
ans=ans*n+a[i]
return ans
def solve():
k,A=int(input()),[]
for i in range(k):
A.append(list(map(int, input().split(" "))))
n=int(input())
t,nx=[],[1]*k
heapq.heapify(t)
for i in range(k):
v=f(A[i], nx[i])
heapq.heappush(t,(v,i))
nx[i]+=1
ans=0
while n:
v,i=heapq.heappop(t)
heapq.heappush(t,(f(A[i], nx[i]),i))
n,nx[i],ans=n-1,nx[i]+1,v
return ans
print(solve())