[HDU2138]How many prime numbers
来源:
HDU 2007-11 Programming Contest_WarmUp
题目大意:素数判定。
思路:
事实上暴力判定也可以过,但我还是用了Miller-Rabin算法。
核心思想:利用费马小定理,得到对于质数$p$,我们有$a^{p-1}\equiv 1(mod\ p)$或$a^p\equiv a(mod\ p)$。
反过来,满足条件的不一定是质数,但有很大概率是质数,因此我们只要多随机几个$a$来判定,出错的概率就非常低了。
求幂的运算可以使用Montgomery模幂算法。
注意就算数据在int范围内,中间的运算结果一样会爆int。
一开始还把快速幂中底数和指数的位置打反。
#include<ctime>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#define int long long
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
inline int Montgomery(int a,int b,const int p) {
int ret=;
while(b) {
if(b&) ret=ret*a%p;
a=(long long)a*a%p;
b>>=;
}
return ret;
}
inline bool MillerRabin(const int x) {
if(x==) return true;
for(int i=;i<;i++) {
int a=rand()%(x-)+;
if(Montgomery(a,x-,x)!=) return false;
}
return true;
}
signed main() {
srand(time(NULL));
int n;
while(~scanf("%lld",&n)) {
int ans=;
while(n--) {
if(MillerRabin(getint())) ans++;
}
printf("%lld\n",ans);
}
return ;
}
暴力代码:
#include<cmath>
#include<cstdio>
#include<cctype>
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
inline bool isPrime(const int x) {
for(int i=;i<=floor(sqrt(x));i++) {
if(!(x%i)) return false;
}
return true;
}
int main() {
int n;
while(~scanf("%d",&n)) {
int ans=;
while(n--) {
if(isPrime(getint())) ans++;
}
printf("%d\n",ans);
}
return ;
}
[HDU2138]How many prime numbers的更多相关文章
- hdu2138 How many prime numbers 米勒测试
hdu2138 How many prime numbers #include <bits/stdc++.h> using namespace std; typedef long long ...
- 2018.12.17 hdu2138 How many prime numbers(miller-rbin)
传送门 miller−rabbinmiller-rabbinmiller−rabbin素数测试的模板题. 实际上miller−rabinmiller-rabinmiller−rabin就是利用费马小定 ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2739 Sum of Consecutive Prime Numbers(尺取法)
题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Description S ...
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is ...
- 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 ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- Alexandra and Prime Numbers(思维)
Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
随机推荐
- 解决pdf打印预览中遇到特殊字符,导出失败问题
本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处. 由于近日由于pdf中存在特殊字符导致导出失败,主要原因是"&"字符与freema ...
- centos下安装python3.6.2
一.下载 官网地址:https://www.python.org/downloads/source/ 我下载的是最新的3.6.2rc版本 # cd /opt/ wget https://www.pyt ...
- ASP.NET MVC学习(三)之过滤器Filter
http://www.cnblogs.com/yaopengfei/p/7910763.html
- 作为一个有B格的前端工程师需要掌握的一些知识
如果说你3年还在不停地切页面的... 那么你对http协议的了解程度 你的原生的javascript的掌握程度 你的页面的优化的理念 你在写页面是否会有什么独特地技巧 你对ajax的get和post方 ...
- 阿里云Linux服务器挂载数据盘
步骤1.登录服务器2.检查磁盘信息 命令:fdisk -l3.磁盘分区 命令:fdisk /dev/xvdb 查看命令帮助 m n //新增一个分区 p //建立一个主分区 1 //设置盘符为1 回车 ...
- 转载 为什么print在Python 3中变成了函数?
转载自编程派http://codingpy.com/article/why-print-became-a-function-in-python-3/ 原作者:Brett Cannon 原文链接:htt ...
- 2017/05/21 java 基础 随笔
工具类:所有的方法都是静态的,如果一个类中所有的方法都是静态的,需要再多做一步,私有构造方法,不让其他类创建本类对象. 生成文档: java.lang 包不用导入 常见代码块的应用 * a:局部 ...
- python代码在IDE下调试设置命令行参数
带命令行参数的代码在IDE下调试,需要把参数赋值,本文mark一下具体的命令行参数在代码中赋值方法. if __name__ == "__main__": sys.argv = [ ...
- js实现避免浏览器拦截弹出新页面的方法
1 问题描述 点击button按钮,提交页面的form表单,后台执行完毕后返回参数,前台页面需要该参数实现跳转,如何实现保留该原来的页面,并在浏览器选项卡新建一个页面,且不被浏览器拦截? 2 方法及问 ...
- zabbix报警Too many processes on zabbix server
zabbix大量报警,运行进程过多,但实际有部分机器可以忽略,需要关闭相关的报警 Configuration-->Templates找到Template_Linux点该行的 Triggers选择 ...