来源:

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。
一开始还把快速幂中底数和指数的位置打反。

  1. #include<ctime>
  2. #include<cstdio>
  3. #include<cctype>
  4. #include<cstdlib>
  5. #define int long long
  6. inline int getint() {
  7. char ch;
  8. while(!isdigit(ch=getchar()));
  9. int x=ch^'';
  10. while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
  11. return x;
  12. }
  13. inline int Montgomery(int a,int b,const int p) {
  14. int ret=;
  15. while(b) {
  16. if(b&) ret=ret*a%p;
  17. a=(long long)a*a%p;
  18. b>>=;
  19. }
  20. return ret;
  21. }
  22. inline bool MillerRabin(const int x) {
  23. if(x==) return true;
  24. for(int i=;i<;i++) {
  25. int a=rand()%(x-)+;
  26. if(Montgomery(a,x-,x)!=) return false;
  27. }
  28. return true;
  29. }
  30. signed main() {
  31. srand(time(NULL));
  32. int n;
  33. while(~scanf("%lld",&n)) {
  34. int ans=;
  35. while(n--) {
  36. if(MillerRabin(getint())) ans++;
  37. }
  38. printf("%lld\n",ans);
  39. }
  40. return ;
  41. }

暴力代码:

  1. #include<cmath>
  2. #include<cstdio>
  3. #include<cctype>
  4. inline int getint() {
  5. char ch;
  6. while(!isdigit(ch=getchar()));
  7. int x=ch^'';
  8. while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
  9. return x;
  10. }
  11. inline bool isPrime(const int x) {
  12. for(int i=;i<=floor(sqrt(x));i++) {
  13. if(!(x%i)) return false;
  14. }
  15. return true;
  16. }
  17. int main() {
  18. int n;
  19. while(~scanf("%d",&n)) {
  20. int ans=;
  21. while(n--) {
  22. if(isPrime(getint())) ans++;
  23. }
  24. printf("%d\n",ans);
  25. }
  26. return ;
  27. }

[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. python---django中models配置修改数据库引擎

    Django支持多种数据库,sqlite,mysql,oracle等,其默认数据库是sqlite 在settings文件中可以发现: DATABASES = { 'default': { 'ENGIN ...

  2. Angular2新人常犯的5个错误

    看到这儿,我猜你肯定已经看过一些博客.技术大会录像了,现在应该已经准备好踏上angular2这条不归路了吧!那么上路后,哪些东西是我们需要知道的? 下面就是一些新手常见错误汇总,当你要开始自己的ang ...

  3. ViewGroup.layout(int l, int t, int r, int b)四个输入参数的含义

    ViewGroup.layout(int l, int t, int r, int b)这个方法是确定View的大小和位置的,然后将其绘制出来,里面的四个参数分别是View的四个点的坐标,他的坐标不是 ...

  4. Debian安装Nvidia最简单方法

    电脑配置: Dell本本 i7+gtx1050+8g 安装bumblebee: sudo apt install bumblebee-nvidia primus 以上会自动安装nvidia驱动. bu ...

  5. Three.js基础探寻三——透视投影照相机

    本篇主要介绍Three.js照相机中的透视投影照相机. 上一篇:正交投影照相机 5.透视投影照相机构造函数 透视投影照相机(Perspective Camera)的构造函数是: THREE.Persp ...

  6. 爬虫、框架scrapy

    阅读目录 一 介绍 二 安装 三 命令行工具 四 项目结构以及爬虫应用简介 五 Spiders 六 Selectors 七 Items 八 Item Pipeline 九 Dowloader Midd ...

  7. Python学习二|Python的一些疑问

    最近写了一点Python代码,作为一个java程序员,面对Python这么便捷的语言不禁有点激动.不过呢,有时候也会遇到一些无法理解的东西. 例如: er = [[1,2,3], [4,5,6], [ ...

  8. 报错stale element reference: element is not attached to the page document结局方案

    今天在调试脚本时,遇到如下报错: org.openqa.selenium.StaleElementReferenceException: stale element reference: elemen ...

  9. 关于overflow: hidden;的一个诡异问题

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  10. 使用 Application Loader提交IPA文件到苹果市场

    打包.导出ipa包后剩下的就是要将ipa包推到appstore.Application Loader是苹果提供的ipa包提交工具. 1.启动Application Loader 打开xcode,在xc ...