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<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
template<typename T>
void read1(T &m)
{
    T x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    m = x*f;
}
template<typename T>
void read2(T &a,T &b){read1(a);read1(b);}
typedef long long ll;
int T,kase = 1,i,j,k,n,m;
ll mult(ll x,ll y,ll mod) // ·ÀÖ¹x*y±¬long long;
{
    ll ans = 0;x %= mod;
    while(y){
        if(y&1) ans += x, y--;
        if(ans >= mod) ans -= mod;
        y >>= 1;
        x <<= 1;
        if(x >= mod) x -= mod;
    }
    return ans;
}
ll pow(ll a,ll n,ll mod)
{
    a %= mod;
    ll ans = 1;
    while(n){
        if(n&1) ans = ans*a%mod;
        a = a*a%mod;
        n >>= 1;
    }
    return ans;
}
int p[16]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
bool Miller_Rabin(ll n)
{
    if(n <= 2) return n == 2;
    if(n%2 == 0) return false;
    ll t = n - 1;
    while(t%2 == 0) t >>= 1;
    for(int i = 0;i < 16;i++){
        if(p[i] >= n) return true;
        if(n % p[i] == 0) return false;
        ll tmp = t;
        ll x = pow(p[i],t,n); // p[i]^t % n;
        while(tmp < n){
            ll y = mult(x,x,n);
            if(y == 1 && x != 1 && x != n-1) return false;
            x = y;
            tmp <<= 1;
        }
        if(x != 1) return false; // Fermat theory
    }
    return true;
}
int main()
{
    ll x,y;
    while(read2(x,y), x + y){
        if(Miller_Rabin(x) || pow(y,x,x) != y) puts("no");
        else puts("yes");
    }
    return 0;
}
时间: 2024-11-11 22:04:22

poj 3641 Pseudoprime numbers Miller_Rabin测素裸题的相关文章

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

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

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)

POJ 2234 Matches Game(Nim博弈裸题)

Description Here is a simple game. In this game, there are several piles of matches and two players. The two player play in turn. In each turn, one can choose a pile and take away arbitrary number of matches from the pile (Of course the number of mat

POJ 3641 素数打表+快速幂 简单题

给出2个数,p和a,2<p<=1e9,1<a<p 若p满足下面2个条件,输出yes,否则输出no 1.p不是素数 2.有a^p=a(mod p) 先判断第一个条件: 本来想用一个数组is_prime[i]表示i是不是素数的,明显,这里p太大,数组开不下 若p不是素数的话, 则p必有p=b*c,其中b<=c, 则(sqrt(p))^2=b*c,则b<=sqrt(p)<=10^4.5<10^5 所以若在10^5内存在数b满足b<p&&p%b

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

POJ 3624 Charm Bracelet(01背包裸题)

Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38909   Accepted: 16862 Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible fro