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 ...
随机推荐
- 转:Linux字符编码方式
首先,解释一下字符集: 汉字编码: * GB2312字集是简体字集,全称为GB2312(80)字集,共包括国标简体汉字6763个. * BIG5字集是台湾繁体字集,共包括国标繁体汉字13053个. * ...
- python type
基于2.7 版本 type 是内置函数,有两种用法 class type(object) With one argument, return the type of an object. The re ...
- 如何删除xcode启动主页面项目列表
Open Xcode, leave the splash screen up and choose "File", "Open Recent Projects" ...
- mysql 建立utf8字符集数据库
CREATE DATABASE `evaluate` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
- android项目大全,总有你所需的
注:打开请贴网址.有些直接通过链接打开的不对. 1.相对布局实例 http://kukuqiu.iteye.com/blog/1018396 2.Log图文具体解释(Log.v,Log.d,Log. ...
- (转) SYSTEM_HANDLE_INFORMATION中ObjectTypeIndex的定义
typedef struct _SYSTEM_HANDLE_TABLE_ENTRY_INFO{ USHORT UniqueProcessId; USHORT CreatorBackTraceIndex ...
- 双向队列(STL做法)
双向队列 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 想想双向链表--双向队列的定义差点儿相同,也就是说一个队列的队尾同一 ...
- SAE云平台的使用
参考文章:http://www.cnblogs.com/luyangsblog/p/3956135.html Web开发从零单排之一:在新浪云平台SAE上开发一个htm ...
- 【转载】一张“神图”看懂单机/集群/热备/磁盘阵列(RAID)
单机部署(stand-alone):只有一个饮水机提供服务,服务只部署一份 集群部署(cluster):有多个饮水机同时提供服务,服务冗余部署,每个冗余的服务都对外提供服务,一个服务挂掉时依然可用 热 ...
- 一篇很好的讲解SIFT算法的文章
http://blog.csdn.net/zddblog/article/details/7521424