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

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

代码如下:

#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. chrome 设置是否缓存

    在进行本地开发时,因老需要修改js,css等文件,而页面又带有缓存因此无法自动更新为新的文件. 在页面点击 -> F12 ->F1 ->References -> NetWor ...

  2. ABAP-索引

    转载:http://blog.sina.com.cn/s/blog_498610450101kbxl.html tables: csks. start-of-selection. select * u ...

  3. Redis用在哪里

    1. 高并发缓存/共享session:     UserInfo getUserInfo (long id) {}     取:     userRedisKey = "user:info: ...

  4. 关于gevent的一些理解(二)

    3 实际应用 1 zeromq和gevent: zeromq的介绍请参看:http://www.infoq.com/cn/news/2010/09/introduction-zero-mq 假设你已经 ...

  5. 富文本编辑器-UEditor

    官方网址:http://ueditor.baidu.com/website/index.html 下载地址:http://ueditor.baidu.com/website/download.html ...

  6. 再识ASCII实体、符号实体和字符实体

    一.前言            相信大家都熟悉通过字符实体   来实现多个连续空格的输入吧!本文打算对三类HTML实体及JS相关操作作进一步的整理和小结,若有纰漏请大家指正,谢谢. 二.初识HTML实 ...

  7. c++实现扫雷(坐标)

    昨天在观察贪食蛇的代码时,看到了有如何实现扫雷的c++代码,觉得挺有趣,今天便又试了一下 #include <ctime> #include <cstdlib> #includ ...

  8. Mybatis中传入时间值

    <if test="search_content2 != null and search_content2 != ''"> AND add_time <![CDA ...

  9. selenium -- 鼠标悬停

    针对页面上的二级菜单,需要鼠标悬停才能进行操作. /** * Clicks (without releasing) in the middle of the given element. This i ...

  10. toString方法的用法

    public class JLDtoS {   public static void main(String[]args)   {    long a=123;    Long aa=new Long ...