POJ3641_Pseudoprime numbers【快速幂】【伪素数】

Pseudoprime numbers

Time Limit: 1000MS
Memory Limit: 65536K

Total Submissions: 6544Accepted: 2648

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 ≤ 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, 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

Source

Waterloo Local Contest, 2007.9.23

题目大意:费马定理:a^p = a(mod p) (a为大于1的整数,p为素数),一些非素数p,同样也符合上边的

定理,这样的p被称作基于a的伪素数,给你p和a,判断p是否是基于a的伪素数

思路:很简单的快速幂取余+素性判断

如果p为素数,则直接输出no

如果p不为素数,则进行快速幂取余判断是否为伪素数,若是,输出yes,不是,输出no

#include<stdio.h>
#include<math.h>

__int64 QuickPow(__int64 a,__int64 p)
{
    __int64 r = 1,base = a;
    __int64 m = p;
    while(p!=0)
    {
        if(p&1)
            r = r * base % m;
        base = base * base % m;
        p >>= 1;
    }
    return r;
}

bool IsPrime(__int64 p)
{
    for(__int64 i = 2; i <= sqrt(p) + 1; i++)
    {
        if(p % i == 0)
            return false;
    }
    return true;
}
int main()
{
    __int64 a,p;
    while(~scanf("%I64d %I64d",&p,&a) && (p!=0 || a!=0))
    {
        if(IsPrime(p))
            printf("no\n");
        else
        {
            if(QuickPow(a,p) == a)
                printf("yes\n");
            else
                printf("no\n");
        }
    }
    return 0;
}
时间: 2024-12-11 14:13:07

POJ3641_Pseudoprime numbers【快速幂】【伪素数】的相关文章

POJ1995 Raising Modulo Numbers(快速幂)

POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时01分45秒 星期三 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #

POJ3641 Pseudoprime numbers(快速幂+素数判断)

POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Carmichael Number 的简化版 /* * Created: 2016年03月30日 22时32分15秒 星期三 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring&g

POJ 1995 Raising Modulo Numbers (快速幂模板)

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4938   Accepted: 2864 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

poj Raising Modulo Numbers 快速幂模板

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8606   Accepted: 5253 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

Raising Modulo Numbers ---- 快速幂

Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5087 Accepted: 2959 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and

hdu 2817 A sequence of numbers(快速幂)

Problem Description Xinlv wrote some sequences on the paper a long time ago, they might be arithmetic or geometric sequences. The numbers are not very clear now, and only the first three numbers of each sequence are recognizable. Xinlv wants to know

POJ 1995 Raising Modulo Numbers (快速幂)

题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL Ksm(LL a, LL b, LL p) { LL ans = 1; while(b) { if(b & 1) { ans = (ans * a) % p; } a = (a * a) % p; b >>= 1; } return ans; } int main() { LL p, a

POJ1995:Raising Modulo Numbers(快速幂取余)

题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; int n,mod,sum; int main() { int

POJ 3641 Pseudoprime numbers (快速幂)

题意:给出a和p,判断p是否为合数,且满足a^p是否与a模p同余,即a^p%p与a是否相等 算法:筛法打1万的素数表预判p.再将幂指数的二进制形式表示,从右到左移位,每次底数自乘. #include <cstdio> #include <cstring> typedef long long LL; int p[10010]; bool np[100010]; int cntp; void SievePrime(int n) { memset(np, true, sizeof(np)