来源:

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. ThreadLocal实现线程范围的共享变量

    一.如何理解线程范围内共享数据 1.static int num=0; 2.线程1访问num变量,并设置为num=2:线程2访问num变量,并设置为num=3: 3.当线程1中对象A.B.C 在访问线 ...

  2. .NET面试题系列(七)IIS

    应用程序池的集成模式和经典模式的区别 应用程序池模式会影响服务器处理托管代码请求的方式. 如果托管应用程序在采用集成模式的应用程序池中运行,服务器将使用 IIS 和 ASP.NET 的集成请求处理管道 ...

  3. 【原创】backbone1.1.0源码解析之View

    作为MVC框架,M(odel)  V(iew)  C(ontroler)之间的联系是必不可少的,今天要说的就是View(视图) 通常我们在写逻辑代码也好或者是在ui组件也好,都需要跟dom打交道,我们 ...

  4. 20155230 2016-2017-2 《Java程序设计》第九周学习总结

    20155230 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 第十六章 statement在不使用时所关联的resultset也会自动关闭. 要让SQL执 ...

  5. 第10月第1天 storyboard uitableviewcell

    1. 如图,我们在Cell的属性界面对其进行了注册,identifier 为"TableViewCell" 不需要在 ViewDidLoad 对其进行注册了,如果进行注册的话,则对 ...

  6. Ubuntu 14.04 + gnome session back (metacity) 任务栏右上角图标消失问题解决

    没错, 就是说右上角的所有图标 (时间啊, 系统啊所有的)都消失了. 通过下列命令可以恢复 dconf reset -f /org/gnome/gnome-panel/ 参考这篇帖子: Upgrade ...

  7. [cookie篇]cookie-parser之parser.js

    cookie-parser的作用,官方的说法是:Parse Cookie header and populate req.cookies with an object keyed by the coo ...

  8. sqlmap工作流程图

  9. 在win7_64bit + ubuntu-12.04-desktop-amd64+VMware-workstation-full-10.0.1-1379776平台上安装ns-allinone-2.35

    step1.  ns-allinone-2.35的下载地址:http://www.isi.edu/nsnam/ns/ns-build.html#allinone step2.  在虚拟机中打开term ...

  10. javascript研究小组知识库

    http://hzjavaeyer.group.iteye.com/group/wiki?category_id=0