其他分享
首页 > 其他分享> > nefu120梅森素数

nefu120梅森素数

作者:互联网

#include<iostream>
#include<cstdio>
 
using namespace std;
typedef long long ll;
const int maxn = 63;
ll multi(ll a,ll b,ll mod_val)//实现a * b % mod_val的操作(大数乘法换成加法,否则直接乘会爆)
{
    a = a % mod_val;
    b = b % mod_val;
    ll ans = 0;
    while(b)
    {
        if(b & 1)
        {
            ans = (ans + a)  % mod_val;
        }
        b >>= 1;
        a = (a <<= 1) % mod_val;
    }
    return ans;
}
bool is_meiprime(ll n,int p)
{
    ll t = 4;
    for(int i = 2; i <= p - 1; i ++)//求出R[p - 1]
    {
        t = (multi(t,t,n)  - 2  ) % n ;
    }
    t %= n;
    if(t == 0)//素数:r_{p-1}\equiv 0(mod M_p)
    return true;
    return false;
}
int main()
{
    int Tcase;
    scanf("%d",&Tcase);
    for(int ii = 1; ii <= Tcase; ii ++)
    {
        int p;
        scanf("%d",&p);
        if(p == 2)//当为2的时候需要特判
        {
            cout << "yes" << endl;
            continue;
        }
        ll sum = 1;
        sum <<= p;
        sum --;//sum 为求出的梅森数
        if(is_meiprime(sum,p))
            cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

 

标签:梅森,val,nefu120,ll,long,素数,ans,include,mod
来源: https://www.cnblogs.com/Astronaut0142/p/15082919.html