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

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. Redis命令行之Zset

    一.Redis之Zset简介 1. 有序集合Zset是String类型的有序集合. 2. Zset中每个元素都会关联一个double类型的分数值,redis通过分数值来为集合中所有成员进行从小到大排序 ...

  2. android实现通知栏消息

    一.原理 消息推送有两种,一种是客户端定时直接到服务器搜索消息,如果发现有新的消息,就获取消息下来:另一种是服务器向客户端发送消息,也就是当有信息消息时,服务器端就会向客户端发送消息. 二.步骤(代码 ...

  3. Container With Most Water 双指针法

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  4. HDU 4341 Gold miner(分组背包)

    题目链接 Gold miner 目标是要在规定时间内获得的价值总和要尽可能大. 我们先用并查集把斜率相同的物品分在同一个组. 这些组里的物品按照y坐标的大小升序排序. 如果组内的一个物品被选取了,那该 ...

  5. vue之条件渲染

    一.v-if v-if指令用于条件的渲染一块内容,当指令的表达式返回true时,内容才会被渲染. <h1 v-if="isshow">要显示么</h1> d ...

  6. BZOJ——1649: [Usaco2006 Dec]Cow Roller Coaster

    http://www.lydsy.com/JudgeOnline/problem.php?id=1649 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 7 ...

  7. HBASE的安装过程及运行HBASE程序的需要配置的内容

    HBase安装配置 ①下载压缩包(选择与自己安装的Hadoop版本的兼容版本,见后面附录) 官网下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/hba ...

  8. Mybatis详解

    SqlSession(SqlSessionDaoSupport类) SqlSessionDaoSupportSqlSessionDaoSupport是一个抽象的支持类,用来为你提供SqlSession ...

  9. 【kotlin】基本语法when的使用,类似于java中的switch,但是又青出于蓝而胜于蓝

    when(要判断的参数){ 参数值为1 ->做这种事情 参数值为2 ->做另一种事情 else -> 类似于switch中的default } 扩展使用:https://www.cn ...

  10. C#如何设置控件水平对齐,垂直对齐

    如果要设置一些控件垂直对齐,点击这个按钮 如果要设置水平对齐,则点击这个按钮,选中控件之后点击左对齐(多个按钮都试下吧,总归能对齐到你要的效果的)