목차

Kangaroo Race

ps
링크acmicpc.net/…
출처BOJ
문제 번호32594
문제명Kangaroo Race
레벨골드 3
분류

수학

시간복잡도O(T*logn)
인풋사이즈T<=10^5, n<=10^18
사용한 언어Python 3.11
제출기록31120KB / 1264ms
최고기록1264ms
해결날짜2024/11/01

풀이

코드

"""Solution code for "BOJ 32594. Kangaroo Race".

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

Tags: [math]
"""

import sys


def main():
    k = int(sys.stdin.readline())
    for _ in range(k):
        n, x = [int(x) for x in sys.stdin.readline().split()]

        if x == 1:
            print('0')
            continue

        answer = next(
            (i for i in range(1, n.bit_length() + 1) if (x := x * x % n) == 1),
            None,
        )
        print('impossible' if answer is None else answer)


if __name__ == '__main__':
    main()