hdu2138 How many prime numbers 米勒测试
hdu2138 How many prime numbers
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; ll prime[] = {,,,,};
ll qmul(ll a, ll b, ll mod) {
ll res = ;
while (b) {
if (b&) res = (res+a)%mod;
a = (a+a)%mod;
b >>= ;
}
return res;
}
ll qpow(ll a, ll b, ll mod) {
ll res = ;
while (b) {
if (b&) res = qmul(res,a,mod);
a = qmul(a,a,mod);
b >>= ;
}
return res;
}
bool Miller_Rabin(ll p) {
if (p < ) return ;
if (p != && p % == ) return false;
ll s = p-;
while (!(s&)) s >>= ;
for (int i = ; i < ; i++) {
if (p == prime[i]) return true;
ll t = s, m = qpow(prime[i],s,p);
while (t != p- && m != && m != p-) {
m = qmul(m,m,p);
t <<= ;
}
if (m != p- && !(t&)) return false;
}
return true;
}
int main() {
int n;
while (~scanf("%d",&n)) {
int ans = ;
for (int i = ; i <= n; i++) {
ll x; scanf("%lld",&x);
if (Miller_Rabin(x)) ans++;
}
printf("%d\n",ans);
}
return ;
}
hdu2138 How many prime numbers 米勒测试的更多相关文章
- Project Euler 41 Pandigital prime( 米勒测试 + 生成全排列 )
题意:如果一个n位数恰好使用了1至n每个数字各一次,我们就称其为全数字的.例如,2143就是一个4位全数字数,同时它恰好也是一个素数. 最大的全数字的素数是多少? 思路: 最大全排列素数可以从 n = ...
- 2018.12.17 hdu2138 How many prime numbers(miller-rbin)
传送门 miller−rabbinmiller-rabbinmiller−rabbin素数测试的模板题. 实际上miller−rabinmiller-rabinmiller−rabin就是利用费马小定 ...
- [HDU2138]How many prime numbers
来源: HDU 2007-11 Programming Contest_WarmUp 题目大意:素数判定. 思路: 事实上暴力判定也可以过,但我还是用了Miller-Rabin算法. 核心思想:利用费 ...
- CSU 1552 Friends(二分图 + 米勒测试)
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1552 Description On an alien planet, every e ...
- poj 2739 Sum of Consecutive Prime Numbers 尺取法
Time Limit: 1000MS Memory Limit: 65536K Description Some positive integers can be represented by a ...
- Project Euler 27 Quadratic primes( 米勒测试 + 推导性质 )
题意: 欧拉发现了这个著名的二次多项式: f(n) = n2 + n + 41 对于连续的整数n从0到39,这个二次多项式生成了40个素数.然而,当n = 40时402 + 40 + 41 = 40( ...
- 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 ...
随机推荐
- udp包最大数据长度是多少
因为udp包头有2个byte用于记录包体长度. 2个byte可表示最大值为: 2^16-1=64K-1=65535 udp包头占8字节, ip包头占20字节, 65535-28 = 65507 ...
- Python 如何写 Ubuntu syslog
address='/dev/log' 是关键 import logging from logging.handlers import SysLogHandler logger = logging.ge ...
- FluentAspects -- 基于 Fluent API 的 Aop
FluentAspects -- 基于 Fluent API 的 Aop Intro 上次我们做了一个简单的 AOP 实现示例,但是实现起来主要是基于 Attribute 来做的,对于代码的侵入性太强 ...
- 最大比例 公约数复用 【蓝桥真题】 (c++)
最大比例 X星球的某个大奖赛设了M级奖励.每个级别的奖金是一个正整数.并且,相邻的两个级别间的比例是个固定值.也就是说:所有级别的奖金数构成了一个等比数列.比如:16,24,36,54其等比值为:3/ ...
- 覆盖equals 时总要覆盖hashCode(9)
2019独角兽企业重金招聘Python工程师标准>>> 1.在每个覆盖了equals 方法的类中,也必须覆盖hashCode 这是关于hashCode 的通用约定 这样可以与 基于散 ...
- 一个简易的SocketIM
今天做了一个简易的socketIM的小示例.基本思想是开启两个winform,每个winform既充当服务器也充当客户端.一个监听8000端口,另外一个监听8001端口,两个winform接收到信息之 ...
- Effective C++学习记录
Effective C++算是看完了,但是并没有完全理解,也做不到记住所有,在此记录下55个条款及条款末的"请记住". 让自己习惯C++ 条款01:视C++为一个语言联邦 ① C ...
- nginx的数据结构集合(随时更新)
在学习nginx的时候,因为其数据结构略多,看过后一般就忘记了.所以边学习边记录在这里吧,方便以后查看. ngx_buf_t:缓冲区结点 1: typedef struct ngx_buf_s ngx ...
- ubuntu 和 centos 如何区分系统
Ubuntu Ubuntu有着漂亮的用户界面,完善的包管理系统,强大的软件源支持,丰富的技术社区,Ubuntu还对大多数硬件有着良好的兼容性,包括最新的图形显卡等等.这一切让Ubuntu越来越向大众化 ...
- GCRoots
JVM面试汇总 JVM垃圾回收的时候如何确定垃圾?是否知道什么是GC Roots 什么是垃圾 简单来说就是内存中已经不再被使用的空间就是垃圾 如何判断一个对象是否可以被回收 引用计数法 Java中,引 ...