You are given a function f(x)=x^2. You are also given k lists. The ith list consists of ni elements.
You have to pick one element from each list so that the value from the equation below is maximized:
s-fx1+fx2................fxk %m
% denotes the element picked from the list . Find the maximized value obtained.
denotes the modulo operator.
Note that you need to take exactly one element from each list, not necessarily the largest element. You add the squares of the chosen elements and perform the modulo operation. The maximum value that you can obtain, will be the answer to the problem.
from itertools import product
k,m = map(int,input().split())
number = []
for _ in range(k):
r = map(int,input().split()[1:])
number.append(map(lambda n:n**2%m, r))
print(max(map(lambda n: sum(n)%m, product(*number))))
Comments
Leave a comment