POJ3641 (Fast power) to determine whether a^p = A (mod p) is established

Source: Internet
Author: User

Description

Fermat ' s theorem states that for any prime number P and for any integer a > 1, ap = a (mod p). That's, if we raise a to the pth power and divide by P, the remainder is a. Some (but not very many) non-prime values of p, known as base-a pseudoprimes, the Some a. (and some, known as Carmichael Numbers, is base-a pseudoprimes for all a.)

Given 2 < P ≤1000000000 and 1 < a < p, determine whether or not p -is A base-a pseudoprime.

Input

Input contains several test cases followed by a line containing "0 0". Each test case consists of a line containing p and a.

Output

For each test case, the output "yes" if P is a base-a pseudoprime; Otherwise output "no".

Sample Input
3 210 3341 2341 31105 21105 30 0
Sample Output
Nonoyesnoyesyes

If P is a prime number, output no; If P is not a prime number, determine if a^p is equal to P.
1#include <cstdio>2#include <math.h>3 __int64 F (__int64 A,__int64 B)4 {5__int64 c=b,t=1;6      while(b)7     {8         if(b%2!=0)9         {Tent=t*a%C; One         } Aa=a*a%C; -B/=2; -     } the     returnt%C; - } - __int64 F2 (__int64 a) - { + __int64 i; -     if(A <=1|| A2==0)return 0; +      for(i=3; I<=sqrt (a); i++) A     { at         if(a% i = =0)return 0; -     } -     return 1; - } - intMain () - { in      - __int64 p,a; to      while(SCANF ("%i64d%i64d", &p,&a) && p &&a) +     { -         if(F2 (p) = =1) printf ("no\n"); the         Else *         { $             if(f (a,p) = = a) printf ("yes\n");Panax Notoginseng             Else  -printf"no\n"); the         } +          A     } the}

POJ3641 (Fast power) to determine whether a^p = A (mod p) is established

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.