编程介的小学生 2019-03-18 16:25 采纳率: 20.5%
浏览 414

素数的队列的计算问题的算法,采用C语言的编程计算实现它

roblem Description
Fermat's theorem states that for any prime number p and for any integer a > 1, a^p == 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
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, output "yes" if p is a base-a pseudoprime; otherwise output "no".

Sample Input
3 2
10 3
341 2
341 3
1105 2
1105 3
0 0

Sample Output
no
no
yes
no
yes
yes

  • 写回答

1条回答 默认 最新

  • wudengyu 2019-03-26 07:42
    关注
    #include <stdio.h>
    unsigned long long pow(unsigned long long x, long y,long mod) {
        unsigned long long p = 1;
        while (y) {
            if (y & 1)p = x * p%mod;
            x = x * x%mod;
            y >>= 1;
        }
        return p;
    }
    int prime(long long a) {
        int i;
        if (a == 2)
            return 1;
        for (i = 2; i*i <= a; i++)
            if (a%i == 0)
                return 0;
        return 1;
    }
    void main() {
        long p,a;
        while (1) {
            scanf("%ld %ld", &p, &a);
            if (a == 0 || p == 0)break;
            if (!prime(p)&&pow(a,p,p)==a)
                printf("yes\n");
            else
                printf("no\n");
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!