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. 细数国外SEO,SEM,SNS资深博客论坛和站点

    如果你有时间,如果有英语还不错能看懂国外的推广营销知识,如果你想做个实战者,如果你想比别人多领先,如果你爱好这个推广行业,如果你不想做河塘里的小鱼,如果····请一个个的看以下的站点,个人觉得会给你另 ...

  2. C#网络编程:Socket编程

    套接字简介:套接字最早是Unix的,window是借鉴过来的.TCP/IP协议族提供三种套接字:流式.数据报式.原始套接字.其中原始套接字允许对底层协议直接访问,一般用于检验新协议或者新设备问题,很少 ...

  3. 为什么要点两下才能删除一个li节点 原来是空白节点作怪

    奇怪吧,下面的代码居然要点两次button才能删除一个li节点: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional// ...

  4. GlusterFS源代码解析 —— GlusterFS 内存分配方式

    原文地址:http://blog.csdn.net/wangyuling1234567890/article/details/24564891 GlusterFS 的内存分配主要有两种方式,一种是内存 ...

  5. xshell中配置linux密钥登陆

    参见 http://www.aiezu.com/system/linux/xshell_ssh_public-key_login.html 配置只能秘钥登陆 http://blog.csdn.net/ ...

  6. Spket安装

    1.http://www.spket.com/下载1.6.23放到Eclipse或者myEclipse的dropins文件夹下 2. 3. 4. 我的是jquery-1.8.2.js,2.1.1不会提 ...

  7. SharePoint 2013 和 SharePoint 2010 功能对比

    微软的网站找东西太麻烦了,在这记录一下. 从 SharePoint 2010 更改为 SharePoint 2013 http://technet.microsoft.com/zh-cn/librar ...

  8. nginx+keepalived实现双机热备高可用性

    搭建准备: 机器两台 ip分别为192.168.100.128 192.168.100.129(能够用虚拟机測试.虚拟机网络模式为NET模式.且为静态ip) 另外须要准备一个虚拟ip对外提供服务.即通 ...

  9. Solr局部或指定字段更新之set用法

    solr wiki文档也有        http://yonik.com/solr/atomic-updates/         java code   public static void up ...

  10. 本地aar文件引用

    有时须要使用第三方的aar库.或是project源码越来越大.项目内分工须要或出于模块化考虑.须要引用aar文件. arr就像C/C++中的静态库. 怎样建一个aar.网上的文章非常多,这里不再重述. ...