Pseudoprime

Interview

Time limit1sMemory limit128 MB

Summary
For each pair p and a, decide whether p is composite and satisfies a^p mod p == a, printing yes or no.
Level

Medium4 of 10

Topics
Math, Number theory, Binary search, Bit manipulation
Solved
No attempts yet

Problem

Fermat's little theorem states the following.

If pp is prime, then for any integer a>1a > 1 the congruence ap≡a(modp)a^p \equiv a \pmod{p} holds. In other words, raising aa to the pp-th power and taking the remainder modulo pp gives back aa.

However, even when pp is not prime, the congruence above may still hold for some integer aa. In that case pp is called a pseudoprime to base aa. (A composite number that satisfies the congruence for every aa is called a Carmichael number.)

Given pp and aa, write a program that determines whether pp is a pseudoprime to base aa.

Input

The input consists of several test cases. Each test case is a single line containing two integers pp and aa separated by a space. The last line contains 0 0, which must not be processed.

2<p≤1092 < p \le 10^9, 1<a<p1 < a < p

Output

For each test case, print yes if pp is a pseudoprime to base aa, and no otherwise, one per line.

Examples1

  1. Example 1

    Input
    3 2
    10 3
    341 2
    341 3
    1105 2
    1105 3
    0 0
    
    Expected output
    no
    no
    yes
    no
    yes
    yes