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 ...
随机推荐
- 3 并发编程-(进程)-join方法
1.查看当前进程的进程号getpid() 和 其父 进程号 getppid() # 查看进程的pid from multiprocessing import Process import time,o ...
- kernel TCP time wait bucket table overflow
# 故障描述 有一个需求是实时分析API接口访问日志,提取token去数据库查询对应的uid,然后收集一些指标存入到hbase中. 当程序执行一会后会被系统杀死 Killed ! # 故障排查 .CP ...
- eclipse 使用tomcat7.0建立Dynamic Web Project 时 web.xml的问题
最近使用Eclipse helios版本结合tomcat7.0建立动态的web项目时,发现在WEB-INF下的web.xml没有了. 解决方案: 建立web项目时,建到第三个下一步时,将 Genera ...
- homework 补第三题
上次只写了两道题 发现少了一道 所以今天补上 4 . 另外补上今天的程序代码 代码 1. 2. 3 4 运行截图 1. 2 3
- [图解tensorflow源码] 入门准备工作
tensorflow使用了自动化构建工具bazel.脚本语言调用c或cpp的包裹工具swig.使用EIGEN作为矩阵处理工具.Nvidia-cuBLAS GPU加速计算库.结构化数据存储格式prot ...
- substring_index 用法
substring_index http://blog.csdn.net/wolinxuebin/article/details/7845917 1.substring_index(str,delim ...
- python json5
install pip install json5 test a.json: { 'a':'b', 'aa':['b1','b2']} =========================== impo ...
- windows服务启动的进程无窗口
勾选允许服务与桌面交互 指服务是否在桌面上提供用户界面,当服务启动后不论是谁登录都能使用.只有作为 LocalSystem 帐户(由“此帐户”指定)运行时,该选项才能使用. 如果一个服务需要界面(比如 ...
- 好用的模板引擎NVelocity
CastleNVelocity-1.1.1,使用方法: 把dll放到项目中,添加引用,修改配置的文件夹以及数据模型,最后在逻辑代码中调用即可. 封装到CommonHelper.cs using Sys ...
- html中的innerHTML
首先看个例子 <html> <head> <script type="text/javascript"> function getInnerHT ...