HDU-2138-How many prime numbers(Miller-Rabin新解法)
题目传送门
sol1:普通判到sqrt(n)的素数判定,不多说了。
- 素数判定
#include "bits/stdc++.h"
using namespace std;
bool is_prime(int n) {
for (int i = ; 1LL * i * i <= n; i++)
{
if (n % i == ) return false;
}
return true;
}
int main() {
int n, m;
while (~scanf("%d", &n)) {
int cnt = ;
for (int i = ; i <= n; i++) {
scanf("%d", &m);
if (is_prime(m)) cnt++;
}
printf("%d\n", cnt);
}
return ;
}复杂度sqrt(m),判断n次就是n * sqrt(m);
sol2:新get到的技巧Miller-Rabin素数测试,结合了费马小定理和二次探测定理,可以更高效的判断素数,存在误判可能,不过误判可能非常小,可以忽略不计;
- Miller-Rabin素数测试
#include "bits/stdc++.h"
using namespace std;
int quick_pow(int n, int k, int p) {
int ans = ;
while (k) {
if (k & ) ans = 1LL * ans * n % p;
n = 1LL * n * n % p;
k >>= ;
}
return ans;
}
bool is_prime(int n) {
// if (n < 2) return false;
int s = , t = n - ;
while (!(t & )) s++, t >>= ;
for (int i = ; i <= ; i++) {
int a = rand() % (n - ) + ;
int k = quick_pow(a, t, n);
for (int j = ; j <= s; j++) {
int x = 1LL * k * k % n;
if (x == && k != && k != n - ) return false;
k = x;
}
if (k != ) return false;
}
return true;
}
int main() {
int n, m;
srand(time(NULL));
while (~scanf("%d", &n)) {
int cnt = ;
for (int i = ; i <= n; i++) {
scanf("%d", &m);
if (is_prime(m)) cnt++;
}
printf("%d\n", cnt);
}
return ;
}复杂度logm,判断n次就是n * log(m);
附加一个用于 LL 范围素数测试的模板:s
- Miller-Rabin素数测试 LL 范围模板
typedef long long LL;
LL quick_mul(LL n, LL k, LL p) {
LL ans = ;
while (k) {
if (k & ) ans = (ans + n) % p;
n = (n + n) % p;
k >>= ;
}
return ans;
}
LL quick_pow(LL n, LL k, LL p) {
LL ans = ;
while (k) {
if (k & ) ans = quick_mul(ans, n, p);
n = quick_mul(n, n, p);
k >>= ;
}
return ans;
}
bool is_prime(LL n) {
if (n < ) return false;
LL s = , t = n - ;
while (!(t & )) s++, t >>= ;
for (int i = ; i <= ; i++) {
LL a = rand() % (n - ) + ;
LL k = quick_pow(a, t, n);
for (int j = ; j <= s; j++) {
LL x = quick_mul(k, k, n);
if (x == && k != && k != n - ) return false;
k = x;
}
if (k != ) return false;
}
return true;
}大致差不多,为了防止爆 LL 加了一个快速积用于乘, 所以复杂度变成了log(m) * log(m)
HDU-2138-How many prime numbers(Miller-Rabin新解法)的更多相关文章
- 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 ...
- HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )
How many prime numbers Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 2138 How many prime numbers
米勒罗宾素数测试: /* if n < 1,373,653, it is enough to test a = 2 and 3. if n < 9,080,191, it is enoug ...
- HDU 2138 How many prime numbers (判素数,米勒拉宾算法)
题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <c ...
- 【HDU】2138 How many prime numbers
http://acm.hdu.edu.cn/showproblem.php?pid=2138 题意:给n个数判断有几个素数.(每个数<=2^32) #include <cstdio> ...
- hdu 5108 Alexandra and Prime Numbers(水题 / 数论)
题意: 给一个正整数N,找最小的M,使得N可以整除M,且N/M是质数. 数据范围: There are multiple test cases (no more than 1,000). Each c ...
- hdu 5108 Alexandra and Prime Numbers
数论题,本质是求出n的最大质因子 #include<time.h> #include <cstdio> #include <iostream> #include&l ...
- HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...
- POJ1811- Prime Test(Miller–Rabin+Pollard's rho)
题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...
- poj 1811 Pallor Rho +Miller Rabin
/* 题目:给出一个数 如果是prime 输出prime 否则输出他的最小质因子 Miller Rabin +Poller Rho 大素数判定+大数找质因子 后面这个算法嘛 基于Birthday Pa ...
随机推荐
- PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]
题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family mem ...
- PCB上LED指示灯电流、电压总结
一般指示灯正常发光的电流在10~20mA,低电流LED灯的工作电流在2mA一下,亮度和普通的一样. 压降 电流 红色 1.82~1.88V ...
- matlab初级
命令 ======== 系统命令 命令 功能 例 date 显示当前日期 ans = 20-Jul-2019 what 当前文件夹下的matlab文件 type 文件中的内容 type CV.m ...
- 洛谷P4071-[SDOI2016]排列计数 题解
SDOI2016-排列计数 发现很多题解都没有讲清楚这道题为什么要用逆元.递推公式怎么来的. 我,风雨兼程三十载,只为写出一篇好题解. 还是我来造福大家一下吧. 题目大意: 一个长度为 n 且 1~n ...
- 微信oauth2授权获得用户信息
<?php session_start(); header("Content-type: text/html; charset=utf-8"); $home = 'index ...
- LeetCode——48. 旋转图像
给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 m ...
- 快速dns推荐
中国互联网络中心:1.2.4.8.210.2.4.8.101.226.4.6(电信及移动).123.125.81.6(联通) 阿里DNS:223.5.5.5.223.6.6.6 googleDNS:8 ...
- HDU-4460 Friend Chains(BFS&权为1所有最短路的最大值)
题目: For a group of people, there is an idea that everyone is equals to or less than 6 steps away fro ...
- python学习笔记-字符串的拼接
1.百分号方式拼接 %[(name)][flags][width].[precision]typecode (name) 可选,用于选择指定的key flags 可选,可供 ...
- 漫谈设计模式(二):单例(Singleton)模式
1.前言 实际业务中,大多业务类只需要一个对象就能完成所有工作,另外再创建其他对象就显得浪费内存空间了,例如web开发中的servlet,这时便要用到单例模式,就如其名一样,此模式使某个类只能生成唯一 ...