[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 ...
随机推荐
- java字符数组char[]和字符串String之间的转换
java字符数组char[]和字符串String之间的转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 使用String.valueOf()将字符数组转换成字符串 void (){ cha ...
- css原生变量var()
了解css/css3原生变量var 阮一峰css变量教程 深入学习css自定义属性(css变量)
- 从url到页面加载浏览器做了什么?
从输入url到页面加载发生了什么?1.DNS解析DNS解析是一个递归查询的过程.DNS解析的过程就是寻找哪台机器上有你需要资源的过程,当你在浏览器中输入一个地址时,www.baidu.com.其实不是 ...
- Spark记录-源码编译spark2.2.0(结合Hive on Spark/Hive on MR2/Spark on Yarn)
#spark2.2.0源码编译 #组件:mvn-3.3.9 jdk-1.8 #wget http://mirror.bit.edu.cn/apache/spark/spark-2.2.0/spark- ...
- LaTeX字体设置
% 导言区 % 帮助文档 texdoc lshort-zh % 设置normalsize大小 \documentclass[10pt]{ctexart} %article,ctexbook封面, ct ...
- [转载]Brackets - 强大免费的开源跨平台Web前端开发工具IDE (HTML/CSS/Javascript代码编辑器)
http://brackets.io/ Brackets 是一个免费.开源且跨平台的 HTML/CSS/JavaScript 前端 WEB 集成开发环境 (IDE工具).该项目由 Adobe 创建和维 ...
- Javascript摸拟自由落体与上抛运动 说明!
JavaScript 代码 //**************************************** //名称:Javascript摸拟自由落体与上抛运动! //作者:Gloot //邮箱 ...
- 20155227 2016-2017-2 《Java程序设计》第五周学习总结
20155227 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 语法与继承架构 使用try...catch JVM会尝试执行try区块中的程序代码,如果发生 ...
- SQL SERVER C#数据库操作类(连接、执行SQL)
using System; using System.Collections; using System.Collections.Specialized; using System.Data; usi ...
- 在maven 2工程中加入iTextAsian支持(maven添加自定义jar包到本地仓库)
最近需要在工程中加入JasperReports,其中要用到把报表导出为pdf文件的功能.JasperReports内部使用iText来输出pdf文档,而iText对中文是放在单独的包iTextAsia ...