YTUOJ-Pseudoprime numbers

2015-11-21 01:05:11 ¡¤ ×÷Õß: ¡¤ ä¯ÀÀ: 9

Description

Fermat'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 a. Some (but not very many) non-prime values of p, known as base-a pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-a pseudoprimes for all a.)

Given 2 < p ¡Ü 1,000,000,000 and 1 < a < p, determine whether or not p is a base-a pseudoprime.

Input contains several test cases followed by a line containing "0 0". Each test case consists of a line containing p and a. For each test case, output "yes" if p is a base-a pseudoprime; otherwise output "no".

Input

Output

Sample Input

3 2
10 3
341 2
341 3
1105 2
1105 3
0 0

Sample Output

no
no
yes
no
yes
yes

HINT

ÌâÒ⣺

¸ø¶¨p£¬aÁ½¸öÊý×Ö2<=p<=1000000000,1

´úÂëÈçÏ£º

#include 
  
   
#include 
   
     using namespace std; bool isPrime(long long n) { long long i; for (i=2;i<=sqrt(n);i++) { if (n%i==0) return false; } return true; } long long mod(long long a,long long n,long long m) { long long d=1,t=a; while (n>0) //ͨ¹ýnÀ´Çóa^p%p; { if (n%2==1) //µ±nÎªÆæÊýʱÏȳËÒ»¸öa²¢%m; d=(d*t)%m; n/=2; t=(t*t)%m; //Ëõ¶Ì¼ÆËã¹ý³Ì } return d; } int main() { long long a,p; while (cin>>p>>a) { if (a==0&&p==0) break; if (isPrime(p)) cout<<"no"<
    
     

ÔËÐнá¹û£º

\

?

?

?

?

ÕâµÀÌâÊÇÈ¥ÄêÐÂÐãÈüµÄÒ»µÀÓ¢ÓïÌ⣬±¾À´×òÌìÍíÉϾÍÓ¦¸ÃÍê³ÉµÄ£¬µ«×òÌìÍíÉÏÏÂÓ꣬ΪÁË´Ó»ú·¿¾¡Ôç¸Ï»ØËÞÉá²âÊÔûͨ¹ý¾ÍûÔÙÈ¥¿¼ÂÇËüÁË¡£½ñÌìÄîÁËËüÒ»Ìì¡£ºÍͬѧһ½»Á÷²Å·¢ÏÖ£¬Ó¢Óï¹ûÈ»»¹ÊÇÓ²ÉË£¬ÓÃÉÏÁ˰ٶȷ­Òë½á¹û»¹ÊÇŪ´íÁËÌâÒ⣬pÊÇËØÊýµÄ»°Ó¦¸ÃÊÇÊä³öno£¬ÎÒÈ´ÈÏΪÊÇÊä³öyes¡£¡£¡£ÐĺÃÀÛ¡£¡£¡£

?