목차

Fibonacci Representation

ps
링크acmicpc.net/…
출처BOJ
문제 번호8229
문제명Fibonacci Representation
레벨플래티넘 4
분류

그리디

시간복잡도O(t*logn)
인풋사이즈t<=10, n<=4*10^17
사용한 언어Python
제출기록30840KB / 68ms
최고기록60ms
해결날짜2022/04/28

풀이

코드

코드 1 - O(t*log^2(n))

"""Solution code for "BOJ 8229. Fibonacci Representation".

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

Tags: [Greedy]
"""


def main():
    p = int(input())
    for _ in range(p):
        k = int(input())

        answer = 0
        while k:
            a, b = 1, 1
            while b <= k:
                a, b = b, a + b
            k = min(b - k, k - a)
            answer += 1

        print(answer)


if __name__ == '__main__':
    main()

코드 2 - O(t*logn)

"""Solution code for "BOJ 8229. Fibonacci Representation".

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

Tags: [Greedy]
"""


def main():
    p = int(input())
    for _ in range(p):
        k = int(input())

        answer = 0
        a, b = 1, 1
        while b <= k:
            a, b = b, a + b
        while k:
            while a > k:
                a, b = b - a, a
            k = min(b - k, k - a)
            answer += 1

        print(answer)


if __name__ == '__main__':
    main()