Pseudoprime numbers

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 
11052 
1105 3 
0 0

Sample Output

no 
no 
yes
no 
yes 
yes


 
  
  1. #include<stdio.h>
  2. #include<math.h>
  3. int main()
  4. {
  5.     long long a,p;
  6.     int isprime(long long n);
  7.     long long modular(long long a,long long r,long long m);
  8.     while(scanf("%lld%lld",&p,&a)!=EOF)
  9.     {
  10.         if(p==0&&a==0)
  11.             break;
  12.         long long result;
  13.         if(isprime(p))
  14.             printf("no\n");
  15.         else
  16.         {
  17.             if(a==modular(a,p,p))
  18.   printf("yes\n");
  19.             else
  20.                 printf("no\n");
  21.         }
  22.     }
  23.     return 0;
  24. }
  25. int isprime(long long n)
  26. {
  27.     if(n==2)
  28.         return 1;
  29.     if(n<=1||n%2==0)
  30.         return 0;
  31.     long long j=3;
  32.     while(j<=(long long)sqrt(double(n)))
  33.     {
  34.         if(n%j==0)
  35.             return 0;
  36.         j+=2;
  37.     }
  38.     return 1;
  39. }
  40. long long modular(long long a,long long r,long long m)
  41. {
  42.     long long d=1,t=a;
  43.     while(r>0)
  44.     {
  45.         if(r%2==1)
  46.             d=(d*t)%m;
  47.         r/=2;
  48.         t=t*t%m;
  49.     }
  50.     return d;
  51.  















































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值