都是取的模板,这几天做的素数题挺多的,所以整理了放在这里,感觉有一天回用到的!

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 (模板)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  4. CodeForces - 385C Bear and Prime Numbers (埃氏筛的美妙用法)

    Recently, the bear started studying data structures and faced the following problem. You are given a ...

  5. 快速切题 sgu113 Nearly prime numbers 难度:0

    113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...

  6. [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 ...

  7. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  8. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  9. 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 ...

随机推荐

  1. solus系统配置

    #更新软件源 清华稳定源 sudo eopkg ar Tuna https://mirrors.tuna.tsinghua.edu.cn/solus/shannon/eopkg-index.xml 清 ...

  2. 2017 ACM/ICPC Asia Regional Urumuqi Online 记录

    比赛题目链接  Urumuqi

  3. Python3:urllib模块的使用

    Python3:urllib模块的使用1.基本方法 urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=N ...

  4. 2015多校联合训练赛 Training Contest 4 1008

    构造题: 比赛的时候只想到:前面一样的数,后面 是类似1,2,3,4,5,6....t这 既是:t+1,t+1...,1,2,3,...t t+1的数目 可能 很多, 题解时YY出一个N 然后对N   ...

  5. T13432 1.计数

    题目描述 给出m个数a[1],a[2],…,a[m] 求1~n中有多少数不是a[1],a[2],…,a[m]的倍数. 输入输出格式 输入格式: 输入文件名为count.in. 第一行,包含两个整数:n ...

  6. codeforces 946G

    题意: 有一个长度为n的数组a.你可以删除一个位置之后进行操作,一次操作可以把任意位置上的数字变成任意的值,问最少需要多少操作能使得数列变成严格上升的. n<=200000 分析: 如果没有删除 ...

  7. CodeBlock换肤

    CodeBlock换肤 conf文件下载地址 我的是在D:\Program Files (x86)\CodeBlocks\AppData\codeblocks\default.conf 然后替换本地的 ...

  8. idea tomcat 配置

    昨天我们讲了如何新建多模块项目:idea创建maven多模块项目 本节课,我们讲如何配置tomcat,使昨天配置的web项目,JRapid.Admin可以运行起来.具体步骤如下 第一步 第二步 第三步 ...

  9. Credit Memo和Debit Memo在AR以及AP中的概念比较

    AR和AP中都有Credit Memo和Debit Memo的概念, 但是其含义和用法完全不一样,比较难懂,现在整理如下:AR中的CreditMemo和DebitMemo是和客户打交道:AR中的Cre ...

  10. RMA Sales Order – Stuck with “Awaiting Return Disposition”

    RMA Sales Order – Stuck with “Awaiting Return Disposition” Action : (Prod) ð  Login to Unix ð  su – ...