We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Python
- Itertools
- Maximize It!
- Discussions
Maximize It!
Maximize It!
Sort by
recency
|
1112 Discussions
|
Please Login in order to post a comment
using sparse DP approach
I hit this problem where I get 766 but expected output is 763, even with some of the answers here. Anyone else had this problem?
6 767 indata = ['2 488512261 423332742', '2 625040505 443232774', '1 4553600', '4 92134264 617699202 124100179 337650738', '2 778493847 932097163', '5 489894997 496724555 693361712 935903331 518538304']
I think the important python learning here is that while the docs only give product() examples with two iterables, product() can take an arbitrary list of iterables to produce all combinations. E.g., modifying an example from the docs:
This then lets us create the things to sum over and test for max all at once.
It should be noted that product() isn't magical with respect to run time, this is still a polynomial algorithm in O(n^K).
from itertools import product if name=='main': K,M=input().split() K=int(K) M=int(M) input_list=[] for i in range(K): N=list(map(int,input().split())) input_list.append(N) for i in range(len(input_list)): for j in range (len(input_list[i])): if(j==0): input_list[i].remove(input_list[i][j]) result=list(product(*input_list)) added=[] for i in range(len(result)): sum=0 for j in range(K): sum=sum+(result[i][j]**2) function=sum%M added.append(function) print(max(added))