HDU2138 素数判定
HDU2138 给定N个32位大于等于2的正整数 输出其中素数的个数
用Miller Rabin 素数判定法 效率很高
数学证明比较复杂,略过, 会使用这个接口即可。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long int LL;
LL getPrime(bool notprime[],LL prime[],LL N)
{
LL tot=0;
for(LL i=2;i<=N;i++)
{
if(!notprime[i]) prime[tot++]=i;
for(LL j=0;j<tot;j++)
{
if(prime[j]*i>N)break;
notprime[prime[j]*i]=true;
if(i%prime[j]==0)break;
}
}
return tot;
}
LL pow_mod(LL n,LL k,LL p)
{
if(k==0)return 1;
LL w=1;
if(k&1)w=n%p;
LL ans=pow_mod(n*n%p,k>>1,p);
return ans*w%p;
}
bool Miller_Rabin(LL n,LL a,LL d)
{
if(n==2)return true;
if(n==a)return true;
if(n%a==0)return false;
if((n&1)==0)return false;
while(!(d&1))d=d>>1;
LL t=pow_mod(a,d,n);
if(t==1)return true;//特别注意!!
while((d!=n-1)&&(t!=1)&&(t!=n-1))
{
t=(LL)t*t%n;
d=d<<1; } return (t==n-1||(d&1)==1);
}
bool isPrime(LL n,LL times)
{
if(n<2)return false;
LL a[4]={2,3,5,7};
for(LL i=0;i<4;i++){if(!Miller_Rabin(n,a[i],n-1))return false;}
return true;
}
int main()
{
LL np;
while(scanf("%lld",&np)==1)
{
LL ans=0,now;
for(LL i=0;i<np;i++)
{
scanf("%lld",&now);
if(isPrime(now,1000000))ans++;
}
printf("%lld\n",ans);
}
return 0;
}
HDU2138 素数判定的更多相关文章
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDOJ2012素数判定
素数判定 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- algorithm@ 大素数判定和大整数质因数分解
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...
- Codevs 1702 素数判定 2(Fermat定理)
1702 素数判定 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 一个数,他是素数么? 设他为P满足(P< ...
- hdu 2012 素数判定 Miller_Rabbin
素数判定 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 素数判定 AC 杭电
素数判定 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 杭电ACM 素数判定
素数判定 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&2429
素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: h ...
- 多项式求和,素数判定 HDU2011.2012
HDU 2011:多项式求和 Description 多项式的描述如下: 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ... 现在请你求出该多项式的前n项的和. Input ...
随机推荐
- pandas的合并、连接、去重、替换
import pandas as pd import numpy as np # merge合并 ,类似于Excel中的vlookup df1 = pd.DataFrame({'key': ['K0' ...
- 如何设置路由器的MTU
前几天搞了个ER-X,总觉得没有发挥其最大的能力.今天查了下如何设置MTU,罗列如下,备忘. 1. 目前都是PPPOE,这个不管网络如何复杂,均不要在路由后面计算封包大小.正确的是电脑直接连猫,直接拔 ...
- JQuery判断radio是否选中并获取选中值的示例代码
这篇文章主要介绍了JQuery判断radio是否选中并获取选中值的方法,代码很简单,但很实用,需要的朋友可以参考下 其他对radio操作功能,以后在添加.直接上代码,别忘记引用JQuery包 ? 1 ...
- Path Sum(参考别人,二叉树DFS)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- MySQL JDBC URL参数(转)
MySQL的 JDBC URL格式: jdbc:mysql://[host][,failoverhost...][:port]/[database] » [?propertyName1][=prope ...
- JAVA分布式架构
- Map根据value排序ASC DESC
原文:http://blog.csdn.net/k21325/article/details/53259180 需求有点刁钻,写关键词组合匹配标题的时候,遇到关键词像这样 XXX XXX 1222 X ...
- SAS编程基础 - 菜鸟入门常用操作
1. SAS9.4添加和取消注释的快捷键? Ctrl+/:添加注释 Ctrl+Shift+/:取消注释 2. 如何强制终止程序运行? 看到那个圆圈里带叹号的图标了吗?没错,就是它 - 中断! 3. 如 ...
- zabbix学习系列之基础概念
触发器 概念 "监控项"仅负责收集数据,而通常收集数据的目的还包括在某指标对应的数据超出合理范围时给相关人员发送警告信息,"触发器"正式英语为监控项所收集的数据 ...
- [Java] 实验5參考代码
实验4月3日晚截止,实验截止后将在此给出完整的參考代码. 1. 怎样使用以下的代码模板: 1.1 在eclipse中创建相应名称的类 1.2 将代码拷贝到类文件中 1.3 在//todo凝视中 ...