luogu#P4718. 【模板】Pollard-Rho
【模板】Pollard-Rho
Problem Description
The Miller Rabin algorithm is an efficient method for primality testing. Although it is a probabilistic primality test, when multiple bases are chosen, the accuracy is acceptable.
Pollard rho is a rather “mystical” method used to find a non-trivial factor of a composite number in expected time complexity . In fact, Introduction to Algorithms gives , where is the smallest factor of , and is coprime with . However, these are all expected results and may not match reality. In practice, the Pollard rho algorithm runs quite well.
Here you need to write a program: for each number, check whether it is prime. If it is prime, output Prime; otherwise, output its largest prime factor.
Input Format
The first line contains , the number of test cases (not greater than ).
The next lines each contain an integer , where is guaranteed.
Output Format
Output lines.
For each test case, output the result.
6
2
13
134
8897
1234567654321
1000000000000
Prime
Prime
67
41
4649
5
Hint
On 2018.8.14, two new groups of testdata were added, and the time limit was increased to 2 s. Thanks to @whzzt.
On 2022.12.22, new testdata were added. Thanks to @ftt2333 and Library Checker.
by @will7101.
Translated by ChatGPT 5