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 ...
随机推荐
- 谷歌应用商店chrome扩展程序和APP的发布流程
互联网上有很多大牛,他们再工作中需要一些难题,再找到解决办法后,如果会使用js的话,大多数人就可以自己动手写一个chrome插件,而且非常容易.开发人员都喜欢与大家分享自己的成就!google是一个全 ...
- 利用类装饰器自定制property实现延迟计算
class LazyProperty: ''' hello,我是非数据描述符(没有定义__set__,不然是大哥数据描述符了--!) ''' def __init__(self, func): pri ...
- 数据结构代码实现之队列的链表实现(C/C++)
上班闲着无聊,一直想着要开始写博客,但又不知道写什么.最近又回顾了下数据结构的知识,那就从数据结构开始吧. 前言 关于C语言结构体的知识以及队列的特性请读者自行了解,此处不做过多解释,嘻嘻. 同时此篇 ...
- 集训第四周(高效算法设计)N题 (二分查找优化题)
原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...
- java字符串利用正则表达式分割
平时用到,整理的,总感觉缺点什么: private String getKeywordByContent(String content) { StringBuffer sbAllKwyword = n ...
- poj 3667 Hotel (线段树的合并操作)
Hotel The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...
- 玛丽卡(codevs 1021)
题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们 ...
- intent使用Serializable传递对象
package com.pingyijinren.test; import android.content.Intent; import android.support.v7.app.AppCompa ...
- UVA 11400_ Lighting System Design
题意: 给定一系列灯泡的额定功率,电源价钱,一个灯泡的价格以及系统所需该种灯泡的数量.已知流过灯泡的电流相等,所以为省钱可以将电压小的灯泡换成电压大的灯泡,但是不能换成电压更小的灯泡,问最少要花多少钱 ...
- mybatis association和collection标签怎么用
<resultMap type="Bill" id="ResultBill"> <id property="id" col ...