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;
}
原文地址:https://www.cnblogs.com/hxer/p/5369714.html