목차

Four Squares

ps
링크acmicpc.net/…
출처BOJ
문제 번호17626
문제명Four Squares
레벨실버 3
분류

정수론

시간복잡도O(sqrt(n))
인풋사이즈n<=50000
사용한 언어Python
제출기록31312KB / 84ms
최고기록36ms
해결날짜2021/08/14

풀이

코드

"""Solution code for "BOJ 17626. Four Squares".

- Problem link: https://www.acmicpc.net/problem/17626
- Solution link: http://www.teferi.net/ps/problems/boj/17626
"""

import math


def main():
    n = int(input())
    squares = {i * i for i in range(1, math.isqrt(n) + 1)}
    if n in squares:
        print(1)
    elif any(n - x in squares for x in squares):
        print(2)
    else:
        while n % 4 == 0:
            n //= 4
        print(4 if n % 8 == 7 else 3)


if __name__ == '__main__':
    main()