YTUOJ-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
1105 2
1105 3
0 0

Sample Output

no
no
yes
no
yes
yes

HINT

题意:

给定p,a两个数字2<=p<=1000000000,1<a<p;若p为素数,则输出no,否则(若a^p%p=a 输出yes,否则输出no)

代码如下:

#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(long long n)
{
    long long i;
    for (i=2;i<=sqrt(n);i++)
    {
        if (n%i==0)
            return false;
    }
    return true;
}

long long mod(long long a,long long n,long long m)
{
    long long d=1,t=a;
    while (n>0)                        //通过n来求a^p%p;
    {
        if (n%2==1)                    //当n为奇数时先乘一个a并%m;
            d=(d*t)%m;
        n/=2;
        t=(t*t)%m;                     //缩短计算过程
    }
    return d;
}

int main()
{
    long long a,p;
    while (cin>>p>>a)
    {
        if (a==0&&p==0)
            break;
        if (isPrime(p))
            cout<<"no"<<endl;
        else
        {
            if (a==mod(a,p,p))
                cout<<"yes"<<endl;
            else
                cout<<"no"<<endl;
        }
    }
    return 0;
}

运行结果:

这道题是去年新秀赛的一道英语题,本来昨天晚上就应该完成的,但昨天晚上下雨,为了从机房尽早赶回宿舍测试没通过就没再去考虑它了。今天念了它一天。和同学一交流才发现,英语果然还是硬伤,用上了百度翻译结果还是弄错了题意,p是素数的话应该是输出no,我却认为是输出yes。。。心好累。。。

时间: 2024-08-22 18:48:02

YTUOJ-Pseudoprime numbers的相关文章

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 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 poj3641

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

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

POJ 3641 Pseudoprime numbers 米勒罗宾算法

链接:http://poj.org/problem?id=3641 题意:由费马小定理可得,对于素数p,a^p = a (mod p),但是对于某些非素数p,也有比较小的可能满足a^p = a (mod p),如果满足,则称p是a条件下的伪素数,现给出p,a,问p是不是a条件的伪素数. 思路:首先用米勒 罗宾判断p是不是素数,如果不是,判断a^p = a (mod p)是否成立. 代码: #include <iostream> #include <cstdio> #include

poj Pseudoprime numbers 3641

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

GCD&amp;&amp;素筛&amp;&amp;快速幂 --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,

poj 3641 Pseudoprime numbers Miller_Rabin测素裸题

题目链接 题意:题目定义了Carmichael Numbers 即 a^p % p = a.并且p不是素数.之后输入p,a问p是否为Carmichael Numbers? 坑点:先是各种RE,因为poj不能用srand()...之后各种WA..因为里面(a,p) ?= 1不一定互素,即这时Fermat定理的性质并不能直接用欧拉定理来判定..即 a^(p-1)%p = 1判断是错误的..作的 #include<iostream> #include<cstdio> #include&l

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) //如果是

POJ 3641 Pseudoprime numbers

p是素数直接输出no,然后判断a^p%p和a是否相等. #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<map> #include<algorithm> using namespace std; long long mod_exp(long long a, long long b, long long c) { long long