题意:给定一个数,判断是不是素数。

析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法。

代码如下:

#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 (判素数,米勒拉宾算法)的更多相关文章

  1. FZU 1649 Prime number or not米勒拉宾大素数判定方法。

    C - Prime number or not Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. csu 1552(米勒拉宾素数测试+二分图匹配)

    1552: Friends Time Limit: 3 Sec  Memory Limit: 256 MBSubmit: 723  Solved: 198[Submit][Status][Web Bo ...

  6. Miller_Rabin(米勒拉宾)素数测试

    2018-03-12 17:22:48 米勒-拉宾素性检验是一种素数判定法则,利用随机化算法判断一个数是合数还是可能是素数.卡内基梅隆大学的计算机系教授Gary Lee Miller首先提出了基于广义 ...

  7. How many prime numbers(素数)

    Problem Description   Give you a lot of positive integers, just to find out how many prime numbers t ...

  8. Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)

    385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...

  9. Sum of Consecutive Prime Numbers(素数打表+尺取)

    Description Some positive integers can be represented by a sum of one or more consecutive prime numb ...

随机推荐

  1. as3与php交互

    (1)直接读取 php: <? $state="开始接收"; $var1="收到"; echo "state=".$state.&qu ...

  2. jetty异常

    异常一: java.net.BindException: Address already in use: bind jvm 1 | 2017-10-18 15:08:10,792+0800 WARN ...

  3. 前段开发-css-总结

    应用参考https://www.cnblogs.com/alice-bj/p/8972299.html CSS:层叠样式表(Cascading Style Sheets) 1.css的特征 一.css ...

  4. javascript中 try catch用法

    javascript中 try catch用法 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2015-08-16我要评论 JS try catch语句一般在什么情况下使用?是必须使 ...

  5. Css 特性之 transition和transform

    CSS 有一些新的属性,可以简化代码的书写,用简单的代码就可以实现复杂的变化.不用像 js 那样,需要写很多代码 这里主要介绍三个属性:transition ,transform,以及translat ...

  6. 值得推荐的C/C++开源框架和库

    值得推荐的C/C++开源框架和库  转自:http://www.cnblogs.com/lidabo/p/5514155.html   - 1. Webbench Webbench是一个在Linux下 ...

  7. MySQL 逻辑备份工具

    简介: Mydumper.Myloader 是一个第三方的.开源的 MySQL 逻辑备份工具. 支持多线程,比起 mysqldump 要快很多,也能解决 innobackupex 备份工具对 MyIS ...

  8. VScode 安装必备

    1.运行程序:

  9. js ParseUrl

    js ParseUrl function parseURL(url) { var a = document.createElement('a'); a.href = url; return { sou ...

  10. Mysql两个time类型计算时间相减

    round((UNIX_TIMESTAMP(finishtime)-UNIX_TIMESTAMP(starttime))/60) 得到的时间是分钟数