Description

  Give you a lot of positive integers, just to find out how many prime numbers there are.

Input

  There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be less than 2.

Output

  For each case, print the number of prime numbers you have found out.

Sample Input

3
2 3 4

Sample Output

2

【题目简述】输入一个n和n个int32整数,询问其中有多少个质数,有多组数据

【题解】

有的时候我们需要快速判断一个数是不是质数

这时候我们需要使用miller-rabin算法

首先,根据费马小定理

我们认识到若p是质数

则a^p=a(mod p)

于是我们使用一个推广的结论

“记n=a*2^b,在[1,n)中随机选取一个整数x,如果x^a ≡1或x^(a*2^i) ≡-1(其中0<=i<b),那么我们认为n是质数。”——ysy

如果这样判断,我们会发现有1/4的概率出错

我们多判断几次即可

除非你是宇宙无敌非洲人

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
#define il inline
#define re register
using namespace std;
typedef long long ll;
int T;
ll n,ans=,a,b;
il int ran(){
return rand()*rand()+rand();
}
il ll pow(ll base,ll pow){
ll ans=;
for(;pow;pow>>=){
if(pow&) ans=ans*base%n;
base=base*base%n;
}
return ans;
}
il bool chk(){
ll x=ran()%(n-)+,now=pow(x,a);
if(now==) return true;
for(int i=;i<b;i++){
if(now==n-) return true;
now=now*now%n;
}
return false;
}
il bool isprime(){
a=n-;b=;
while(a%==){
a/=;b++;
}
for(int i=;i<=;i++)
if(!chk()) return false;
return true;
}
il void init(){
srand(T);ans=;
for(int i=;i<=T;i++){
cin>>n;
ans+=isprime();
}
cout<<ans<<endl;
}
int main(){
while(scanf("%d",&T)!=EOF){
init();
}
return ;
}

hdu2138 Miller_Rabin的更多相关文章

  1. HDU2138 素数判定

    HDU2138 给定N个32位大于等于2的正整数 输出其中素数的个数 用Miller Rabin 素数判定法 效率很高 数学证明比较复杂,略过, 会使用这个接口即可. #include<iost ...

  2. hdu2138 How many prime numbers 米勒测试

    hdu2138 How many prime numbers #include <bits/stdc++.h> using namespace std; typedef long long ...

  3. Miller_Rabin素数测试

    #include<iostream> #include<cmath> #include<cstdio> #include<cstring> #inclu ...

  4. HDU2138 & 米勒拉宾模板

    题意: 给出n个数,判断它是不是素数. SOL: 米勒拉宾裸题,思想方法略懂,并不能完全理解,所以实现只能靠背模板.... 好在不是很长... Code: /*==================== ...

  5. 【数论】Miller_Rabin

    Miller_Rabin素数测试     Miller_Rabin判断单个素数的方法运用了费马小定理,可以说非常之快了.     Miller_Rabin曾经被称作“黑科技”,但是根据费马小定理其实完 ...

  6. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  7. 优化后的二次测试Miller_Rabin素性测试算法

    ll random(ll n) { return (ll)((double)rand()/RAND_MAX*n + 0.5); } ll pow_mod(ll a,ll p,ll n) { ) ; l ...

  8. Miller_Rabin (米勒-拉宾) 素性测试

    之前一直对于这个神奇的素性判定方法感到痴迷而又没有时间去了解.借着学习<信息安全数学基础>将素性这一判定方法学习一遍. 首先证明一下费马小定理. 若p为素数,且gcd(a, p)=1, 则 ...

  9. HDU-3864 D_num Miller_Rabin和Pollard_rho

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864 题意:给定一个数n,求n的因子只有四个的情况. Miller_Rabin和Pollard_rho ...

随机推荐

  1. K-SVD字典学习及其实现(Python)

    算法思想 算法求解思路为交替迭代的进行稀疏编码和字典更新两个步骤. K-SVD在构建字典步骤中,K-SVD不仅仅将原子依次更新,对于原子对应的稀疏矩阵中行向量也依次进行了修正. 不像MOP,K-SVD ...

  2. Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第5节: 同线程回收对象

    Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 第五节: 同线程回收对象 上一小节剖析了从recycler中获取一个对象, 这一小节分析在创建和回收是同线程的 ...

  3. nodejs加载模块心得,mongoose的继承,schematype的mixd介绍

    1. require("xxx")可以是原生模块, 也可以是根目录“/node_modules”下的某个模块 2. 多个模块的package.json使用同一个相同模块的时候,将改 ...

  4. time命令详情

    基础命令学习目录首页 原文链接:https://blog.csdn.net/adaptiver/article/details/6596143?utm_source=blogxgwz3 linux下t ...

  5. Vue 入门之数据绑定

    什么是双向绑定? Vue 框架很核心的功能就是双向的数据绑定. 双向是指:HTML 标签数据 绑定到 Vue 对象,另外反方向数据也是绑定的.通俗点说就是,Vue 对象的改变会直接影响到 HTML 的 ...

  6. centos下设置自启动和配置环境变量的方法

    1. 设置自启动 在CentOS系统下,主要有两种方法设置自己安装的程序开机启动.1.把启动程序的命令添加到/etc/rc.d/rc.local文件中,比如下面的是设置开机启动httpd. #!/bi ...

  7. BugPhobia开发篇章:Scurm Meeting-更新至0x03

    0x01 :目录与摘要 If you weeped for the missing sunset, you would miss all the shining stars 索引 提纲 整理与更新记录 ...

  8. JAVA开发环境的熟悉

    北京电子科技学院(BESTI) 实 验 报 告 课程:Java程序设计 班级:1352 姓名:马悦 学号:20135235 成绩: 指导教师:娄嘉鹏 实验日期:2015.4.13 实验密级: 预习程度 ...

  9. Leetcode题库——14.最长公共前缀

    @author: ZZQ @software: PyCharm @file: longestCommonPrefix.py @time: 2018/9/16 17:50 要求:查找字符串数组中的最长公 ...

  10. 2018软工实践第八次作业-团队项目UML设计

    团队信息 队员姓名与学号 学号 姓名 博客链接 124 王彬(组长) 点击这里 206 赵畅 点击这里 215 胡展瑞 点击这里 320 李恒达 点击这里 131 佘岳昕 点击这里 431 王源 点击 ...