[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---django中models配置修改数据库引擎
Django支持多种数据库,sqlite,mysql,oracle等,其默认数据库是sqlite 在settings文件中可以发现: DATABASES = { 'default': { 'ENGIN ...
- Angular2新人常犯的5个错误
看到这儿,我猜你肯定已经看过一些博客.技术大会录像了,现在应该已经准备好踏上angular2这条不归路了吧!那么上路后,哪些东西是我们需要知道的? 下面就是一些新手常见错误汇总,当你要开始自己的ang ...
- ViewGroup.layout(int l, int t, int r, int b)四个输入参数的含义
ViewGroup.layout(int l, int t, int r, int b)这个方法是确定View的大小和位置的,然后将其绘制出来,里面的四个参数分别是View的四个点的坐标,他的坐标不是 ...
- Debian安装Nvidia最简单方法
电脑配置: Dell本本 i7+gtx1050+8g 安装bumblebee: sudo apt install bumblebee-nvidia primus 以上会自动安装nvidia驱动. bu ...
- Three.js基础探寻三——透视投影照相机
本篇主要介绍Three.js照相机中的透视投影照相机. 上一篇:正交投影照相机 5.透视投影照相机构造函数 透视投影照相机(Perspective Camera)的构造函数是: THREE.Persp ...
- 爬虫、框架scrapy
阅读目录 一 介绍 二 安装 三 命令行工具 四 项目结构以及爬虫应用简介 五 Spiders 六 Selectors 七 Items 八 Item Pipeline 九 Dowloader Midd ...
- Python学习二|Python的一些疑问
最近写了一点Python代码,作为一个java程序员,面对Python这么便捷的语言不禁有点激动.不过呢,有时候也会遇到一些无法理解的东西. 例如: er = [[1,2,3], [4,5,6], [ ...
- 报错stale element reference: element is not attached to the page document结局方案
今天在调试脚本时,遇到如下报错: org.openqa.selenium.StaleElementReferenceException: stale element reference: elemen ...
- 关于overflow: hidden;的一个诡异问题
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- 使用 Application Loader提交IPA文件到苹果市场
打包.导出ipa包后剩下的就是要将ipa包推到appstore.Application Loader是苹果提供的ipa包提交工具. 1.启动Application Loader 打开xcode,在xc ...