Все задачи
Средне Решена: 1 Процент решений: 100.0%

Right-Triangle Product

Find the largest side product of an integer right triangle with a given perimeter.

Чему вы научитесь

  • Search integer right triangles by perimeter
  • Reduce three loops to two
  • Check the Pythagorean condition

Условие

Показать перевод

Find positive integers a < b < c with a² + b² = c² and a + b + c = perimeter. Return the largest product a × b × c among qualifying triangles, or −1 if there are none. This generalizes the fixed-perimeter source task and explicitly handles multiple answers.

Implement pythagorean_product(perimeter).

Input

perimeter: The required sum of the three sides.

Output

Return the largest product, or −1 when no triangle qualifies.

Ограничения

3 ≤ perimeter ≤ 100,000. Products fit signed 64-bit integers.

Примеры

Пример 1
Ввод:
perimeter = 30

Ожидаемый вывод:
expected = 780
Ввод:
long long perimeter = 30;

Ожидаемый вывод:
long long expected = 780;
Ввод:
long perimeter = 30;

Ожидаемый вывод:
long expected = 780;

Пояснение

The triangle is (5, 12, 13): 25 + 144 = 169 and 5 + 12 + 13 = 30. Its product is 780.

Пример 2
Ввод:
perimeter = 11

Ожидаемый вывод:
expected = -1
Ввод:
long long perimeter = 11;

Ожидаемый вывод:
long long expected = -1;
Ввод:
long perimeter = 11;

Ожидаемый вывод:
long expected = -1;

Пояснение

No increasing positive integer right-triangle sides sum to 11.

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

Only certain perimeters can form a right triangle, so first narrow the search using the side-sum constraint.

Подсказка 2

For each possible shortest side, derive the other two sides from the perimeter and check the right-triangle condition.

Подсказка 3

More than one triangle may fit; keep the largest product and return no-solution when nothing valid appears.

Ожидаемая сложность

O(perimeter^2) time, O(1) extra space

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Лучшее принятое решение каждого участника. Более быстрые, экономные и высоко оценённые решения стоят выше.

# Участник Язык Время Память Оценка ИИ Решение
1 ННикита Python 957 ms - 4/5 Посмотреть код

Учитесь у других

Решения сообщества

Внимание, спойлер: ниже 1 полное решение Попробуйте сначала решить сами. Вы всегда можете посмотреть код других и AI-обзоры. Войдите и решите задание, чтобы присоединиться к обсуждению.