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 and divide by p, the remainder
is a. Some (but not very many) non-prime values of p, known as base-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

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

typedef long long LL;

bool isPrime(int n) {
    if(n < 2) return false;
    int t = sqrt((double)n);
    for(int i = 2; i <= t; ++i)
        if(n % i == 0) return false;
    return true;
}

LL mod_power(LL x, LL n, LL mod) {
    LL ret = 1;
    for( ; n > 0; n >>= 1) {
        if(n & 1) ret = ret * x % mod;
        x = x * x % mod;
    }
    return ret;
}

int main() {
    LL p, a;
    while(scanf("%lld%lld", &p, &a), p | a) {
        if(isPrime(p)) printf("no\n");
        else printf(mod_power(a, p, p) == a ? "yes\n" : "no\n");
    }
    return 0;
}
时间: 2024-08-03 00:11:35

POJ3641 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 (快速幂)

题意:给出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)

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

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

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

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

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

北大ACM3641——Pseudoprime numbers~~快速求幂法

这一题,主要是快速求幂的方法的应用. 可以看看快速求幂方法的原理:http://blog.csdn.net/qq_25425023/article/details/44316463 题目的大概意思是: 输入两个数p,a,p为素数,则直接输出no,否则判断a^p % p == a?等于就yes,不等于就no. 理解了题目的意思,就很容易了. 下面的是AC代码: #include <iostream> #include <cstdio> using namespace std; int