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)        //如果是素数,直接输出no
{
    if(t == 2)
        return 1;
    else{
        for(int i = 2; i * i < t; i++){
            if(t % i == 0)
                return 0;
        }
        return 1;
    }
}
 ll mod_pow(ll n, ll m)
 {
     ll res = 1;
     ll l;
     l = m;
     while(l > 0){
        if(l & 1){
            res = res * n % m;
        }
        n = n * n % m;
        l >>= 1;
     }
     return res;
 }

int main()
{
    while(scanf("%l64d%l64d", &p, &a) != EOF){
        if(a == 0 && p == 0)
            break;
        if(ok_prime(p))
            printf("no\n");
        else{
            if(mod_pow(a, p) == a)
                printf("yes\n");
            else
                printf("no\n");
        }
    }
    return 0;
}
时间: 2024-10-12 02:24:12

POJ3641:Pseudoprime numbers(判断是否为Carmichael numbers,挑战P122)的相关文章

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

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

UVA10006 - Carmichael Numbers(筛选构造素数表+快速幂)

UVA10006 - Carmichael Numbers(筛选构造素数表+快速幂) 题目链接 题目大意:如果有一个合数,然后它满足任意大于1小于n的整数a, 满足a^n%n = a;这样的合数叫做Carmichael Numbers.题目给你n,然你判断是不是Carmichael Numbers. 解题思路:首先用筛选法构造素数表,判断n是否是合数,然后在用快速幂求a^2-a^(n - 1)是否满足上述的式子.快速幂的时候最好用long long ,防止相乘溢出. 代码: #include <

Carmichael Numbers(Uva 10006)

  Carmichael Numbers An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography. Alvaro is one

UVA - 10006 - Carmichael Numbers (快速幂+素数判断)

题目传送:UVA - 10006 思路:就是快速幂暴力过去就行了,然后要注意点细节,就是快速幂的时候会爆int,然后就是先判断是否为素数,是素数就直接输出结果is normal,不然会超时 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #inclu

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

(hdu step 2.1.2)How many prime numbers(判断一个数是否是质数)

题目: How many prime numbers Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8513 Accepted Submission(s): 2716   Problem Description Give you a lot of positive integers, just to find out how many pr