List of Powers

면접 대비

시간 제한3.5초메모리 제한1024 MB

요약
소수 p, 밑 a, 구간 [l, r]이 주어질 때 a^k mod p 값 중 구간에 들어가는 수를 오름차순으로 출력한다.
난이도

보통10점 중 5점

유형
정수론, 수학, 해시맵
정답자
아직 제출이 없습니다

문제

Let pp be a prime number and aa an integer such that 0<a<p0 < a < p. Consider all integers from ll to rr inclusive which can be expressed as ak mod pa^{k} \bmod p for some non-negative integer kk. Given that the number of such integers is at most 100100, print these integers in ascending order.

입력

The only line contains four integers pp, aa, ll, and rr separated by spaces (0<a<p≤1090 < a < p \le 10^{9}, pp is prime, 0≤l≤r<p0 \le l \le r < p).

출력

Print all integers from ll to rr inclusive which can be expressed as ak mod pa^{k} \bmod p for some non-negative integer kk. The integers must be printed in ascending order. Separate consecutive integers by spaces. The input is guaranteed to be such that the correct answer contains at most 100100 numbers.

힌트

In the first example, we must find all integers from l=0l = 0 up to r=3r = 3 inclusive which can be expressed as 3k mod 53^{k} \bmod 5 for some integer k≥0k \ge 0. These are numbers 30 mod 5=13^{0} \bmod 5 = 1, 31 mod 5=33^{1} \bmod 5 = 3 and 33 mod 5=27 mod 5=23^{3} \bmod 5 = 27 \bmod 5 = 2. The number 00 can not be expressed this way because 3k3^{k} does not divide evenly by 55 for any integer k≥0k \ge 0. So, we must print the numbers 11, 22, and 33 in ascending order.

In the second example, we must find all integers from l=2l = 2 up to r=3r = 3 inclusive which can be expressed as 4k mod 54^{k} \bmod 5 for some integer k≥0k \ge 0. Let us write down the first few such numbers: 40 mod 5=14^{0} \bmod 5 = 1, 41 mod 5=44^{1} \bmod 5 = 4, 42 mod 5=16 mod 5=14^{2} \bmod 5 = 16 \bmod 5 = 1, 43 mod 5=64 mod 5=44^{3} \bmod 5 = 64 \bmod 5 = 4, 44 mod 5=256 mod 5=14^{4} \bmod 5 = 256 \bmod 5 = 1, …\ldots. It can be proved that this sequence contains only numbers 11 and 44. So, the result is an empty list.

예제2

  1. 예제 1

    입력
    5 3 0 3
    
    예상 출력
    1 2 3
    
  2. 예제 2

    입력
    5 4 2 3
    
    예상 출력