Nth prime & numbers of primes (模板)
都是取的模板,这几天做的素数题挺多的,所以整理了放在这里,感觉有一天回用到的!
SPOJ:Nth Prime: 求第N个素数,N<1e9。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=,P=,Q=;
struct getnthprime
{
int prime[N+],pi[N+],e[P];
void init(){
for(int i=;i<=N;i++) {
if(!prime[i]) prime[++prime[]]=i,pi[i]=pi[i-]+;
else pi[i]=pi[i-];
for(int j=;j<=prime[]&&i<=N/prime[j];j++) {
prime[i*prime[j]]=;
if(i%prime[j]==) break;
}
}
for(int i=;i<P;i++) e[i]=i;
for(int i=;i<=;i++) {
for(int j=P-;j>=;j--)
e[j]-=e[j/prime[i]];
}
}
ll get_phi(ll m,int n) {
if (n==) return m/P*Q+e[m%P];
if (m<prime[n]) return ;
if (m<=N&&m<=(ll)prime[n]*prime[n]*prime[n]) {
ll ans=pi[m]-n+;
for(int i=n+,l=pi[(int)sqrt(m+0.1)];i<=l;i++)
ans+=pi[m/prime[i]]-i+;
return ans;
}
return get_phi(m,n-)-get_phi(m/prime[n],n-);
} ll get_pi(ll m){
if(m<=N) return pi[m];
int n=pi[(int)cbrt(m-0.1)+];
ll ans=get_phi(m,n)+n-;
for(int i=n+,l=pi[(int)sqrt(m+0.1)];i<=l;i++)
ans-=get_pi(m/prime[i])-i+;
return ans;
} bool f[];
ll get_pn(ll n) {
if (n<=prime[]) return prime[n];
ll x=n*(log(n)+log(log(n))-)+n*(log(log(n))-)/log(n)-*n/;
ll y=n*(log(log(n)))*(log(log(n)))/log(n)/log(n);
y=min(y,ll());
ll l=x,r=x+y,flag = ;
for (int i=;i<;i++) {
ll m=(l+r)>> ;
ll pm=get_pi(m);
if(pm>=n) r=m,flag=;
else l=m+,flag=pm;
}
ll count=flag?flag:get_pi(l-);
for(int i=,li=pi[(int)sqrt(r+0.1)];i<=li;i++) {
for(int j=((l-)/prime[i]+)*prime[i]-l;j<=r-l+;
j+=prime[i]){
f[j]=true;
}
}
for(int i=;i<=r-l+;i++) {
if(!f[i]){
count++;
if(count==n) return i+l;
}
}
return -;
}
}NP; int main() {
NP.init();
ll n; scanf("%lld",&n);
cout<<NP.get_pn(n)<<endl;
return ;
}
HDU5901:Count primes: 求1到N有多少个素数。N<1e11。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=5e6+,M=,PM=******;
struct countprimes
{
bool np[N],did[N];
int prime[N],pi[N],phi[PM+][M+],sz[M+];
vector<ll>v;
int getprime()
{
int cnt=;
np[]=np[]=true;
pi[]=pi[]=;
for(int i=;i<N;++i){
if(!np[i]) prime[++cnt]=i; pi[i]=cnt;
for(int j=;j<=cnt&&i*prime[j]<N;++j){
np[i*prime[j]]=true;
if(i%prime[j]==) break;
}
} return cnt;
}
void init()
{
getprime();
sz[]=;
for(int i=;i<=PM;++i) phi[i][]=i;
for(int i=;i<=M;++i){
sz[i]=prime[i]*sz[i-];
for(int j=;j<=PM;++j) phi[j][i]=phi[j][i-]-phi[j/prime[i]][i-];
}
}
int sqrt2(ll x)
{
ll r=(ll)sqrt(x-0.1);
while(r*r<=x) ++r;
return int(r-);
}
int sqrt3(ll x)
{
ll r=(ll)cbrt(x-0.1);//开三次方
while(r*r*r<=x) ++r;
return int(r-);
}
ll getphi(ll x,int s)
{
if(s==) return x;
if(s<=M) return phi[x%sz[s]][s]+(x/sz[s])*phi[sz[s]][s];
if(x<=prime[s]*prime[s]) return pi[x]-s+;
if(x<=prime[s]*prime[s]*prime[s]&&x<N)
{
int s2x=pi[sqrt2(x)];
ll ans=pi[x]-(s2x+s-)*(s2x-s+)/;
for(int i=s+;i<=s2x;++i) ans+=pi[x/prime[i]];
return ans;
}
return getphi(x,s-)-getphi(x/prime[s],s-);
}
ll getpi(ll x)
{
if(x<N) return pi[x];
ll ans=getphi(x,pi[sqrt3(x)])+pi[sqrt3(x)]-;
for(int i=pi[sqrt3(x)]+,ed=pi[sqrt2(x)];i<=ed;++i) ans-=getpi(x/prime[i])-i+;
return ans;
}
ll lehmer_pi(ll x)
{
if(x<N) return pi[x];
int a=(int)lehmer_pi(sqrt2(sqrt2(x)));
int b=(int)lehmer_pi(sqrt2(x));
int c=(int)lehmer_pi(sqrt3(x));
ll sum=getphi(x,a)+(ll)(b+a-)*(b-a+)/;
for(int i=a+;i<=b;i++)
{
ll w=x/prime[i];
sum-=lehmer_pi(w);
if(i>c) continue;
ll lim=lehmer_pi(sqrt2(w));
for(int j=i;j<=lim;j++) sum-=lehmer_pi(w/prime[j])-(j-);
}
return sum;
}
}CP;
int main()
{
CP.init();
ll n,ans=;
while(~scanf("%lld",&n)){
cout<<CP.lehmer_pi(n)<<endl;
}
}
Nth prime & numbers of primes (模板)的更多相关文章
- 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 ...
- 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 ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- CodeForces - 385C Bear and Prime Numbers (埃氏筛的美妙用法)
Recently, the bear started studying data structures and faced the following problem. You are given a ...
- 快速切题 sgu113 Nearly prime numbers 难度:0
113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...
- [Algorithm] Finding Prime numbers - Sieve of Eratosthenes
Given a number N, the output should be the all the prime numbers which is less than N. The solution ...
- 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 ...
- 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 ...
随机推荐
- 【lombok】使用lombok注解,在代码编写过程中可以调用到get/set方法,但是在编译的时候无法通过,提示找不到get/set方法
错误如题:使用lombok注解,在代码编写过程中可以调用到get/set方法,但是在编译的时候无法通过,提示找不到get/set方法 报错如下: 解决方法: 1.首先查看你的lombok插件是否下载安 ...
- 【Todo】git的fast forward & git命令学习 & no-ff
git的fast-forward在之前的文章有介绍过,但是介绍的不细: http://www.cnblogs.com/charlesblc/p/5953066.html fast-forward方式就 ...
- ubuntu compile php from source code
10down vote Assuming that you already have the OpenSSL libraries and header files (on rpm systems th ...
- [转]JVM堆和栈的区别
物理地址 堆的物理地址分配对对象是不连续的.因此性能慢些.在GC的时候也要考虑到不连续的分配,所以有各种算法.比如,标记-消除,复制,标记-压缩,分代(即新生代使用复制算法,老年代使用标记——压缩) ...
- Git的使用 -- 用git玩翻github,结尾有惊喜!有惊喜!有惊喜!林妙妙看了说:牛呲呼啦带闪电 (三)(超详解)
简介 上一篇主要讲解的是Git安装及配置,这一篇就详细的从无到有的来用Git玩翻github. 一.什么是Github Github是全球最大的社交编程及代码托管网站(https://github.c ...
- C#串口通讯教程 简化一切 只保留核心功能 这可能是最易于理解的一篇教程
C#串口通讯教程 简化一切 只保留核心功能 这可能是最易于理解的一篇教程 串口的定义,请自行了解. C#操作串口通讯在.Net强大类库的支持下,只需要三个步骤: 1 创建 2 打开 3 发送/接受 ...
- Network Booting
http://en.wikipedia.org/wiki/Network_booting Network booting Network booting is the process of booti ...
- 设计模式入门之訪问者模式Visitor
//訪问者模式定义:表示一个作用于某对象结构中的各个元素的操作,它使你能够在不改变各元素类的前提下定义作用于这些元素的新操作. //从定义上看.这个模式跟装饰模式的定义非常类似(动态地给一个对象加入一 ...
- 线程、SMP、微内核
- 基于bootstrap_后台管理
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...