【数论】Prime Time UVA - 10200 大素数 Miller Robin 模板
题意:验证1~10000 的数 n^n+n+41 中素数的个数。每个询问给出a,b 求区间[a,b]中质数出现的比例,保留两位
题解:质数会爆到1e8 所以用miller robin ,
另外一个优化是预处理
一个坑是四舍五入卡精度。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<ctime>
using namespace std;
typedef long long ll;
const int MAXN = + + ;
const int maxn = MAXN;
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mmm(a,b) memset(a,b,sizeof(a))
int phi[MAXN], prime[MAXN];
struct Miller_Rabin
{
int prime[] = { ,,,, };
ll qmul(ll x, ll y, ll mod) {
ll ans = (x*y - (ll)((long double)x / mod * y + 1e-)*mod);
ans = (ans%mod + mod) % mod;
return ans;
}
ll qpow(ll x, ll n, ll mod) {
ll ans = ;
while (n) {
if (n & ) ans = qmul(ans, x, mod);
x = qmul(x, x, mod);
n >>= ;
}
return ans;
}
bool isprime_std(ll p) {
if (p < ) return ;
if (p != && p % == ) return ;
ll s = p - ;
while (!(s & )) s >>= ;
for (int i = ; i < ; ++i) {
if (p == prime[i]) return ;
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 ;
}
return ;
}
bool isprime(ll p) {
if (p< || (p != && p % == )) return false;
for (int i = ; i < ; ++i)
{
if (p == prime[i]) return true;
ll t = qpow(prime[i], p - , p);
if (t != ) return false;
}
return true;
}
}mr;
int tot;
void get_phi()
{
phi[] = ;
for (int i = ; i <= MAXN - ; i++) {
if (!phi[i]) {
phi[i] = i - ;
prime[++tot] = i;
}
for (int j = ; j <= tot && 1LL * i*prime[j] <= MAXN - ; j++) {
if (i%prime[j]) phi[i*prime[j]] = phi[i] * (prime[j] - );
else {
phi[i*prime[j]] = phi[i] * prime[j];
break;
}
}
}
}
int isntp[maxn];
void sieve(int n) {
int m = (int)sqrt(n + 0.5);
mmm(isntp, );
rep(i, , m)if (!isntp[i])for (int j = i * i; j <= n; j += i)isntp[j] = ; }
int ans[maxn];
int s[maxn];
int smain();
//#define ONLINE_JUDGE
int main() { //ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
FILE *myfile;
myfile = freopen("C:\\Users\\acm-14\\Desktop\\test\\b.in", "r", stdin);
if (myfile == NULL)
fprintf(stdout, "error on input freopen\n");
FILE *outfile;
outfile = freopen("C:\\Users\\acm-14\\Desktop\\test\\out.txt", "w", stdout);
if (outfile == NULL)
fprintf(stdout, "error on output freopen\n");
long _begin_time = clock();
#endif
smain();
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return ;
}
int smain()
{ int t;
int a, b; s[] = ;
rep(i, , 1e4) {
if (mr.isprime_std(i * i + i + ))s[i] = s[i - ] + ;
else s[i] = s[i - ];
}
while (cin >> a >> b)
{
int cnt = ;
/*rep(i, a, b) {
if (i * i + i + 41 < 1e7) {
if (isntp[i * i + i + 41] == 0)cnt++;
else if(mr.isprime(i * i + i + 41))cnt++;
}
}*/
cnt = s[b];
if (a != )cnt -= s[a - ];
double ans = (double)cnt / (double)(b - a + ) * ;
ans = (double)((int)(ans + 0.50000001)); printf("%.2lf\n", ans/);
}
//cin >> t;
return ;
}
/*
0 39
0 40
39 40
*/
【数论】Prime Time UVA - 10200 大素数 Miller Robin 模板的更多相关文章
- Prime Time UVA - 10200(精度处理,素数判定)
Problem Description Euler is a well-known matematician, and, among many other things, he discovered ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- Project Euler 97 :Large non-Mersenne prime 非梅森大素数
Large non-Mersenne prime The first known prime found to exceed one million digits was discovered in ...
- FZU 1649 Prime number or not米勒拉宾大素数判定方法。
C - Prime number or not Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- Miller Robin大素数判定
Miller Robin算法 当要判断的数过大,以至于根n的算法不可行时,可以采用这种方法来判定素数. 用于判断大于2的奇数(2和偶数需要手动判断),是概率意义上的判定,因此需要做多次来减少出错概率. ...
- POJ 1811 大素数判断
数据范围很大,用米勒罗宾测试和Pollard_Rho法可以分解大数. 模板在代码中 O.O #include <iostream> #include <cstdio> #inc ...
- Miller_Rabbin大素数测试
伪素数: 如果存在和n互素的正整数a满足a^(n-1)≡1(mod n),则n是基于a的伪素数. 是伪素数但不是素数的个数是非常非常少的,所以如果一个数是伪素数,那么他几乎是素数. Miller_Ra ...
- 计蒜客 18492.Upside down primes-米勒拉宾判大素数 (German Collegiate Programming Contest 2015 ACM-ICPC Asia Training League 暑假第一阶段第三场 K)
K. Upside down primes 传送门 这个题就是把大数按字符串输进去,判断一下是不是素数,然后反转180度,先判断反转之后的东西是不是一个数,如果是的话,再把这个数判一下是不是素数,如果 ...
- 重复造轮子之RSA算法(一) 大素数生成
出于无聊, 打算从头实现一遍RSA算法 第一步, 大素数生成 Java的BigInteger里, 有个现成的方法 public static BigInteger probablePrime(int ...
随机推荐
- openssl - 数字证书的编程解析
原文链接: http://www.cangfengzhe.com/wangluoanquan/37.html 这篇文章主要介绍PKI公钥体系中非常核心元素——数字证书的编程解析.在SSL,SET等安全 ...
- 关于MYSQL ERROR1045 报错的解决办法
**问题描述 **ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)或者ERROR ...
- FFmpeg: AVCodecParameters 结构体分析
/** * This struct describes the properties of an encoded stream. * * sizeof(AVCodecParameters) is no ...
- Mac下软件包管理器-homebrew
类似于redhat系统的yum,ubuntu的apt-get,mac系统下也有相应的包管理容器:homebrew.用法与apt-get.yum大同小异,都是对安装软件做一些安装删除类的命令行操作,以下 ...
- 4. OpenAI GPT算法原理解析
1. 语言模型 2. Attention Is All You Need(Transformer)算法原理解析 3. ELMo算法原理解析 4. OpenAI GPT算法原理解析 5. BERT算法原 ...
- MXNET:监督学习
线性回归 给定一个数据点集合 X 和对应的目标值 y,线性模型的目标就是找到一条使用向量 w 和位移 b 描述的线,来尽可能地近似每个样本X[i] 和 y[i]. 数学公式表示为\(\hat{y}=X ...
- Halcon 之dyn_threshold与threshold区别与用法
相同点:都是为了选择想要的灰度区域 dyn_threshold (OrigImage, MeanImage, SmallRaw, 3, 'light') //动态阈值分割 threshold()// ...
- Halcon的数据类型
两大类: 1.图形参数Iconic (image, region, XLD) 2.与控制参数Control (string, integer, real, handle), 在Halcon算子的参数中 ...
- Oracle HAVING子句 - 转
使用 HAVING 子句选择行 HAVING 子句对 GROUP BY 子句设置条件的方式与 WHERE 子句和 SELECT 语句交互的方式类似.WHERE 子句搜索条件在进行分组操作之前应用:而 ...
- .Net MVC Cache 缓存技术总结
一.细说 ASP.NET Cache 及其高级用法 二..Net环境下的缓存技术介绍 (转) 三.asp.net中缓存的使用介绍一 四.HttpContext.Current.Cache 过期时间