[51nod 1184] Nth prime number

Source: Internet
Author: User
Main Topic

Find nth prime number, n<=1e9 mess

Drin_e taught me the Chau GE sieve.
But it seems not orthodox.
We can make two points, so we just judge the problem of the number of mass.
Set C[i] denotes the first prime number.
F[i] Indicates the number of the mass of I.
Set g (n,m) to indicate numbers that are not divisible by c[1~m within N.
Set Q=√n, then N of the number of characters f[q]+g (N,f[q])-1 (1 is not prime to subtract)
Apparently G (n,m) =g (n,m-1)-G (n/c[m],m-1)
Pay attention to the boundary m=0 or 1
and metaphysical optimization.
Set MAXN as the pretreatment range
When N<=MAXN
If f[n]<=m, only 1 matches return 1
Otherwise, if f[√n]<=m, return f[n]+1-m (except for prime numbers outside M prime)
See code for details.
Complexity is not proven.
MAXN open to 1e7 almost.

 #include <cstdio> #include <algorithm> #include <cmath> #define FO (i,a,b)
for (i=a;i<=b;i++) using namespace std;
typedef long Long LL;
const int MAXN=12500000+10;
ll F[MAXN],C[MAXN];
BOOL BZ[MAXN];
ll I,j,k,t,n,m,q,top;
ll L,r,mid;
    ll G (ll n,int m) {if (!m) return n;
    if (m==1) return N-N/2;
        if (n<=maxn-10) {if (f[n]<=m) return 1;
    if (f[(int) sqrt (n)]<=m) return f[n]+1-m;
} return G (N,m-1)-G (n/c[m],m-1);
    } ll count (ll N) {Q=floor (sqrt (n));
Return F[q]+g (N,f[q])-1;
        } int main () {fo (i,2,maxn-10) {if (!bz[i]) c[++top]=i;
            Fo (j,1,top) {if (i*c[j]>maxn-10) break;
            Bz[i*c[j]]=1;
        if (i%c[j]==0) break;
    }} f[1]=0;
    Fo (i,2,maxn-10) f[i]=f[i-1]+ (!bz[i]);
    scanf ("%d", &n);
    l=1;r=22801763489;
        while (L<r) {mid= (l+r)/2;
    if (count (mid) >=n) R=mid;else l=mid+1;
} printf ("%lld\n", L); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.