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

Problem 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
 
算法:每次输入一个数直接判断暴力sqrt判断该数字是不是素数,然后累加素数的个数,也可以水过。
但是也可以拿这道水题那练习Miller_Rabin判断素数法!时间复杂度比较低!
代码:

#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法判断素数 【*模板】 用到了快速幂算法 )的更多相关文章

  1. 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 ...

  2. 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 ...

  3. HDU 2138 How many prime numbers (判素数,米勒拉宾算法)

    题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <c ...

  4. POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

    Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Ac ...

  5. poj3006 筛选法求素数模板(数论)

    POJ:3006 很显然这是一题有关于素数的题目. 注意数据的范围,爆搜超时无误. 这里要用到筛选法求素数. 筛选法求素数的大概思路是: 如果a这个数是一个质数,则n*a不是质数. 用一个数组实现就是 ...

  6. HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

  7. 【HDU】2138 How many prime numbers

    http://acm.hdu.edu.cn/showproblem.php?pid=2138 题意:给n个数判断有几个素数.(每个数<=2^32) #include <cstdio> ...

  8. hdu 5108 Alexandra and Prime Numbers(水题 / 数论)

    题意: 给一个正整数N,找最小的M,使得N可以整除M,且N/M是质数. 数据范围: There are multiple test cases (no more than 1,000). Each c ...

  9. hdu 5108 Alexandra and Prime Numbers

    数论题,本质是求出n的最大质因子 #include<time.h> #include <cstdio> #include <iostream> #include&l ...

随机推荐

  1. MetaQ对接SparkStreaming示例代码

    由于JavaReceiverInputDStream<String> lines = ssc.receiverStream(Receiver<T> receiver) 中 没有 ...

  2. Linux学习之十二-Linux文件属性

    Linux文件属性 在Linux中,对于每个文件都有相应属性,以Linux中root用户家目录下新建文件a.txt为例,在a.txt中输入几个字符 使用命令ls -ild a.txt查看文件的权限等 ...

  3. 两段用来启动/重启Linux下Tomcat的Perl脚本

    两段代码,第二段比较好些. 下面是Split输出结果方式的代码: #!/usr/local/bin/perl #Date:2015-07-07 print "Begin to restart ...

  4. 谈 API 的撰写 - 子系统

    在做一个系统时,有一些子系统几乎是必备的:配置管理,CLI,以及测试框架. 配置管理 我们先说配置管理.一个系统的灵活度,和它的配置管理是离不开的.系统中存在的大量的预置的属性(下文简称 proper ...

  5. Archlinux 下的 VMWare Workstation 维护笔记

    印象中 Archlinux 下的 VMWare Workstation 总是出问题, 因此写这个帖子, 记录出问题时间/原因/解决方案. PS: 每次更新内核后可能需要重新编译 vmware 的内核模 ...

  6. UVA12096 - The SetStack Computer(set + map映射)

    UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来, ...

  7. 何为SLAM

    名词解释:        SLAM (simultaneous localization and mapping),也称为CML (Concurrent Mapping and Localizatio ...

  8. Struts2学习二----------访问Servlet API

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2提供了三种方式去访问Servlet API -ActionContext -实现*Aware接口 -ServletActionConte ...

  9. mysql日期格式转化

    select DATE_FORMAT( '20170701', '%Y-%m-%d'); 先挖坑

  10. 多媒体开发之rtp打包---打包中的FU-A分包方式说明

    继上篇rtp中的时间戳和负载类型之后,升入到了nalu的分片打包问题,这里做下笔记 (1)fu-a的打包格式 1.基于RTP协议的打包及解包 (1)单个NAL打包 H.264NALU单元常由[star ...