Under construction

Still being built. Things may change

List of Powers

Time limit3.5sMemory limit1024 MB

Summary
Given a prime p and a base a, list every integer in [l, r] that equals a^k mod p for some non-negative k, in ascending order.
Level

Medium6 of 10

Topics
Math, Number theory, Implementation
Solved
No attempts yet

Problem

Let pp be a prime number and aa an integer such that 0<a<p0 < a < p. Find all integers from ll to rr inclusive that can be written as ak mod pa^{k} \bmod p for some non-negative integer kk. The number of such integers is at most 100100.

Input

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

Output

Print all integers from ll to rr inclusive that can be written as ak mod pa^{k} \bmod p for some non-negative integer kk, in ascending order. Separate consecutive integers by spaces. The input is guaranteed to have at most 100100 such integers.

Hint

Since pp is prime and 0<a<p0 < a < p, aa is not a multiple of pp, so ak mod pa^{k} \bmod p is never 00. The values of ak mod pa^{k} \bmod p repeat in a cycle as kk grows.

Examples2

  1. Example 1

    Input
    5 3 0 3
    
    Expected output
    1 2 3
    
  2. Example 2

    Input
    5 4 2 3
    
    Expected output