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 ...
随机推荐
- 2017.2.16 开涛shiro教程-第十七章-OAuth2集成(一)服务器端
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十七章-OAuth2集成 1.OAuth2介 ...
- 调用聚合数据新闻头条API
基于聚合数据新闻头条接口 支持阅读新闻类型包括: 各类社会.国内.国际.体育.娱乐.科技等资讯,更新周期5-30分钟. 新闻内容类型的多选,支持皮肤功能. 使用前需要有聚合数据账号,并实名制后通过 新 ...
- HDOJ 2829 Lawrence
四边形不等式优化DP Lawrence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- SpringBoot学习之启动方式
1.通过@SpringBootAppliction注解类启动 启动方法:找到注解类->鼠标右键->run as-> java application. 2 通过maven启动Spri ...
- winform 下载文件显示进度和百分比
/// <summary> /// 下载完成 /// </summary> private void DownloadFileCompleted() { IsComlate = ...
- git 操作分支
1. git 查看本地分支:git branch 2. git 查看所有分支:git branch -a 3. git 新建本地分支:git branch branchName 4. git 新建分支 ...
- CocoaPods安装教程 pod setup很慢解决方案
CocoaPods安装教程 pod setup很慢解决方案 http://www.jianshu.com/p/6230eec137f6
- python升级安装后的yum的修复
升级python版本号后,执行yum # yum -y install openssl 提演示样例如以下: There was a problem importing one of the Pytho ...
- 记录下关于ejabberd及XMPP的官网链接
ejabberd中文翻译 ——http://wiki.jabbercn.org/Ejabberd2:安装和操作指南 XMPP中文翻译: http://wiki.jabbercn.org/XEP-012 ...
- oracle数据库表格操作
create table dept--创建表格( deptno number(2) primary key, dname varchar2(9) check(dname=Upper(dname)), ...