题意:验证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 模板的更多相关文章

  1. Prime Time UVA - 10200(精度处理,素数判定)

    Problem Description Euler is a well-known matematician, and, among many other things, he discovered ...

  2. [ACM] POJ 2689 Prime Distance (筛选范围大素数)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12811   Accepted: 3420 D ...

  3. Project Euler 97 :Large non-Mersenne prime 非梅森大素数

    Large non-Mersenne prime The first known prime found to exceed one million digits was discovered in ...

  4. FZU 1649 Prime number or not米勒拉宾大素数判定方法。

    C - Prime number or not Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  5. Miller Robin大素数判定

    Miller Robin算法 当要判断的数过大,以至于根n的算法不可行时,可以采用这种方法来判定素数. 用于判断大于2的奇数(2和偶数需要手动判断),是概率意义上的判定,因此需要做多次来减少出错概率. ...

  6. POJ 1811 大素数判断

    数据范围很大,用米勒罗宾测试和Pollard_Rho法可以分解大数. 模板在代码中 O.O #include <iostream> #include <cstdio> #inc ...

  7. Miller_Rabbin大素数测试

    伪素数: 如果存在和n互素的正整数a满足a^(n-1)≡1(mod n),则n是基于a的伪素数. 是伪素数但不是素数的个数是非常非常少的,所以如果一个数是伪素数,那么他几乎是素数. Miller_Ra ...

  8. 计蒜客 18492.Upside down primes-米勒拉宾判大素数 (German Collegiate Programming Contest 2015 ACM-ICPC Asia Training League 暑假第一阶段第三场 K)

    K. Upside down primes 传送门 这个题就是把大数按字符串输进去,判断一下是不是素数,然后反转180度,先判断反转之后的东西是不是一个数,如果是的话,再把这个数判一下是不是素数,如果 ...

  9. 重复造轮子之RSA算法(一) 大素数生成

    出于无聊, 打算从头实现一遍RSA算法 第一步, 大素数生成 Java的BigInteger里, 有个现成的方法 public static BigInteger probablePrime(int ...

随机推荐

  1. iOS开发 关于启动页和停留时间的设置

    引言: 在开发一款商业App时,我们大都会为我们的App设置一个启动页. 苹果官方对于iOS启动页的设计说明: 为了增强应用程序启动时的用户体验,您应该提供一个启动图像.启动图像与应用程序的首屏幕看起 ...

  2. 解决:ngxin做http强制跳转https,接口的POST请求变成GET

    域名配置了http强制跳转htpps后发现app发起post请求会出现405错误. 所以怀疑是http强制跳转https出现了问题.修改nginx配置如下即可解决: server { listen ; ...

  3. python风格的抽象工厂模式

    抽象工厂模式: 提供一个接口,用户创建多个相关或依赖对象,而不需要指定具体类. 原则: 依赖抽象,不依赖具体类. 实例: 用不同原材料制作不同口味的披萨,创建不同原材料的工厂,不同实体店做出口味不同的 ...

  4. Android实现电话录音功能

    需求分析 电话录音是在通话的时候进行录音,所以需要使用一个服务来完成功能. 需要监听电话的状态,分为三种状态:  空闲状态 TelephonyManager.CALL_STATE_IDLE 响铃状态 ...

  5. excel中批量删除公式,保留数值

    excel中批量删除公式,保留数值 Sub macro1() Dim sh As Worksheet For Each sh In Sheets sh.UsedRange = sh.UsedRange ...

  6. 音视频处理ffmpeg使用

    参考资料: [url]http://blog.163.com/prosen@yeah/blog/static/12251328720099101378975/ http://ffmpeg.org/ff ...

  7. DOTween 使用方法

    参考链接: http://dotween.demigiant.com/documentation.php https://www.cnblogs.com/backlighting/p/5344047. ...

  8. [PGM] Variable elimination and Belief propagation

    内容范围如题.Lec 08-11的内容:https://www.youtube.com/watch?v=Qa04kw1gKHk&index=36&list=PLQiVpyxVlLkbp ...

  9. [GPU] DIY for Deep Learning Workstation

    Link: jcjohnson/cnn-benchmarks Ref: Build a super fast deep learning machine for under $1,000 Graphi ...

  10. iOS shell脚本打包

    原文链接:http://www.jianshu.com/p/5abbe0d61cef 参考链接:http://blog.csdn.net/potato512/article/details/52176 ...