HDU 2138 How many prime numbers (判素数,米勒拉宾算法)
题意:给定一个数,判断是不是素数。
析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法。
代码如下:
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <queue>
- #include <vector>
- #include <cstring>
- #include <map>
- #include <cctype>
- using namespace std;
- typedef long long LL;
- const int maxn = 1000 + 5;
- LL qpow(int a, int b, int r){
- LL ans = 1;
- LL k = a % r;
- while(b){
- if(b & 1) ans = (ans * k) % r;
- k = (k * k) % r;
- b >>= 1;
- }
- return ans;
- }
- bool miller_rabbin(int n, int a){
- int r = 0,s = n-1;
- if(!(n % a)) return false;
- while(!(s & 1)){ s >>= 1; ++r; }
- LL k = qpow(a, s, n);
- if(1 == k) return true;
- for(int j = 0; j < r; ++j, k = k * k % n)
- if(k == n-1) return true;
- return false;
- }
- bool is_prime(int n){
- int tab[] = {2, 3, 5, 7};
- for(int i = 0; i < 4; ++i){
- if(n == tab[i]) return true;
- if(!miller_rabbin(n, tab[i])) return false;
- }
- return true;
- }
- int main(){
- // freopen("in.txt", "r", stdin);
- int n, x;
- while(~scanf("%d", &n)){
- int cnt = 0;
- for(int i = 0; i < n; ++i){
- scanf("%d", &x);
- if(is_prime(x)) ++cnt;
- }
- printf("%d\n", cnt);
- }
- }
HDU 2138 How many prime numbers (判素数,米勒拉宾算法)的更多相关文章
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- 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(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )
How many prime numbers Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 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 ...
- csu 1552(米勒拉宾素数测试+二分图匹配)
1552: Friends Time Limit: 3 Sec Memory Limit: 256 MBSubmit: 723 Solved: 198[Submit][Status][Web Bo ...
- Miller_Rabin(米勒拉宾)素数测试
2018-03-12 17:22:48 米勒-拉宾素性检验是一种素数判定法则,利用随机化算法判断一个数是合数还是可能是素数.卡内基梅隆大学的计算机系教授Gary Lee Miller首先提出了基于广义 ...
- How many prime numbers(素数)
Problem Description Give you a lot of positive integers, just to find out how many prime numbers t ...
- Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)
385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...
- Sum of Consecutive Prime Numbers(素数打表+尺取)
Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...
随机推荐
- Hibernate常用网址
Hibernate 官网用户手册: http://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide ...
- 获取request错误信息
from: https://stackoverflow.com/questions/19370436/get-errno-from-python-requests-connectionerror 当使 ...
- 集中化管理平台Saltstack安装配置
salt是一个异构平台基础设置管理工具(虽然我们通常只用在Linux上),使用轻量级的通讯器ZMQ,用Python写成的批量管理工具,完全开源,遵守Apache2协议,与Puppet,Chef功能类似 ...
- CAD2010安装说明
1. 2. 3. 4. 5. . 提供算号服务(也就是付费才能获得的注册码)... 不会的可以问的撒....
- Implementing On Item Click / Double Click for TListView
ListView.On Item Click & ListView.On Item Double Click To be able to locate the clicked (if ther ...
- Linux下php5.3.3安装mcrypt扩展
具体操作: 一.下载软件包 1.下载php(版本要与系统安装的一致) http://pan.baidu.com/s/1mifTbfE 2.下载libmcrypt(安装mcrypt需要此软件包) htt ...
- hdoj1075-What Are You Talking About 【map】
http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS ...
- SpringMVC入门(基于XML方式实现)
----------------------siwuxie095 SpringMVC 入门(基于 XML 方式实现) (一)搭建 SpringMVC 环境 1.先下载相关库文件,下载链接: (1)ht ...
- SVN客户端的安装和使用
----------------------siwuxie095 SVN 客户端的安装 1.SVN 客户端,选择 TortoiseSVN,下载链接: https://tortoisesvn.net/d ...
- .NET中的文件IO操作实例
1.写入文件代码: //1.1 生成文件名和设置文件物理路径 Random random = new Random(DateTime.Now.Millisecond); ); string Physi ...