#include<iostream>
#include<cstring>
#include<algorithm>
#include<random>
using namespace std;
typedef long long LL;
LL qmi(LL x, LL p,LL k) {
LL ans = 1;
while (k) {
if(k&1)
ans = ans * x % p;
x = (x * x) % p;
k >>= 1;
}
return ans;
}
bool isPrime(LL p) {
default_random_engine e;
uniform_int_distribution<LL> a(2ll, p-1);
auto t = p;
while (t) {
auto tmp = (qmi(a(e), p, (p - 1) / 2));
if (tmp!=1&&tmp!=(p-1))return false;
t >>= 1;
}
return true;
}
int main() {
for (LL i = 10;; i++) {
cout << i << ": " << isPrime(i) << endl;
}
}
蒙特卡罗-费马小定理-求素数
最新推荐文章于 2021-09-03 17:14:32 发布