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

题目分析:主要在于素数的判断和快速幂算法

 1 #include <iostream>
 2 using namespace std;
 3
 4 int prime(long long a)
 5 {
 6     int i;
 7     if(a == 2)
 8         return 1;
 9     for(i = 2; i*i<=a; i++)
10         if(a%i == 0)
11            return 0;
12     return 1;
13 }
14
15 long long mod(long long a,long long b,long long c)
16 {
17     long long ans = 1;
18     a=a%c;
19     while(b>0)
20     {
21         if(b%2==1) ans=(ans*a)%c;
22         b=b/2;
23         a=(a*a)%c;
24     }
25     return ans;
26 }
27 int main()
28 {
29     long long a,p;
30
31     while(cin >> p >> a)
32     {
33         if(p==0 &&    a==0)    break;
34         long long ans;
35         if(prime(p))
36         cout << "no" << endl;
37         else
38         {
39             ans = mod(a,p,p);
40             if(ans == a)
41             cout << "yes" << endl;
42             else
43             cout << "no" << endl;
44         }
45     }
46
47     return 0;
48 }  

时间: 2024-10-18 09:05:43

Pseudoprime numbers poj3641的相关文章

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(快速幂+素数判断)

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 【快速幂】

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 【快速幂】

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

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

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

输入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

POJ3641 Pseudoprime numbers (幂取模板子)

给你两个数字p,a.如果p是素数,并且ap mod p = a,输出“yes”,否则输出“no”. 很简单的板子题.核心算法是幂取模(算法详见<算法竞赛入门经典>315页). 幂取模板子: 1 int pow_mod(int a,int n,int m) 2 { 3 if(n==0) return 1; 4 int x = pow_mod(a, n / 2, m); 5 long long ans = (long long)x * x % m; 6 if(n%2) ans = ans * a