标签:ble pac bin span cst clu ios for else
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 typedef long long ll; 5 // 18位素数:154590409516822759 6 // 19位素数:2305843009213693951 (梅森素数) 7 // 19位素数:4384957924686954497 8 ll prime[6] = {2, 3, 5, 233, 331}; 9 ll qmul(ll x, ll y, ll mod) { // 乘法防止溢出, 如果p * p不爆LL的话可以直接乘; O(1)乘法或者转化成二进制加法 10 11 12 return (x * y - (long long)(x / (long double)mod * y + 1e-3) *mod + mod) % mod; 13 /* 14 ll ret = 0; 15 while(y) { 16 if(y & 1) 17 ret = (ret + x) % mod; 18 x = x * 2 % mod; 19 y >>= 1; 20 } 21 return ret; 22 */ 23 } 24 ll qpow(ll a, ll n, ll mod) { 25 ll ret = 1; 26 while(n) { 27 if(n & 1) ret = qmul(ret, a, mod); 28 a = qmul(a, a, mod); 29 n >>= 1; 30 } 31 return ret; 32 } 33 bool Miller_Rabin(ll p) { 34 if(p < 2) return 0; 35 if(p != 2 && p % 2 == 0) return 0; 36 ll s = p - 1; 37 while(! (s & 1)) s >>= 1; 38 for(int i = 0; i < 5; ++i) { 39 if(p == prime[i]) return 1; 40 ll t = s, m = qpow(prime[i], s, p); 41 while(t != p - 1 && m != 1 && m != p - 1) { 42 m = qmul(m, m, p); 43 t <<= 1; 44 } 45 if(m != p - 1 && !(t & 1)) return 0; 46 } 47 return 1; 48 } 49 int main() 50 { 51 ll n; 52 while(cin>>n) 53 { 54 if(Miller_Rabin(n)) 55 cout<<true<<endl; 56 else cout<<false<<endl; 57 } 58 return 0; 59 }
标签:ble pac bin span cst clu ios for else
原文地址:https://www.cnblogs.com/meanttobe/p/11354747.html