Pseudoprime
InterviewTime limit1sMemory limit128 MB
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 is prime, then for any integer the congruence holds. In other words, raising to the -th power and taking the remainder modulo gives back .
However, even when is not prime, the congruence above may still hold for some integer . In that case is called a pseudoprime to base . (A composite number that satisfies the congruence for every is called a Carmichael number.)
Given and , write a program that determines whether is a pseudoprime to base .
Input
The input consists of several test cases. Each test case is a single line containing two integers and separated by a space. The last line contains 0 0, which must not be processed.
,
Output
For each test case, print yes if is a pseudoprime to base , and no otherwise, one per line.