来源:

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的更多相关文章

  1. hdu2138 How many prime numbers 米勒测试

    hdu2138 How many prime numbers #include <bits/stdc++.h> using namespace std; typedef long long ...

  2. 2018.12.17 hdu2138 How many prime numbers(miller-rbin)

    传送门 miller−rabbinmiller-rabbinmiller−rabbin素数测试的模板题. 实际上miller−rabinmiller-rabinmiller−rabin就是利用费马小定 ...

  3. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  4. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  5. 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 ...

  6. 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 ...

  7. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  8. POJ2739 Sum of Consecutive Prime Numbers(尺取法)

    POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...

  9. Alexandra and Prime Numbers(思维)

    Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

随机推荐

  1. Codeforces 338 D. GCD Table

    http://codeforces.com/problemset/problem/338/D 题意: 有一张n*m的表格,其中第i行第j列的数为gcd(i,j) 给出k个数 问在这张表格中是否 有某一 ...

  2. bzoj千题计划181:bzoj1878: [SDOI2009]HH的项链

    http://www.lydsy.com/JudgeOnline/problem.php?id=1878 之前用莫队做的,现在用树状数组 把每种数的第一个出现位置在树状数组中+1 nxt[i] 记录i ...

  3. FFprobe使用指南

    http://blog.csdn.net/stone_wzf/article/details/45378759 http://blog.chinaunix.net/uid-26000296-id-42 ...

  4. CSS 编码中超级有用的工具集合

    当你开发网站和 Web 应用时,使用合适的工具可以节省大量的时间.本文我将收集一些非常有用的 CSS 编码工具,希望对你有帮助. Pure CSS Pure 是来自雅虎的 CSS 框架,使用 Norm ...

  5. ASP.NET实现网站的自动升级

    网站的自动升级主要是要实现从一台服务器上下载某些文件到本服务器上,然后对下载下来的文件进行更新等操作. 比如,现在有服务器A,服务器B和客户端C. 作为COM公司开发的产品DIV网站系统被安装到服务器 ...

  6. 数据绑定和第一个AngularJS Web应用

    <!DOCTYPE html> <html lang="en" ng-app> <head> <meta charset="UT ...

  7. Axure RP Pro 7.0

    Axure RP Pro专为Rapid Prototype Design而生,它可以辅助产品经理快速设计完整的产品原型,并结合批注.说明以及流程图.框架图等元素将产品完整地表述给各方面设计人员,如UI ...

  8. Ubuntu下mysql使用

    1. 从网上安装 sudo apt-get install mysql-server.装完已经自动配置好环境变量,可以直接使用mysql的命令. 注:建议将/etc/apt/source.list中的 ...

  9. Vue学习系列---安装

    一.前言 学任何东西都是有理由的,目前主要有Angular,React,Vue这三个前端MVVM框架.我选择vue原因很简单,“”入门简单“”,是的只是这个理由.相较于其他2个框架,vue的文档真的是 ...

  10. CAS5.2x单点登录(二)cas服务器连接数据库

    前面一节应该已经告诉大家如何搭建cas的服务器了,可是搭建好能用吗?我们现在的用户验证是在哪呢?哪个默认的用户名和密码有是在哪呢? 本节就讲一下如何使用cas服务器连接我们自己的用户数据库,毕竟没有哪 ...