1 Const intS = -;//the number of random algorithms, the larger the s, the smaller the probability of the wrong judgment2ll Pow_mod (ll A, ll B, ll MoD) {//A^b%mod3LL ans =1;4A = a%MoD;5 while(b) {6 if(B &1) {7Ans = (ans * a)%MoD;8 }9A = (A * a)%MoD;Tenb >>=1; One } A returnans; - } - BOOLCheck (ll A, ll N, ll X, ll t) { theLL ret =Pow_mod (A, x, n); -LL last =ret; - for(inti =1; I <= t; i++) { -RET = (ret * ret)%N; + if(ret = =1&& Last! =1&& Last! = N-1)return true; -Last =ret; + } A if(Ret! =1)return true; at Else return false; - } - BOOLMiller_rabin (Long LongN) { - if(N <2)return false; - if(n = =2)return true; - if(N &1) ==0)return false; inLL x = n-1; -LL T =0; to while((X &1) ==0 ) { +X >>=1; -t++; the } * for(inti =0; i < S; i++) { $LL a = rand ()% (N-1) +1;Panax Notoginseng if(Check (A, n, x, T)) - return false; the } + return true; A}
Miller_rabin algorithm (random algorithm to determine whether a number is a prime)