POJ3641: Pseudo Prime numbers

Source: Internet
Author: User

DescriptionFermat's theorem states that for any prime number p and for any integer a> 1, ap = a (mod p ). that is, if we raise a to the pth power and divide by p, the remainder is. some (but not very values) non-prime values of p, known as base-a pseudo doprimes, have this property for some. (And some, known as Carmichael Numbers, are base-a pseudo Primes for all .) given 2 <p ≤ 1000000000 and 1 <a <p, determine whether or not p is a base-a Pseudo Prime. inputInput contains several test cases followed by a line containing "0 0 ". each test case consists of a line containing p and. outputFor each test case, output "yes" if p is a base-a pseudo-prime; otherwise output "no ". sample Input3 210 3341 2341 31105 21105 30 0 Sample Outputnonoyesnoyesyes [cpp] [cpp] # include Using namespace std; int prime (long a) {int I; if (a = 2) return 1; for (I = 2; I * I <=; I ++) if (a % I = 0) return 0; return 1;} long mod (long a, long B, long m) {long ans = 1; while (B> 0) {if (B & 1) {ans = ans * a % m; // B --;} B >>= 1; a = a * a % m;} return ans;} int main () {long a, p; while (cin> p> a & (p | a) {long ans; if (prime (p) cout <"no" <endl; else {ans = mod (a, p, p); if (ans = a) cout <"yes" <endl; else cout <"no" <endl ;}} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.