编程介的小学生 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");
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制