HDU 2138 Miller-Rabin 模板题
求素数个数。
/** @Date : 2017-09-18 23:05:15
* @FileName: HDU 2138 miller-rabin 模板.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; LL fpow(LL a, LL n, LL mod)//快速幂
{
LL res = 1;
while(n)
{
if(n & 1) res = (res * a) % mod;
a = (a * a % mod + mod) % mod;
n >>= 1;
}
return res;
}
bool Miller_Rabbin(int n,int a)//米勒拉宾素数测试
{
int r = 0, s = n - 1;
if(!(n % a))
return false;
while(!(s & 1))
{
s>>=1;
r++;
}
LL k = fpow(a, s, n);
if(k == 1)
return true;
for(int j = 0; j < r; j++, k = k * k % n)
if(k == n-1)
return true;
return false;
}
bool IsPrime(int n)//判断是否是素数
{
int tab[14] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41};
for(int i = 0; i < 13; i++)
{
if(n == tab[i])
return true;
if(!Miller_Rabbin(n, tab[i]))
return false;
}
return true;
} int main()
{
int n;
while(cin >> n)
{
LL x;
LL cnt = 0;
for(int i = 0; i < n; i++)
scanf("%ld", &x), cnt += (IsPrime(x)?1:0);
printf("%lld\n", cnt);
}
return 0;
}
HDU 2138 Miller-Rabin 模板题的更多相关文章
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
- HDU 2544 最短路(模板题)
求1到N的最短路径,模板题,以1为源点,用dijkstra算法(可以用优先级队列优化) #include <iostream> #include <algorithm> #in ...
- Number Sequence - HDU 1711(KMP模板题)
题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...
- 【网络流#1】hdu 3549 - 最大流模板题
因为坑了无数次队友 要开始学习网络流了,先从基础的开始,嗯~ 这道题是最大流的模板题,用来测试模板好啦~ Edmonds_Karp模板 with 前向星 时间复杂度o(V*E^2) #include& ...
- HDU 4825 Xor Sum (模板题)【01字典树】
<题目链接> 题目大意: 给定n个数,进行m次查找,每次查找输出n个数中与给定数异或结果最大的数. 解题分析: 01字典树模板题,01字典树在求解异或问题上十分高效.利用给定数据的二进制数 ...
- HDU 5901 Count primes (模板题)
题意:给求 1 - n 区间内的素数个数,n <= 1e11. 析:模板题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ...
随机推荐
- HDU 5418 Victor and World 允许多次经过的TSP
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5418 bestcoder(中文): http://bestcoder.hdu.edu.cn ...
- IT小小鸟的读后感
在我经历了半个学期的大学生活后,我依然不清楚我现在所学的专业有什么用或者说该怎么学.直到我阅读了<我是一只IT小小鸟>这篇文章之后.我才对我所将来或许要从事的IT事业有了些许的了解. 在观 ...
- 未能加载文件或程序集 system.Web.Http.WebHost解决办法。
在csdn中找到一个方法: Update-Package Microsoft.AspNet.WebApi -reinstall 然后就好了. 另外一个方法是缺少哪个dll,就复制一个dll放到bin文 ...
- 第三章 ServerSpcket用法详解
构造ServerSocket ServerSocket的构造方法如下: ServerSocket() //Creates an unbound server socket. ServerSocket( ...
- 解释Spring中IOC, DI, AOP
oc就是控制翻转或是依赖注入.通俗的讲就是如果在什么地方需要一个对象,你自己不用去通过new 生成你需要的对象,而是通过spring的bean工厂为你长生这样一个对象.aop就是面向切面的编程.比如说 ...
- 【leetcode】54.Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- libmnl
https://www.netfilter.org/projects/libmnl/doxygen/modules.html 1,tar xvf libmnl-1.0.4.tar.gz 2,cd li ...
- centOS 中安装 Redis
之前安装过了 jdk,mysql,tomcat,这次安装 Redis,最开始是将 redis 安装在 windows 下 run 的,这时安装在 Linux 里面试试. 1 . 首先得安装 c环境,用 ...
- lxs1314 is not in the sudoers file. This incident will be reported.
虚拟机下面 普通用户用sudo执行命令时报"xxx is not in the sudoers file.This incident will be reported"错误,解决 ...
- HDU——1573 X问题
又来一发水题. 解同余方程而已,用类似于剩余定理的方法就O了. 直接上代码:(注意要判断是否有解这种情况) #include <iostream> #include <cstdio& ...