[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 ...
随机推荐
- python创建与遍历List二维列表
python创建与遍历List二维列表 觉得有用的话,欢迎一起讨论相互学习~Follow Me python 创建List二维列表 lists = [[] for i in range(3)] # 创 ...
- [整理]javascript压缩、格式化
1.使用packer来压缩JS文件 packer工具在线版:http://dean.edwards.name/packer/ 通过packer对js打包压缩的同时,执行Base62 encode编码后 ...
- 20155235 2016-2017-2 《Java程序设计》第8周学习总结
20155235 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 第十四章 NIO与NIO2 认识NIO NIO概述 Channel架构与操作 Buffer架 ...
- mysql修改时区
查看mysql当前时间 查看mysql时区设置 查看mysql数据库的时区 mysql修改时区的几种方法
- Caffe 碎碎念
Window Data Layer window data layer 的数据是存在硬盘上的图片, 需要在一个txt里指定用于训练或测试的图片以及bounding box, bounding box ...
- LeetCode-Valid Number - 有限状态机
判断合法数字,之前好像在哪里看到过这题, 记得当时还写了好久,反正各种改, 今天看到了大神的解法(https://github.com/fuwutu/LeetCode/blob/master/Vali ...
- 20165230 ch02 课上测试
题目一 1.参考附图代码,编写一个程序 "week0201学号.c",判断一下你的电脑是大端还是小端. 2.提交运行结果"学号XXXX的笔记本电脑是X端"的运行 ...
- 优化MySQL的21个建议 – MySQL Life【转】
今天一个朋友向我咨询怎么去优化 MySQL,我按着思维整理了一下,大概粗的可以分为21个方向. 还有一些细节东西(table cache, 表设计,索引设计,程序端缓存之类的)先不列了,对一个系统,初 ...
- Nginx报错:upstream timed out (110: Connection timed out)和client intended to send too large body【转】
nginx日志报错 2018/01/26 16:36:49 [error] 23327#0: *54953 upstream timed out (110: Connection timed out) ...
- PHPWeb开发相关知识
转载:https://blog.csdn.net/wj610671226/article/details/78426698 正则表达式 作用:分割.查找.匹配.替换字符串 分割符:正斜线(/).has ...