#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define INF 0x3f3f3f3f
typedef long long LL;
using namespace std;/* *************************************************
* Miller_Rabin 算法进行素数测试
* 速度快,可以判断一个 < 2^63 的数是不是素数
*
**************************************************/
const int S = ;
LL mult_mod(LL a,LL b,LL c)
{
a %= c;
b %= c;
LL ret = ;
LL tmp = a;
while(b)
{
if(b & )
{
ret += tmp;
if(ret > c)ret -= c;
}
tmp <<= ;
if(tmp > c)tmp -= c;
b >>= ;
}
return ret;
}
LL pow_mod(LL a,LL n,LL mod)
{
LL ret = ;
LL temp = a%mod;
while(n)
{
if(n & )ret = mult_mod(ret,temp,mod);
temp = mult_mod(temp,temp,mod);
n >>= ;
}
return ret;
}
bool check(LL a,LL n,LL x,LL t)
{
LL ret = pow_mod(a,x,n);
LL last = ret;
for(int i = ;i <= t;i++)
{
ret = mult_mod(ret,ret,n);
if(ret == && last != && last != n-)return true;
last = ret;
}
if(ret != )return true;
else return false;
}
bool MiLLer_Rabin(LL n)
{
if( n < )return false;
if( n == )return true;
if( (n&) == )return false;
LL x = n - ;
LL t = ;
while( (x&)== ){x >>= ; t++;}
srand(time(NULL));
for(int i = ;i < S;i++)
{
LL a = rand()%(n-) + ;
if( check(a,n,x,t) )
return false;
}
return true;
}
int main()
{
LL n, m;
while(cin >> n){
LL cnt=;
for(int i = ; i < n; i++){
cin >> m;
if(MiLLer_Rabin(m))
cnt++;
}
cout << cnt << endl;
}
}

Miller Rabin素数检测的更多相关文章

  1. Miller Rabin素数检测与Pollard Rho算法

    一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是 ...

  2. POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  3. POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】

    GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...

  4. POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

  5. HDU1164_Eddy&#39;s research I【Miller Rabin素数测试】【Pollar Rho整数分解】

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. 关于素数:求不超过n的素数,素数的判定(Miller Rabin 测试)

    关于素数的基本介绍请参考百度百科here和维基百科here的介绍 首先介绍几条关于素数的基本定理: 定理1:如果n不是素数,则n至少有一个( 1, sqrt(n) ]范围内的的因子 定理2:如果n不是 ...

  7. 与数论的厮守01:素数的测试——Miller Rabin

    看一个数是否为质数,我们通常会用那个O(√N)的算法来做,那个算法叫试除法.然而当这个数非常大的时候,这个高增长率的时间复杂度就不够这个数跑了. 为了解决这个问题,我们先来看看费马小定理:若n为素数, ...

  8. 【数论基础】素数判定和Miller Rabin算法

    判断正整数p是否是素数 方法一 朴素的判定   

  9. POJ1811- Prime Test(Miller–Rabin+Pollard's rho)

    题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...

随机推荐

  1. poshytip基本使用

    js基本调用方法 $("#tips").poshytip({ content: $this.text(), alignTo: 'target', alignX: direction ...

  2. 3. 深入研究 UCenter API 之 加密与解密(转载)

    1.  深入研究 UCenter API 之 开篇 (转载) 2.  深入研究 UCenter API 之 通讯原理(转载) 3.  深入研究 UCenter API 之 加密与解密(转载) 4.  ...

  3. Codeforces 666E E - Forensic Examination SA + 莫队 + 线段树

    E - Forensic Examination 我也不知道为什么这个复杂度能过, 而且跑得还挺快, 数据比较水? 在sa上二分出上下界, 然后莫队 + 线段树维护区间众数. #include< ...

  4. css3实现旋转表

    如图所示: css部分: <style> #clock{width:100px; height:100px; border-radius:50%; border:4px solid bla ...

  5. BZOJ3052/UOJ#58 [wc2013]糖果公园 莫队 带修莫队 树上莫队

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ3052.html 题目传送门 - BZOJ3052 题目传送门 - UOJ#58 题意 给定一棵树,有 ...

  6. Calendar抽象类的使用

    Calendar timeNow = Calendar.getInstance(); int year = timeNow.get(Calendar.YEAR); // 这里月是从0开始的,即0到11 ...

  7. Python交互图表可视化Bokeh:5 柱状图| 堆叠图| 直方图

    柱状图/堆叠图/直方图 ① 单系列柱状图② 多系列柱状图③ 堆叠图④ 直方图 1.单系列柱状图 import numpy as np import pandas as pd import matplo ...

  8. CentOS7 Windows双系统 修复引导

    伪前提:先装Windows再装CentOS7(伪前提是因为没试过先装CentOS再装Windows) Windows用U盘安装CentOS7后,开启启动项里面仅有CentOS7的启动项,要修复Wind ...

  9. css3 的新属性

    1,动画,animate------>//动画-名称-动画的时间间隔-以什么方式播放-循环 .right{ animate:dropdown 4px 5px #000; // x的偏移值 y的偏 ...

  10. HDU 1301-Jungle Roads【Kruscal】模板题

    题目链接>>> 题目大意: 给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费. 解题分析: 最小生成树模板题,下 ...