当前位置:网站首页>Miller_Rabin 米勒拉宾概率筛【模板】
Miller_Rabin 米勒拉宾概率筛【模板】
2022-07-31 13:28:00 【秦小咩】
Miller_Rabin筛法是一种正确率极高,复杂度优秀的概率筛法,将质数分为2和奇质数,对于奇质数n,将n-1其分解为 2^p + m ,然后,取若干[1,n-1]的随机数a,本模板取了20次,每次判断是否有
a^m 三 1(mod n) 或者 是否有 [0,p-1]的j,满足 a^(2jm) 三 n-1 (mod n)
满足二者之一表示本次判断成功,20次中有一次失败就表明不是素数,20次都成功,那么可以说是一个素数。
验证正确性如下,取2-1e6全部数字,进行正确性判断,正确个数1e6-1,全部正确
甚至可以将判断次数缩小至5,也能保证完全正确

# include<iostream>
# include<iomanip>
# include<math.h>
using namespace std;
typedef long long int ll;
ll random(ll n) //生成[0,n]范围内随机数
{
return rand()%(n-1);
}
ll quick_mul(ll a, ll b, ll mod)
{
ll ans=0;
while(b)
{
if(b&1)
ans=(ans+a)%mod;
a=(a+a)%mod;
b>>=1;
}
return ans;
}
ll quick_pow(ll base,ll pow, ll mod)
{
ll ans=1;
while(pow)
{
if(pow&1)
ans=ans*base%mod;
pow>>=1;
base=base*base%mod;
}
return ans;
}
bool witness(ll a, ll n)
{
ll temp=n-1;
int j=0;
while(temp%2==0)
{
temp/=2;
j++;
}
ll x=quick_pow(a,temp,n);
if(x==1||x==n-1)
return 1;
while(j--)
{
x=quick_mul(x,x,n);
if(x==n-1)
return 1;
}
return 0;
}
bool miller_rabin(ll n)
{
if(n==2)
return 1;
if(n<2||n%2==0)
return 0;
for(int i=1; i<=20; i++)
{
ll a=random(n)+1;
// cout<<a<<endl;
if(!witness(a,n))
return 0;
}
return 1;
}
int prime[1000000+10];
bool not_prime[1000000+10];
int tot;
void init()
{
for(int i=2; i<=1000000; i++)
{
if(!not_prime[i])
{
tot++;
prime[tot]=i;
}
for(int j=1; j<=tot&&prime[j]*i<=1000000; j++)
{
not_prime[i*prime[j]]=1;
if(i%prime[j]==0)
{
break;
}
}
}
}
int main ()
{
init();
ll n;
int cnt=0;
for(n=2;n<=1000000;n++)
{
if(miller_rabin(n)!=(!not_prime[n]))
{
cout<<"***";
}
else
cnt++;
}
cout<<cnt<<endl;
return 0;
}
边栏推荐
- Batch大小不一定是2的n次幂!ML资深学者最新结论
- 深入浅出边缘云 | 4. 生命周期管理
- 分布式锁有哪些,怎么实现(分布式锁的三种实现的对比)
- All-round visual monitoring of the Istio microservice governance grid (microservice architecture display, resource monitoring, traffic monitoring, link monitoring)
- Verilog——基于FPGA的贪吃蛇游戏(VGA显示)
- C# control StatusStrip use
- TensorRT安装及使用教程「建议收藏」
- 报错IDEA Terminated with exit code 1
- 「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
- Error: npm ERR code EPERM
猜你喜欢

ICML2022 | Fully Granular Self-Semantic Propagation for Self-Supervised Graph Representation Learning
![[CPU Design Practice] Simple Pipeline CPU Design](/img/83/e1dfedfe2b2cfe83a34f86e252caa7.jpg)
[CPU Design Practice] Simple Pipeline CPU Design

C#控件CheckBox的使用

MATLAB | 我也做了一套绘图配色可视化模板

Optimization of five data submission methods

C#控件 ToolStripProgressBar 用法

ICML2022 | 面向自监督图表示学习的全粒度自语义传播

SetoolKit使用指南

C# 中的Async 和 Await 的用法详解

Introduction to the PartImageNet Semantic Part Segmentation dataset
随机推荐
ICML2022 | 面向自监督图表示学习的全粒度自语义传播
JSP中如何借助response对象实现页面跳转呢?
Selenium IDE for Selenium Automation Testing
Solution for browser hijacking by hao360
Network layer key protocol - IP protocol
电脑重要文件很多,如何备份比较安全?
Optimization of five data submission methods
Controller层代码这么写,简洁又优雅!
Samba 远程命令执行漏洞(CVE-2017-7494)
EXCEL如何快速拆分合并单元格数据
FastAPI encapsulates a generic response
PyQt5 rapid development and actual combat 9.7 Automated testing of UI layer
LeetCode·每日一题·1161.最大层内元素和·层次遍历
ICML2022 | Fully Granular Self-Semantic Propagation for Self-Supervised Graph Representation Learning
Text similarity calculation (Chinese and English) detailed explanation of actual combat
3.爬虫之Scrapy框架1安装与使用
如何使用StarUML画类图[通俗易懂]
How to quickly split and merge cell data in Excel
基于高阶微分器的无模型滑模控制器及其在自动电压调节器中的应用
0X7FFFFFFF,0X80000000「建议收藏」