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

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

代码如下:

#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 string split方法一些注意

    split () 方法 AS3 function split(delimiter:*, limit:Number = 0x7fffffff):Array 如果指定 limit 参数,返回的数组中具有的 ...

  2. Java并发知识(2)

    1. 什么是原子操作?在Java Concurrency API中有哪些原子类(atomic classes)? 原子操作是指一个不受其他操作影响的操作任务单元.原子操作是在多线程环境下避免数据不一致 ...

  3. 迷你MVVM框架 avalonjs 学习教程21、双向绑定链

    avalon的双向绑定机制,是通过一条依赖链实现.此依赖链最底层是监控属性.监控数组,中层是计算属性.监控函数,再上点是求值函数,最上层是视图刷新函数. 所谓计算属性,监控属性,监控函数属性,我们改变 ...

  4. 01Tensorflow学习之Tensorflow基本介绍

    1 tensorflow简介 TensorFlow是谷歌基于DistBelief进行研发的第二代人工智能学习系统,其命名来源于本身的运行原理.Tensor(张量)意味着N维数组,Flow(流)意味着基 ...

  5. Containerpilot 配置文件 之 Jobs

    ContainerPilot job是用户定义的进程和规则,用于何时执行它,如何进行健康检查,以及如何向Consul做广告. 这些规则旨在允许灵活性覆盖几乎可能要运行的任何类型的进程. 一些可能的jo ...

  6. Eclipse出现错误:The selection cannot be launched,and there are no recent launches

    刚装了eclipse,想写个Java程序测试一下能不能用,结果一run就出现错误,Debug也是同样的错误,错误内容为:the selection cannot be launched,and the ...

  7. 大型运输行业实战_day05_1_登录+注销+表单重复提交+登录拦截器

    1.登录 登录实现如下步骤: 1.在首页中添加登录按钮 html代码如下: <%@ page contentType="text/html;charset=UTF-8" la ...

  8. go语言中make和new的区别

    make用于内建类型(map.slice 和channel)的内存分配.new用于各种类型的内存分配. 内建函数new本质上说跟其他语言中的同名函数功能一样:new(T)分配了零值填充的T类型的内存空 ...

  9. cf-Round542-Div2-B(贪心)

    题目链接:http://codeforces.com/contest/1130/problem/B 思路: 贪心题.定义结构体数组a,a[i].x[0],a[i].x[1]分别表示i出现的第一个下标和 ...

  10. python 的时间与日期

    显示当前日期: import time print time.strftime('%Y-%m-%d %A %X %Z',time.localtime(time.time())) 或者 你也可以用: p ...