Pseudoprime numbers POJ - 3641

快速幂入门。。。

wa一次是因为没认识到p不能为质数

/* 
 author:hdsdogge 
 begin: 
 end: 
 cost: 
 */
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<stack>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<vector>
#include<bitset>
#include<cstdlib>
#include<list>
#include <sstream>
#include<ctype.h>
using namespace std;
const int maxn=100+10;
typedef pair<int,int> P;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
int T,n,m;
ll a,p;
bool is_pirme(int n){
    for(int i=2;i*i<=n;i++){
        if(n%i==0)
            return false;
    }
    return true;
}
int main() {
    //freopen("test", "r", stdin);
    //freopen("out", "w", stdout);
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    while (cin >> p >> a && a + p) {
        ll ans = 1;
        ll x = a;
        ll p1 = p;
        while (p1 > 0) {
            if (p1 & 1)
                ans = (ans * x) % p;
            x = (x * x) % p;
            p1 >>= 1;
        }
        if (ans == a && !is_pirme(p)) {
            cout << "yes" << endl;
        }
        else
            cout << "no" << endl;

    }
    return 0;

}

原文地址:https://www.cnblogs.com/hdsdogge/p/9349743.html

时间: 2024-11-13 10:47:54

Pseudoprime numbers POJ - 3641的相关文章

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 &

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

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

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

POJ 3641 Pseudoprime numbers(快速幂)

嗯... 题目链接:http://poj.org/problem?id=3641 AC代码: 1 #include<cstdio> 2 #include<iostream> 3 4 using namespace std; 5 6 inline bool is_prime(int x){ 7 if(x == 2) return 1; 8 if(x % 2 == 0) return 0; 9 for(int i = 3; i * i <= x; i += 2){ 10 if(!