GCD&&素筛&&快速幂 --A - Pseudoprime numbers

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本题用到快速幂,素数判定、二者结合;题意:输入两个数p,a.先判断p是否为素数,如果是,输出no。否则,再判断a的p次方取余p是否为a,是则yes,反之则no。
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;
typedef long long ll;
int isprime(ll n)
{
    if(n<=3)  return n>1;
    int k;
    k=sqrt(n);
    if(n%6!= 1 && n%6!=5)
        return 0;
    for(int i=5;i<=k;i+=6)
    {
        if(n%i==0 || n%(i+2)==0)
            return 0;
    }
    return 1;
}
ll qpow(ll a, ll n,ll mod)//计算a^n % mod
{
    ll re = 1;
    while(n)
    {
        if(n & 1)//判断n的最后一位是否为1
            re = (re * a) % mod;
        n >>= 1;//舍去n的最后一位
        a = (a * a) % mod;//将a平方
    }
    return re;
}
int main()
{
    ll p,a;
    while(cin>>p>>a&&a&&p)
    {
        if(isprime(p))
        cout<<"no"<<endl;
        else
        {
            if(a==qpow(a,p,p))
            cout<<"yes"<<endl;
            else
            cout<<"no"<<endl;
        }

    }
    return 0;
} 

typedef

typedef long long ll;

快速幂模板

ll qpow(ll a, ll n,ll mod)//计算a^n % mod
{
    ll re = 1;
    while(n)
    {
        if(n & 1)//判断n的最后一位是否为1
            re = (re * a) % mod;
        n >>= 1;//舍去n的最后一位
        a = (a * a) % mod;//将a平方
    }
    return re;}

质数判定模板

int isprime(ll n)
{
    if(n<=3)  return n>1;
    int k;
    k=sqrt(n);
    if(n%6!= 1 && n%6!=5)
        return 0;
    for(int i=5;i<=k;i+=6)
    {
        if(n%i==0 || n%(i+2)==0)
            return 0;
    }
    return 1;
}

注意输入用cin,用scanf会wa

原文地址:https://www.cnblogs.com/zjydeoneday/p/11241459.html

时间: 2024-10-07 18:17:21

GCD&&素筛&&快速幂 --A - Pseudoprime numbers的相关文章

GCD + 素数+快速幂

1.欧几里得算法 求解最大公约数,时间复杂度在O(log max(a,b))以内,可以看出,辗转相除法是非常高效的 int gcd(int a,int b) { return (b==0)?a:gcd(b,a%b); } 2.扩展欧几里得算法 求解方程a*x+b*y=gcd(a,b),a.b.x.y均为整数,时间复杂度和辗转相除法是相同的,函数返回gcd(a,b). int gcd(int a,int b) { return (b==0)?a:gcd(b,a%b); } int extgcd(i

POJ3641:Pseudoprime numbers(判断是否为Carmichael numbers,挑战P122)

题意:判断P是否为素数,是即输出no,不是就计算a的p次方是否等于a,是就输出yes,否则输出no: key:快速幂,判断素数,两个函数即可: /*快速幂, Carmicharl numbers*/ #include <iostream> #include <stdio.h> #include <algorithm> using namespace std; typedef long long ll; ll a, p; bool ok_prime(ll t) //如果是

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

【快速幂】POJ3641 - Pseudoprime numbers

输入a和p.如果p不是素数,则若满足ap = a (mod p)输出yes,不满足或者p为素数输出no.最简单的快速幂,啥也不说了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 typedef long long ll; 7 ll p,a; 8 9 int whether(int p) 10 { 1

poj 3641 Pseudoprime numbers 【快速幂】

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6645   Accepted: 2697 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

Pseudoprime numbers(POJ-3641)(快速幂)

快速幂+素数判断 p必须不是素数. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> #include<map> using namespace std; typedef long long ll; ll a,p; ll mod_pow(ll x,ll n,ll

POJ3641 Pseudoprime numbers 【快速幂】

Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6644   Accepted: 2696 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

Pseudoprime numbers POJ 3641(快速幂)

原题 题目链接 题目分析 依题意要先检测p是否为素数,这个可以用埃筛筛出1-sqrt(1e9)的素数,然后判定一下p是否能被这些数整除,不能的话就是素数,否则则为合数.至于a的p次方直接套个快速幂就行了. 代码 1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <utility> 6 #include &

UVa 10006 Carmichael Numbers (快速幂 + 素性测试)

Carmichael Numbers Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people even think that cryptography is t