P1463 [poi2002][haoi2007] Inverse prime

Source: Internet
Author: User

Hit the table a miracle!!!

The violence can certainly be done, but n==2 \times ^9\ not allow violence.

Let's play a miracle!!!

First look at how to efficiently calculate the approximate number of a number:

The most violent is from \ (1\) enumeration to \ (n\), every time + +.

One of the optimizations is to enumerate only to \ (\sqrt{n}\). But it's still very slow.

Let's take a look at the legend's approximate number theorem:

For a positive integer \ (n\), the unique decomposition theorem can be decomposed into \ (p_1^{a_1} \times p_2^{a_2} \times ... \times p_i^{a_i}\).

So this is about a few numbers ( (a_1+1) (a_2+1) ... (a_i+1) \).

So we can write the table program with a good time complexity.

Because the only decomposition theorem requires prime numbers, we can divide the current prime number by the addition of the next, until it is 1.

Because the product of the first 10 prime numbers is already greater than 2e9, the first 10 prime numbers are directly artificially typed.

This is the Play table program:

#pragma optimize(2) //我也学会了这个手动O2#include<cstdio>const int maxn = 2000000005;const int N = maxn - 5;const int prime[] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29};// 10int maxv;int main(){    freopen("out.txt", "w", stdout);     for(int i = 2; i <= N; i++)    {        int temp = i, ans = 1;        for(int j = 1; j <= 10; j++)        {            int cnt = 0;            while(temp % prime[j] == 0)            {                temp = temp / prime[j];                cnt++;            }            ans = ans * (cnt + 1);            if(temp == 1) break;        }        if(ans > maxv)        {            printf("%d\n", i);            maxv = ans;        }    }    return 0;}

The code runs out of about 180s, which is much faster than the second algorithm above.

And then I'm going to convert it to a constant array of these numbers in C + + and it becomes our table.

Then the main program is very brain-free:

#include<cstdio>int g[] = {0, 2, 4, 6, 12, 24, 36, 48, 60, 120, 180, 240, 360, 720, 840, 1260, 1680, 2520, 5040, 7560, 10080, 15120, 20160, 25200, 27720, 45360, 50400, 55440, 83160, 110880, 166320, 221760, 277200, 332640, 498960, 554400, 665280, 720720, 1081080, 1441440, 2162160, 2882880, 3603600, 4324320, 6486480, 7207200, 8648640, 10810800, 14414400, 17297280, 21621600, 32432400, 36756720, 43243200, 61261200, 73513440, 110270160, 122522400, 147026880, 183783600, 245044800, 294053760, 367567200, 551350800, 698377680, 735134400, 1102701600, 1396755360};// 67int main(){    int n; scanf("%d", &n);    for(int i = 1; i <= 67; i++)    {        if(g[i] > n && g[i - 1] <= n)        {            printf("%d\n", g[i - 1]);            return 0;        }    }    printf("%d\n", g[67]);    return 0;}

P1463 [poi2002][haoi2007] Inverse prime

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.