HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )
How many prime numbers
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12955 Accepted Submission(s): 4490
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.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm> using namespace std; long long pow_mod(long long a, long long i, long long n)
{
if(i==0) return 1%n;
long long temp=pow_mod(a, i>>1, n);
temp = temp*temp%n;
if(i&1)
temp = (long long)temp*a%n;
return temp;
} bool test(int n, int a, int dd)
{
if(n==2) return true;
if(n==a) return true;
if( (n&1)==0 ) return false;
while(!(dd&1)) dd=dd>>1; int t=pow_mod(a, dd, n); //调用快速幂函数
while((dd!=n-1) &&(t!=1) && (t!=n-1) )
{
t = (long long)t*t%n;
dd=dd<<1;
}
return (t==n-1 || (dd&1)==1 );
} bool Miller_Rabin_isPrime(int n) //O(logN)
{
if(n<2) return false;
int a[]={2, 3, 61}; //
for(int i=0; i<3; i++)
if(!test(n, a[i], n-1) )
return false;
return true;
} int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
int dd;
int cnt=0;
while(n--)
{
scanf("%d", &dd);
if(Miller_Rabin_isPrime(dd))
cnt++;
}
printf("%d\n", cnt );
}
return 0;
}
HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )的更多相关文章
- HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)
Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...
- HDU 2138 How many prime numbers
米勒罗宾素数测试: /* if n < 1,373,653, it is enough to test a = 2 and 3. if n < 9,080,191, it is enoug ...
- HDU 2138 How many prime numbers (判素数,米勒拉宾算法)
题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <c ...
- POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Ac ...
- poj3006 筛选法求素数模板(数论)
POJ:3006 很显然这是一题有关于素数的题目. 注意数据的范围,爆搜超时无误. 这里要用到筛选法求素数. 筛选法求素数的大概思路是: 如果a这个数是一个质数,则n*a不是质数. 用一个数组实现就是 ...
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
- 【HDU】2138 How many prime numbers
http://acm.hdu.edu.cn/showproblem.php?pid=2138 题意:给n个数判断有几个素数.(每个数<=2^32) #include <cstdio> ...
- hdu 5108 Alexandra and Prime Numbers(水题 / 数论)
题意: 给一个正整数N,找最小的M,使得N可以整除M,且N/M是质数. 数据范围: There are multiple test cases (no more than 1,000). Each c ...
- hdu 5108 Alexandra and Prime Numbers
数论题,本质是求出n的最大质因子 #include<time.h> #include <cstdio> #include <iostream> #include&l ...
随机推荐
- 九度oj题目&吉大考研10年机试题全解
吉大考研机试2010年题目 题目一(jobdu1478:三角形的边). http://ac.jobdu.com/problem.php?pid=1478 给出三个正整数,计算最小的数加上次小的数 ...
- ArrayList和HashSet的比较
ArrayList是数组存储的方式 HashSet存储会先进行HashCode值得比较(hashcode和equals方法),若相同就不会再存储 HashCode和HashSet类 Hashset就是 ...
- 2016.11.4 Injection of autowired dependencies failed
运行项目时,提示错误: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' ...
- Tomcat的Manager显示403 Access Denied
管理tomcat的时候遇到了以下问题: 1.刚开始需要用户名密码,不知道用户名和密码是什么,但是输入什么都不正确. 解决办法: 自己在tomcat-users.xml中按格式添加用户 conf文件夹里 ...
- sql join相关
JOIN: 如果表中有至少一个匹配,则返回行 LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行,返回左表所有行 RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行,返回右表 ...
- RobotFramework-Selenium2Library--关键字
Selenium2Library用户关键字 *** Settings *** Library Selenium2Library *** Keywords *** Checkbox应该不被选择 [Arg ...
- Nginx在windows2003下的使用 PHP
nginx真正能够发挥其良好的负载能力的,是在linux下. 我们在windows下搭建好环境,测试开发使用.强烈建议在linux下开发的. ---- 下载nginx 访问www.nginx.org, ...
- jquery,smarty,dedecms的插件思路------dede未实践
1.jquery定义一个新函数,这个函数可以使用jquey的所有功能 2.smarty,dede其实也和jquery一样,不过是可以使用系统的一些方法而已 3.可能还有一些规范,如smarty插件的命 ...
- linux SPI驱动——spi协议(一)
一:SPI简介以及应用 SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在 ...
- erlang程序优化点的总结
注意,这里只是给出一个总结,具体性能需要根据实际环境和需要来确定 霸爷指出,新的erlang虚拟机有很多调优启动参数,今后现在这个方面深挖一下. 1. 进程标志设置: 消息和binary内存:erla ...