python https://www.hackerrank.com/challenges/maximize-it/problem

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python https://www.hackerrank.com/challenges/maximize-it/problem相关的知识,希望对你有一定的参考价值。

""" https://www.hackerrank.com/challenges/maximize-it/problem """

import itertools

import sys

# TEST_INPUT = """3 1000
# 2 5 4
# 3 7 8 9
# 5 5 7 8 9 10"""

# TEST_INPUT = """7 952
# 6 386364143 56297585 479292050 782778989 177771725 945191156
# 7 458982242 957774948 25202756 357554307 248513713 506622954 769577156
# 3 109432676 494972174 914814315
# 1 49979276
# 2 491584479 103564062
# 1 25883738
# 1 460971693"""

TEST_INPUT = None


def read_from_stdin():
    t = ''

    while True:
        line = sys.stdin.readline()
        if not line:
            return t
        t += line


def parse_line(string):
    return [int(x) for x in string.split()]


def parse_input():
    input_ = TEST_INPUT if TEST_INPUT else read_from_stdin()

    N = []
    K = None
    M = None
    for lineno, line in enumerate(input_.split('\n')):
        if lineno == 0:
            K, M = parse_line(line)
        else:
            values_len, *values = parse_line(line)
            assert values_len == len(values)
            N.append(values)

    assert K == len(N)
    return K, M, N


def calc_result():
    K, M, N = parse_input()

    res = 0
    for comb in set(itertools.product(*N)):
        t = sum(x ** 2 for x in comb) % M
        res = max(res, t)

    return res


if __name__ == '__main__':
    print(calc_result())

以上是关于python https://www.hackerrank.com/challenges/maximize-it/problem的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识