nefu 120 梅森素數 Lucas-Lehmer判定法

來源:互聯網
上載者:User

定義:如果m是一個正整數,且2^m-1是一個素數,則m必是素數.反之,如果m是一個正整數,素數且Mm=2^m-1成為第m個梅森數;如果p是一個素數,並且Mp=2^p-1也是素數,那麼Mp=2^p-1也是素數,那麼Mp就稱為梅森素數.

Lucas-Lehmer判定方法.

設p是素數,第p個梅森數為Mp=2^p-1,r1=4;

對於k>=2,利用rk=((r(k-1))^2-1)%Mp,0<=rk<Mp

可以得到rk的序列,則有Mp是素數,若且唯若rp-1%Mp==0;

#include<iostream>#define ll long longusing namespace std;ll mul( ll a,ll b,ll mod ){   ll ret=0;   while( b>0 )   {    if( b&1 )       ret=(ret+a)%mod;    b>>=1;    a=(a<<1)%mod;    }    return ret;}int main(){ int T; scanf( "%d",&T ); ll r[64],mod,temp; int N; r[1]=4; while( T-- ) {    scanf( "%d",&N );    mod=1;    mod=mod<<N;    mod-=1;    //printf("%lld\n",mod);    for( int i=2;i<=N-1;i++ )    {   temp=mul(r[i-1],r[i-1],mod);   r[i]=(temp-2)%mod;   }    if( N==2||r[N-1]==0 )       printf( "yes\n" );    else       printf( "no\n" );  } return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.