Bzoj 3181| Coci 2012| Broj| Mathematics | The second point | Sieve method __ Maths

Source: Internet
Author: User
Tags square root

Hey, Rank 7?

The square root algorithm is also not called the square root algorithm.
Make h (n) H (n) the standard for us to classify. (H (n) The expression seems to be in 2015 or 2014 of the national Team paper)
If P≥h (n) p\geq H (n), the Sieve method finds the prime number smaller than p in [1,109p] [1,\frac{10^9}{p}], and then selects nth digits.
P≤h (n) p\leq H (n), first find the prime number of [1,p] [1,p], and the second-division.
As for the value of H (n) H (n), it is not deduced theoretically. This value is based on experience.
Taking 65 seems to be more excellent.

#include <cstdio> #include <cstring> #define REP (i,j,k) for (i=j;i<k;++i) typedef long LL;
const int C =, N = 1000000000;
BOOL v[20000000];
int pri[100], tot, N, p;
    ll f (int mid, int p) {int i, j, K; ll ret = Mid, D, F;
    Rep (i,2,p) v[i] = 0;
    tot = 0;
        Rep (i,2,p) {if (!v[i]) pri[tot++] = i;
            for (j = 0; J < tot && I * pri[j] < P ++j) {V[i * Pri[j]] = 1;
        if (i% pri[j] = = 0) break;
        for (k = 1; k < (1 << tot); ++k) {d = 1, f =-1;
            Rep (I,0,tot) if (K & (1 << i)) {D *= pri[i];
            if (d > mid) break;
        f *=-1;
    RET-= f * MID/D;
return ret;
    int Work1 () {int ans = 0, L = 1, r = n/p;
        while (L < r) {int mid = L + R >> 1;
        if (f (Mid, p) < n) L = mid + 1;
    else R = Mid;
    if (f (l, p) = n) ans = l * p;
return ans; } int Work2 ({int ans = 0, len = n/p + 1, I, k = 1;
    if (n = = 1) ans = p;
    Rep (i,2,len) v[i] = 0;  Rep (I,2,len) if (!v[i]) if (I < P) for (ll j = (ll) i * i; j < len; j = i) V[j]
        = 1;
    else if (++k = n) ans = i * p;
return ans;
    int main () {while (scanf ("%d%d", &n, &p)!= EOF) printf ("%d\n", p < C? Work1 (): Work2 ());
return 0; }
3181: [Coci2012]broj Description

To find a small positive integer with the smallest mass factor equal to P (just a n-1 is equal to P and it
A small positive integer). P must be a prime number. Output 0 If the answer exceeds 10^9. Sample Input

2 3 Sample Output

9 HINT

1 <= N, p <= 10^9 Solution

To solve this for large values of p we'll use modification of the sieve
of Eratosthenes. Size of our sieve would be 109/p 10^9/p integers in the sieve
Represent multiples of P p. During the execution of this algorithm we can
Find smallest prime factors or mark multiples of prime numbers
Smaller than p as in the official solution.
For smaller values of P we can binary search through [1,109/p] [1, 10^9/p],
Again looking at numbers as the corresponding multiples of p.
For some number we must find the number of integers not greater and
Relatively prime with that number. We can do this by using inclusionexclusion
Principle with prime numbers less than P p.
With careful implementation this solution can work for much larger
Values of p p than requested for this subtask.
We can also solve this task to smaller value of p by making the use of
Periodic behaviour of smallest prime factors. Let A (n) a (n) be the smallest
Prime factor of n N, b (k) B (k) the K k-th prime number, and T (k)

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.