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. setTag和findViewByTag的使用具体解释

    在使用ListView或者GridView的时候. 假设想要在Aciviry中获取到Item中的子View,比較频繁的使用是:getChildAt(int position): 之前自己差点儿不会去使 ...

  2. AngularJS中Route例子

    代码:https://files.cnblogs.com/files/xiandedanteng/angularJSRouteSample.rar 点击‘首页’后: 点击‘电脑’后: <!DOC ...

  3. HBase笔记

    吴超 1.1 Hbase是Hadoop中的数据库,Hadoop还需要数据库吗?我们学的Hadoop是一个分布式的存储和计算的平台 为什么要在他上面建一个数据库呢,数据库是干什么的呢,数据库是一个管理系 ...

  4. 记录MySQL运行的SQL

    对照Oracle功能去学习Mysql总会发现亮点 Oracle中通过日志挖掘这一技能,能够找到以前运行过的全部记录: Mysql中也提供了3种方法{验证过的,我会记录详细做法} 方法1:{已验证} 记 ...

  5. nightwatch 切换窗口

    .switchWindow() Change focus to another window. The window to change focus to may be specified by it ...

  6. 【Union Find】JAVA implementation

    import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Scan ...

  7. 37:密码截取(回文串manacher算法)

    题目描述:Catcher是MCA国的情报员,他工作时发现敌国会用一些对称的密码进行通信,比如像这些ABBA,ABA,A,123321,但是他们有时会在开始或结束时加入一些无关的字符以防止别国破解.比如 ...

  8. PHP计划任务:如何使用Linux的Crontab执行PHP脚本(转)

    我们的PHP程序有时候需要定时执行,我们可以使用ignore_user_abort函数或是在页面放置js让用户帮我们实现.但这两种方法都不太可靠,不稳定.我们可以借助Linux的Crontab工具来稳 ...

  9. Oracle 11g新增not null的字段比10g快--新特性

    在11g之前添加一个not null的字段很慢.在11g之后就很快了.我们先做一个測试,然后探究下原理. SQL> select * from v$version; BANNER ------- ...

  10. MVC项目总结

    View命名 View下有多个模块的文件夹,我们根据微软的规定,每个模块下的首页都为Index.cshtml命名 获得当前页面的控制器名称 var currentControllerName = th ...